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.

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