OpenCV- Image Threshold - simple thresholding, adaptive thresholding, Otsu's binarization

  • Simple Threshold

  Function: threshold (the src, Thresh, MAXVAL, type, DST = None)

    The four parameters are a function of the original image, threshold value, maximum value, a threshold type 

  Threshold type generally divided into five:  cv2.THRESH_BINARY : a pixel portion becomes larger than the threshold value MAXVAL , the other becomes 0  cv2.THRESH_BINARY_INV : part becomes larger than the threshold 0, the other portion becomes the maximum  cv2.THRESH_TRUNC : greater than the threshold value portion becomes a threshold, otherwise unchanged  cv2.THRESH_TOZERO : invariant portion greater than the threshold, the rest becomes 0  cv2.THRESH_TOZERO_INV : part becomes larger than the threshold 0, otherwise unchanged
    
    
    
    
    

 1 import numpy as np
 2 import cv2
 3 from matplotlib import pyplot as plt
 4 
 5 # 读取灰度图
 6 img = cv2.imread("../image/sight.jpg", 0)
 7 
 8 ret1, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
 9 ret2, thresh2=cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
10 ret3, thresh3=cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
11 ret4, thresh4=cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
12 ret5, thresh5=cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
13 
14 titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
15 images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
16 for i in range(6):
17     plt.subplot(2,3,i + 1),plt.imshow(images[i], 'gray')
18     plt.title(titles[i])
19     plt.xticks([]), plt.yticks([])
20 plt.show()

result:

  •  Adaptive threshold

  In the previous section we use a global threshold, the entire image using the same number as the threshold value. This method does not adapt to time in all cases, especially when having different brightness of different portions of the same image. In this case we need to use an adaptive threshold. At this time, the threshold value is calculated for each small area on the image corresponding threshold. Thus the same in different regions of an image using different thresholds, so that we can get better results at different brightness.

  adaptiveThreshold (src, maxValue, adaptiveMethod,
thresholdType, blockSize, C, dst = None)   This method requires us to specify six parameters, return values only one.

    • Adaptive Method, - specify the method of calculating the threshold.
      - cv2.ADPTIVE_THRESH_MEAN_C : threshold value from the average value of the adjacent areas
      - cv2.ADPTIVE_THRESH_GAUSSIAN_C : threshold values and weighting adjacent regions, a weight of a Gaussian window.
    • Block Size - neighborhood size (region size calculated for the threshold value).
    • C - This is a constant, the threshold value is equal to the average value or weighted average value by subtracting this constant.

Code:

 1 import numpy as np
 2 import cv2
 3 from matplotlib import pyplot as plt
 4 
 5 # 读取灰度图
 6 img = cv2.imread("../image/sight.jpg", 0)
 7 
 8 # 中值滤波,用来平滑图像,去除噪声
 9 img = cv2.medianBlur(img, 5)
10 
11 # 简单阈值
12 ret, thresh1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
13 
14 # 自适应,阈值取相邻区域的平均值
15 thresh2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
16 
17 # 自适应,阈值取值相邻区域的加权和,权重为一个高斯窗口。
18 thresh3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
19 
20 
21 titles = ['Original Image', 'Global Thresholding (v = 127)',
22 'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
23 images = [img, thresh1, thresh2, thresh3]
24 for i in range(4):
25     plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
26     plt.title(titles[i])
27     plt.xticks([]),plt.yticks([])
28 plt.show()

结果:

如果不进行滤波处理,会出现噪声,如下图所示:

 

Guess you like

Origin www.cnblogs.com/bingma/p/11208945.html