14、超大图像二值化方法

分块全局阈值

def big_image_binary(image):
    '''
        超大图像二值化:
            cw = ch = 256:表示分成一块块256长的正方形   
    '''
    cw = 256
    ch = 256
    h, w = image.shape[:2]

    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    for row in range(0, h, ch):  # 0~h,步长为256
        for col in range(0, w, cw):  # 0~h,步长为256
            roi = gray[row:row+ch, col:col+cw]  # roi区域
            ret, dst = cv.threshold(
                roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
            gray[row:row+ch, col:col+cw] = dst
            print(np.std(dst), np.mean(dst))  # std:方差,mean:均值
    cv.imwrite("./BigBinary.jpg", gray)
全局分块,显然效果不是特别好

分块局部阈值

 roi = gray[row:row+ch, col:col+cw]  # roi区域
 dst = cv.adaptiveThreshold(roi, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 127, 10)
 gray[row:row+ch, col:col+cw] = ds
高斯自适应阈值

你可能感兴趣的:(14、超大图像二值化方法)