如果像素值大于一个阈值将它赋予一个值,如果小于一个值就给他赋予一个值。
cv.threshold
参数 | 描述 |
---|---|
src | 图片指针 |
dst | 输出 |
thresh | 阈值 |
maxVal | 设置最大值 |
thresholdType | 阈值类型 |
阈值类型 | 描述 |
---|---|
THRESH_BINARY | 过门限的值设置为maxVal,其它值置零 |
THRESH_BINARY_INV | 过门限的值置零,其它值设置为maxVal |
THRESH_TRUNC | 过门限的值设置为门限值,其它值置不变 |
THRESH_TOZERO | 过门限的值不变,其它值置零 |
THRESH_TOZERO_INV | 过门限的值置零,其它值不变 |
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('gradient.png',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,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 xrange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
自适应阈值
上面的设置阈值是对全局作用的,自适应阈值可以自动分析各个部分的阈值。这对不同光照条件下的图片,会产生更好的效果。
参数 | 描述 |
---|---|
src | 图片指针 |
dst | 二值化后的图像 |
maxValue | 二值化要设置的值 |
method | 计算方法(ADAPTIVE_THRESH_MEAN_C,ADAPTIVE_THRESH_GAUSSIAN_C) |
type | 二值化类型(CV_THRESH_BINARY,大于为最大值;CV_THRESH_BINARY_INV小于为最大值) |
blockSize | 块大小 |
delta | 差值 |
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('sudoku.png',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C,\
cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in xrange(4):
plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
Otsu’s Binarization
双峰图是一个直方图有两个峰值的图像。我们可以在这些峰的中间取一个值作为阈值。这就是Otsu Bin的作用
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('noisy2.png',0)
# global thresholding
ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in xrange(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()
参考文献:
https://docs.opencv.org/3.4.3/d7/d4d/tutorial_py_thresholding.html
https://blog.youkuaiyun.com/qq_41905045/article/details/81333216
https://blog.youkuaiyun.com/iracer/article/details/49232703