OpenCV自适应二值化

《OpenCV系列教程》
项目位置:OpenCV-Sample
代码位置:18-AdaptThresholding.py

import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread('./res/CarID.jpeg',0)

ret,thresh1 = cv.threshold(img,120,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,120,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,120,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,120,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,120,255,cv.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

OpenCV自适应二值化_第1张图片
使用自适应二级化,让编程更加方便。

  • cv.ADAPTIVE_THRESH_MEAN_C 取自相邻区域的平均值。
  • cv.ADAPTIVE_THRESH_GAUSSIAN_C 取相邻区域的加权和,权重为一个高斯窗口。

你可能感兴趣的:(OpenCV)