Image thresholding _ have cv2.threshold, cv2.adaptiveThreshold like.

1. Simple Threshold

Functions used: cv2.threshold (src, thresh, maxval, type)

Comment:

The name suggests, this method is very simple. However, when the pixel value above the threshold, we give this gives a new pixel value (possibly white), otherwise we gave it gives another color (maybe black). This function is cv2.threshhold (). The first argument is the original image, the original image should be grayscale. The second parameter is used to threshold the pixel value classification. When the third parameter is higher than the pixel value (sometimes less) than a threshold value should be assigned new pixel value. OpenCV provides several different methods of thresholds, which is the fourth parameter determined. These include:
• cv2.THRESH_BINARY
• cv2.THRESH_BINARY_INV
• cv2.THRESH_TRUNC
• cv2.THRESH_TOZERO
• cv2.THRESH_TOZERO_INV

 

 

 Code:

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

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

运行结果如下图所示:

2.自适应阈值

使用的函数:dst = cv2.adaptiveThreshold(src, maxval, thresh_type, type, Block Size, C)

注释:

在前面的部分我们使用是全局阈值,整幅图像采用同一个数作为阈值。当时这种方法并不适应与所有情况,尤其是当同一幅图像上的不同部分的具有不同亮度时。这种情况下我们需要采用自适应阈值。此时的阈值是根据图像上的每一个小区域计算与其对应的阈值。因此在同一幅图像上的不同区域采用的是不同的阈值,从而使我们能在亮度不同的情况下得到更好的结果。这种方法需要我们指定三个参数,返回值只有一个。
• Adaptive Method- 指定计算阈值的方法。
– cv2.ADPTIVE_THRESH_MEAN_C:阈值取自相邻区域的平
均值
– cv2.ADPTIVE_THRESH_GAUSSIAN_C:阈值取值相邻区域
的加权和,权重为一个高斯窗口。
• Block Size - 邻域大小(用来计算阈值的区域大小)。
• C - 这就是是一个常数,阈值就等于的平均值或者加权平均值减去这个常
数。

代码:

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

img = cv2.imread('test.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_GAUSSIAN_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', 'Adaptivw 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()

结果如下所示:

3.Otsu’s 二值化

在第一部分中我们提到过retVal,当我们使用Otsu 二值化时会用到它。那么它到底是什么呢?在使用全局阈值时,我们就是随便给了一个数来做阈值,那我们怎么知道我们选取的这个数的好坏呢?答案就是不停的尝试。如果是一副双峰图像(简单来说双峰图像是指图像直方图中存在两个峰)呢?我们岂不是应该在两个峰之间的峰谷选一个值作为阈值?这就是Otsu 二值化要做的。简单来说就是对一副双峰图像自动根据其直方图计算出一个阈值。(对于非双峰图像,这种方法得到的结果可能会不理想)。这里用到到的函数还是cv2.threshold(),但是需要多传入一个参数(flag):cv2.THRESH_OTSU。这时要把阈值设为0。然后算法会找到最优阈值,这个最优阈值就是返回值retVal。如果不使用Otsu 二值化,返回的retVal 值与设定的阈值相等。

下面的例子中,输入图像是一副带有噪声的图像。第一种方法,我们设127 为全局阈值。第二种方法,我们直接使用Otsu 二值化。第三种方法,我们首先使用一个5x5 的高斯核除去噪音,然后再使用Otsu 二值化。看看噪音去除对结果的影响有多大吧。

代码:

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

img = cv2.imread('test.jpg', 0)

ret,th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret,th2 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

blur = cv2.GaussianBlur(img, (5,5), 0)
#阈值一定要设为0
ret3, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

images = [img, 0, th1,
        img, 0, th2,
        blur, 0, th3]

titles = ['Original Noisy Images', '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()

结果如图所示:

 使用的原图如下:

Guess you like

Origin www.cnblogs.com/leoych/p/12114834.html