计算机视觉基础:自适应阈值分割(Computer Vision Fundamentals: Adaptive Threshold Segmentation)

前言

阈值分割方法虽然简单,但是如果场景简单,还是可以尝试使用的。因为其消耗的时间较少。

同时,也可以作为一个baseline来验证提出的新算法是否有效。

对于阈值分割,我们认为没有理由讲了,这里主要介绍两种自适应阈值分割方法,实际工程应用过程中,我们发现这些方法还是挺好用的。

自适应阈值

opencv中给我们提供了一种自适应阈值的方法,即:将整个图像分成一个个的patch,给每个path分别计算一个自适应的阈值。

如:

cv2.ADAPTIVE_THRESH_MEAN_C: threshold value is the mean of neighbourhood area.

cv2.ADAPTIVE_THRESH_GAUSSIAN_C: threshold value is the weighted sum of neighbourhood values where weights are a gaussian window.

这里有两个参数,一个是path的尺寸Block Size,另一个是常数C:

Block Size - It decides the size of neighbourhood area.

C - It is just a constant which is subtracted from the mean or weighted mean calculated.

其分割效果如下:

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()

在这里插入图片描述

大津阈值 (OTSU)

核心思想:

最小化类间方差。

直观理解:

考虑一个具有双峰的图像 (直方图有两个峰),一个最适合的阈值应该是在两个峰值的中间,大津法就是希望找到这两个峰之间的值作为阈值。

实例分析:

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

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

在这里插入图片描述

理论推导

OTSU的目的是最小化类内方差,即最小化:

σ w 2 ( t ) = q 1 ( t ) σ 1 2 ( t ) + q 2 ( t ) σ 2 2 ( t ) \sigma_{w}^{2}(t)=q_{1}(t) \sigma_{1}^{2}(t)+q_{2}(t) \sigma_{2}^{2}(t) σw2(t)=q1(t)σ12(t)+q2(t)σ22(t)

其中,

q 1 ( t ) = ∑ i = 1 t P ( i ) & q 2 ( t ) = ∑ i = t + 1 I P ( i ) μ 1 ( t ) = ∑ i = 1 t i P ( i ) q 1 ( t ) & μ 2 ( t ) = ∑ i = t + 1 I i P ( i ) q 2 ( t ) σ 1 2 ( t ) = ∑ i = 1 t [ i − μ 1 ( t ) ] 2 P ( i ) q 1 ( t ) & σ 2 2 ( t ) = ∑ i = t + 1 I [ i − μ 1 ( t ) ] 2 P ( i ) q 2 ( t ) \begin{array}{r} q_{1}(t)=\sum_{i=1}^{t} P(i) \quad \& \quad q_{2}(t)=\sum_{i=t+1}^{I} P(i) \\ \mu_{1}(t)=\sum_{i=1}^{t} \frac{i P(i)}{q_{1}(t)} \quad \& \quad \mu_{2}(t)=\sum_{i=t+1}^{I} \frac{i P(i)}{q_{2}(t)} \\ \sigma_{1}^{2}(t)=\sum_{i=1}^{t}\left[i-\mu_{1}(t)\right]^{2} \frac{P(i)}{q_{1}(t)} \quad \& \quad \sigma_{2}^{2}(t)=\sum_{i=t+1}^{I}\left[i-\mu_{1}(t)\right]^{2} \frac{P(i)}{q_{2}(t)} \end{array} q1(t)=i=1tP(i)&q2(t)=i=t+1IP(i)μ1(t)=i=1tq1(t)iP(i)&μ2(t)=i=t+1Iq2(t)iP(i)σ12(t)=i=1t[iμ1(t)]2q1(t)P(i)&σ22(t)=i=t+1I[iμ1(t)]2q2(t)P(i)

Python 实现 (OpenCV demo)

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

参考文献

[1] Image Thresholding

猜你喜欢

转载自blog.csdn.net/xuyangcao123/article/details/115189534