# 3.2. 图像阈值化¶

## 3.2.1. 目标¶

• 在本教程中，您将学习简单阈值、自适应阈值、Otsu阈值等。

## 3.2.2. 简单阈值¶

• cv2.THRESH_BINARY

• cv2.THRESH_BINARY_INV

• cv2.THRESH_TRUNC

• cv2.THRESH_TOZERO

• cv2.THRESH_TOZERO_INV

>>> import cv2
>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>>
>>> 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()

<Figure size 640x480 with 6 Axes>


## 3.2.3. 自适应阈值¶

-自适应阈值是邻域面积的平均值。-自适应阈值是权重为高斯窗口的邻域值的加权和。

C -它只是一个从计算出的平均值或加权平均值中减去的常数。

>>> %matplotlib inline
>>>
>>> import cv2
>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>>
>>> # img = cv2.imread('/cvdata/apple.jpg', 0)
>>> img = cv2.medianBlur(img,5)
>>>
>>> ret,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
>>>             cv2.THRESH_BINARY,11,2)
>>>             cv2.THRESH_BINARY,11,2)
>>>
>>> titles = ['Original Image', 'Global Thresholding (v = 127)',
>>> 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.2.4. 大津二值化¶

>>> import cv2
>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>>
>>>
>>>
>>> # 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()


### 大津的二值化是如何工作的？¶

$\sigma_w^2(t) = q_1(t)\sigma_1^2(t)+q_2(t)\sigma_2^2(t)$

$q_1(t) = \sum_{i=1}^{t} P(i) \quad \& \quad q_1(t) = \sum_{i=t+1}^{I} P(i)$

$\mu_1(t) = \sum_{i=1}^{t} \frac{iP(i)}{q_1(t)} \quad \& \quad \mu_2(t) = \sum_{i=t+1}^{I} \frac{iP(i)}{q_2(t)}$

$\sigma_1^2(t) = \sum_{i=1}^{t} [i-\mu_1(t)]^2 \frac{P(i)}{q_1(t)} \quad \& \quad \sigma_2^2(t) = \sum_{i=t+1}^{I} [i-\mu_1(t)]^2 \frac{P(i)}{q_2(t)}$

>>> img = cv2.imread('/cvdata/noisy2.png',0)
>>> blur = cv2.GaussianBlur(img,(5,5),0)
>>>
>>> # find normalized_histogram, and its cumulative distribution function
>>> hist = cv2.calcHist([blur],[0],None,[256],[0,256])
>>> hist_norm = hist.ravel()/hist.max()
>>> Q = hist_norm.cumsum()
>>>
>>> bins = np.arange(256)
>>>
>>> fn_min = np.inf
>>> thresh = -1
>>>
>>> for i in range(1,256):
>>>     p1,p2 = np.hsplit(hist_norm,[i]) # probabilities
>>>     q1,q2 = Q[i],Q[255]-Q[i] # cum sum of classes
>>>     b1,b2 = np.hsplit(bins,[i]) # weights
>>>
>>>     # finding means and variances
>>>     m1,m2 = np.sum(p1*b1)/q1, np.sum(p2*b2)/q2
>>>     v1,v2 = np.sum(((b1-m1)**2)*p1)/q1,np.sum(((b2-m2)**2)*p2)/q2
>>>
>>>     # calculates the minimization function
>>>     fn = v1*q1 + v2*q2
>>>     if fn < fn_min:
>>>         fn_min = fn
>>>         thresh = i
>>>
>>> # find otsu's threshold value with OpenCV function
>>> ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
>>> print (thresh,ret)

118 117.0

/usr/lib/python3/dist-packages/ipykernel_launcher.py:20: RuntimeWarning: invalid value encountered in double_scalars
/usr/lib/python3/dist-packages/ipykernel_launcher.py:20: RuntimeWarning: divide by zero encountered in double_scalars
/usr/lib/python3/dist-packages/ipykernel_launcher.py:21: RuntimeWarning: invalid value encountered in multiply


（有些功能可能是新的，但我们将在接下来的章节中介绍）

1. 数字图像处理

## 3.2.6. 练习¶

1. 有一些优化可用于大津的二值化。你可以搜索并实现它。