opencv python 图像二值化/简单阈值化/大津阈值法

Image Thresholding

1简单的阈值化

cv2.threshold第一个参数是源图像,它应该是灰度图像. 第二个参数是用于对像素值进行分类的阈值, 第三个参数是maxVal,它表示如果像素值大于(有时小于)阈值则要给出的值. OpenCV提供不同类型的阈值,它由函数的第四个参数决定. 不同的类型是:

cv2.THRESH_BINARY如果 src(x,y)>threshold ,dst(x,y) = max_value; 否则,dst(x,y)=0
cv.THRESH_BINARY_INV如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value
cv.THRESH_TRUNC如果 src(x,y)>threshold,dst(x,y) = max_value; 否则dst(x,y) = src(x,y)
cv.THRESH_TOZERO如果src(x,y)>threshold,dst(x,y) = src(x,y) ; 否则 dst(x,y) = 0
cv.THRESH_TOZERO_INV如果 src(x,y)>threshold,dst(x,y) = 0 ; 否则dst(x,y) = src(x,y)

代码

import cv2
import numpy as np
import matplotlib.pylab  as plt

img = cv2.imread('img.jpg',0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.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 python 图像二值化/简单阈值化/大津阈值法

2自适应阈值化

图像在不同区域具有不同照明条件时,应进行自适应阈值处理.因此,我们为同一图像的不同区域获得不同的阈值,并且它为具有不同照明的图像提供了更好的结果.
cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])
adaptiveMethod:决定如何计算阈值

  • cv2.ADAPTIVE_THRESH_MEAN_C:阈值是邻域的平均值
  • cv2.ADAPTIVE_THRESH_GAUSSIAN_C:阈值是邻域值的加权和,其中权重是高斯窗口

blockSize:决定了邻域的大小
C:从计算的平均值或加权平均值中减去的常数

代码

import cv2
import numpy as np
import matplotlib.pylab  as plt

img = cv2.imread('img.jpg',0)
img = cv2.medianBlur(img,5)

ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
            cv2.THRESH_BINARY,11,2)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
            cv2.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 range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

[lz是密集恐惧症,有点不忍直视……]

opencv python 图像二值化/简单阈值化/大津阈值法

3 大津阈值法

根据双峰图像的图像直方图自动计算阈值。 (对于非双峰图像,二值化不准确。)

使用cv.threshold()但是传递了一个额外的标志v.THRESH_OTSU.对于阈值,只需传递零.然后算法找到最佳阈值并返回为第二个输出retVal。如果未使用Otsu阈值法,则retVal与之前使用的阈值相同.

在第一种情况下,将全局阈值应用为值127.在第二种情况下,直接应用了Otsu的阈值.在第三种情况下,使用5x5高斯内核过滤图像以消除噪声,然后应用Otsu阈值处理.
代码

import cv2
import numpy as np
import matplotlib.pylab  as plt

img = cv2.imread('img.jpg',0)
# global thresholding
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

# Otsu's thresholding
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.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 range(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()

opencv python 图像二值化/简单阈值化/大津阈值法