OpenCV 中的圖像處理 003_圖像閾值

本文主要內容來自於 OpenCV-Python 教程OpenCV 中的圖像處理 部分,這部分的全部主要內容如下:

目標

簡單閾值

在這裏,事情是直截了當的。對於每個像素,應用相同的閾值。如果像素值比閾值小,則設置爲 0,否則設置爲最大值。函數 cv.threshold 用於應用閾值。第一個參數是源圖像,它 應該是一幅灰度圖。第二個參數是用於分類像素值的閾值。第三個參數是分配給超出閾值的像素值的最大值。OpenCV 提供了不同類型的閾值,它們由函數的第四個參數給出。如上所述的基本閾值處理是通過使用類型 cv.THRESH_BINARY 完成的。所有的簡單閾值類型如下:

參考這些類型的文檔來了解它們之間的差異。

這些方法放回兩個輸出。第一個是使用的閾值,第二個輸出是 閾值圖像

如下這段代碼比較了不同的簡單閾值類型:

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

def simple_thresholding():
    cv.samples.addSamplesDataSearchPath("/media/data/my_multimedia/opencv-4.x/samples/data")
    img = cv.imread(cv.samples.findFile('gradient.png'), 0)
    # img = cv.imread('gradient.png', 0)
    ret, thresh1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
    ret, thresh2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV)
    ret, thresh3 = cv.threshold(img, 127, 255, cv.THRESH_TRUNC)
    ret, thresh4 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO)
    ret, thresh5 = cv.threshold(img, 127, 255, cv.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', vmin=0, vmax=255)
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])
    plt.show()


if __name__ == "__main__":
    simple_thresholding()

注意 爲了 plot 多幅圖像,我們使用了 plt.subplot() 函數。請參考 matplotlib 的文檔來了解更多細節。

上面這段代碼生成的結果如下:

Image

自適應閾值

在前一節中,我們使用一個全局值作爲閾值。但這可能並不適用於所有情況,例如,如果圖像在不同區域具有不同的照明條件。在這種情況下,自適應閾值可以提供幫助。在這裏,該算法根據像素周圍的小片區域確定像素的閾值。因此,我們爲同一圖像的不同區域獲得了不同的閾值,這對於具有不同照明的圖像提供了更好的結果。

除了上面描述的參數,cv.adaptiveThreshold 方法接收三個輸入參數:

adaptiveMethod 決定了如何計算閾值:

blockSize 決定了鄰域的大小,C 是從鄰域像素的平均值或加權和中減去的常數。

下面的代碼比較了具有不同照明的圖像的全局閾值和自適應閾值:

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

def adaptive_thresholding():
    cv.samples.addSamplesDataSearchPath("/media/data/my_multimedia/opencv-4.x/samples/data")
    img = cv.imread(cv.samples.findFile('sudoku.png'), 0)
    img = cv.medianBlur(img, 5)
    ret, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
    th2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2)
    th3 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.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 range(4):
        plt.subplot(2, 2, i + 1), plt.imshow(images[i], 'gray')
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])
    plt.show()


if __name__ == "__main__":
    adaptive_thresholding()

最終的結果如下: image

看上去 cv.ADAPTIVE_THRESH_GAUSSIAN_C 有一定的圖像降噪作用在。

Otsu 的二值化

在全局閾值中,我們使用任意選擇的值作爲閾值。 相比之下,Otsu 的方法避免了必須選擇一個值並自動確定它。

考慮只有兩個不同圖像值的圖像(bimodal image,雙峯圖像),其中直方圖僅包含兩個峯值。一個好的閾值將在這兩個值的中間。類似地,Otsu 的方法從圖像直方圖中確定一個最佳的全局閾值。

爲此,使用 cv.threshold() 函數,其中 cv.THRESH_OTSU 作爲額外標誌傳遞。閾值可以任意選擇。該算法然後找到作爲第一個輸出返回的最佳閾值。

看看下面的例子。輸入圖像是噪聲圖像。在第一種情況下,應用值爲 127 的全局閾值。在第二種情況下,直接應用 Otsu 的閾值。在第三種情況下,首先使用 5x5 高斯覈對圖像進行過濾以去除噪聲,然後應用 Otsu 閾值。看下噪聲過濾如何改善結果。

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

def otsu_thresholding():
    cv.samples.addSamplesDataSearchPath("/media/data/my_multimedia/opencv-4.x/samples/data")
    img = cv.imread(cv.samples.findFile('noisy2.png'), 0)
    #img = cv.imread('noisy2.png', 0)
    # global thresholding
    ret1, th1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY)
    # Otsu's thresholding
    ret2, th2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
    # Otsu's thresholding after Gaussian filtering
    blur = cv.GaussianBlur(img, (5, 5), 0)
    ret3, th3 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.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()


if __name__ == "__main__":
    otsu_thresholding()

代碼中用的 noisy2.png 圖如下: noisy2.png

結果如下:

image

Otsu 的二值化是如何工作的?

本節演示了 Otsu 二進制化的 Python 實現,以展示它的實際工作原理。如果不感興趣,可以跳過這個。

由於我們使用的是雙峯圖像,Otsu 的算法試圖找到一個閾值 (t),以最小化由如下關係給出的加權類內方差:

$$\sigma_w^2(t) = q_1(t)\sigma_1^2(t)+q_2(t)\sigma_2^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_2(t)]^2 \frac{P(i)}{q_2(t)} $$ $$ \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_2(t)]^2 \frac{P(i)}{q_2(t)} $$

它實際上找到了一個位於兩個峯值之間的 t 值,這樣兩個類的方差都最小。 它可以簡單地用 Python 實現,如下所示:

def otsu_alg():
    img = cv.imread('noisy2.png', 0)
    blur = cv.GaussianBlur(img, (5, 5), 0)
    # find normalized_histogram, and its cumulative distribution function
    hist = cv.calcHist([blur], [0], None, [256], [0, 256])
    hist_norm = hist.ravel() / hist.sum()
    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
        if q1 < 1.e-6 or q2 < 1.e-6:
            continue
        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 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
    print("{} {}".format(thresh, ret))

其它資源

  1. Digital Image Processing, Rafael C. Gonzalez

練習

  1. Otsu 的二值化有一些優化。你可以搜索並實現它。

參考文檔

Image Thresholding

Done.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章