1. 程式人生 > >第十二天 分塊處理二值法及去雪花

第十二天 分塊處理二值法及去雪花

import cv2 as cv
import numpy as np
def big_image_binary(image):
    print (image.shape)
    cw =56
    ch=56
    h,w = image.shape[:2]
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    for row in range(0,h,ch):
        for col in range(0,w,cw):
            rol =gray[row:row+ch,col:cw+col]
            #ret,dst=cv.threshold(rol,0,255, cv.THRESH_BINARY | cv.THRESH_OTSU)    #使用全域性域值
            #gray[row:row + ch, col:cw + col] = dst
            dev = np.std(rol)               ##計算矩陣標準差
            if dev<15:                    ##方差<15  填充成255
                gray[row:row + ch, col:cw + col] = 255
            else:
                ret, dst = cv.threshold(rol, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
                gray[row:row + ch, col:cw + col] = dst

            #print(np.std(dst),np.mean(dst))
    cv.imshow("dst",gray)
    #cv.imwrite("D:/vcprojects/result_binary.png", gray)


src = cv.imread("C:/Users/weiqiangwen/Desktop/sest/lena.png")
# cv.namedWindow("input contours",cv.WINDOW_AUTOSIZE)
cv.imshow("contours", src)
big_image_binary(src)
cv.waitKey(0)

cv.destroyAllWindows()