这篇文章提到的二值化算法在去年写论文的时候用到了,该文章中给出了matlab代码,转义成了python代码,供大家参考
链接:
文章链接
def adaptive_otsu(img_g):
h, w = img_g.shape
mask = np.zeros_like(img_g)
winHalfWidth = 10
localVarThresh = 0.002
for i in range(0,w):
new_img = img_g[:, max(1,i-winHalfWidth): min(w,i+winHalfWidth)]
th , th_otsu = cv.threshold(new_img, 0, 255, cv.THRESH_OTSU)
intile = np.var(new_img / 255)
if intile > localVarThresh:
_, mask[:,i:i+1] = cv.threshold(img_g[:,i:i+1], th, 255, cv.THRESH_BINARY)
else:
mask[:, i:i+1] = 255
前一张为原图,后一张为效果图
![](https://img-blog.csdnimg.cn/20210120092121914.jpg#pic_left)
![](https://img-blog.csdnimg.cn/20210119155941130.bmp#pic_left)