Image Thresholding图像阙值化和Adaptive Thresholding

要用到的函数是cv2.threshold()

这个参数的形式是cv.Threshold(src, dst, threshold, maxValue, thresholdType)

Parameters:
  • src – input array (single-channel, 8-bit or 32-bit floating point).
  • dst – output array of the same size and type as src.
  • thresh – threshold value.
  • maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
  • type – thresholding type (see the details below).

 

一开始没明白thresh和maxval的区别,后来问了同学知道了,maxval就是指超过阙值的部分取maxval这个值

五种不同的type出现的效果图不一样,参考如下

Image Thresholding图像阙值化和Adaptive Thresholding_第1张图片

Image Thresholding图像阙值化和Adaptive Thresholding_第2张图片

tip:在官网tutorial中是for i in xrange(6),在Python3中 range和xrange已经合并成了range 。

matplotlib的一个函数plt.subplot(),可以绘制多个子图,第一个参数是行数,第二个参数是列数,第三个参数表示图形的标号。

贴上代码

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('gradient.png',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()

Adaptive Thresholding

函数形式:cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst])

Parameters:
  • src – Source 8-bit single-channel image.
  • dst – Destination image of the same size and the same type as src .
  • maxValue – Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
  • adaptiveMethod – Adaptive thresholding algorithm to use, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C . See the details below.
  • thresholdType – Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .
  • blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
  • C – Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well. 从计算的平均值或者加权平均值减去的一个常数

 

为什么每次dst这个参数都被省略了。。。

 

贴上代码

 

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('dave.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 xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

 

 

效果图

Image Thresholding图像阙值化和Adaptive Thresholding_第3张图片

你可能感兴趣的:(Image Thresholding图像阙值化和Adaptive Thresholding)