Python-OpenCV 图像处理(十四):超大图像二值化

import cv2
import numpy as np
from matplotlib import pyplot as plt

__author__ = "zxsuperstar"
__email__ = "[email protected]"

"""
超大图像二值化
分块
(全局阈值、局部阈值)
"""
def big_image_binary(image):
    print(image.shape)
    cw = 256
    ch = 256
    h, w = image.shape[0:2]
    gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
    for row in range(0,h,ch):
        for col in range(0,w,cw):
            roi = gray[row:row + ch ,col:cw+col]
            # ret,dst = cv2.threshold(roi, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
            dst = cv2.adaptiveThreshold(roi,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,127,20)
            gray[row:row + ch ,col:cw+col] = dst
            print(np.std(dst),np.mean(dst))
    cv2.imwrite("./result_bigimage1.jpg",gray)


if __name__ == "__main__":
    src = cv2.imread("verybig.jpg") #blue green red
    # cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
    # cv2.imshow("image",src)
    big_image_binary(src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


猜你喜欢

转载自blog.csdn.net/zx_good_night/article/details/88679767