"Digital Image Processing-OpenCV/Python" Serial: Threshold Processing of Images

"Digital Image Processing-OpenCV/Python" Serial: Threshold Processing of Images


This book’s JD discount purchase link https://item.jd.com/14098452.html
This book’s CSDN exclusive serial column https://blog.csdn.net/youcans/category_12418787.html

Insert image description here


Chapter 9 Thresholding of Images


Image threshold processing is simple, intuitive, and fast in calculation. It is the preprocessing process of many image processing algorithms.


Summary of this chapter

  • Learn image threshold processing methods and understand the impact of different thresholds on processing results.
  • Introduces threshold processing methods that utilize local features of images, such as adaptive threshold processing and moving average threshold processing.
  • Introducing the HSV model to learn color image thresholding based on the HSV color range.

9.1 Fixed threshold processing

Dividing the image into multiple areas or extracting target objects in the image according to the gray value and the limits of the gray value is the most basic threshold processing method.

When the grayscale distribution of the target and background in the image is obvious, a fixed threshold can be used for global thresholding of the entire image. If there are obvious boundaries in the histogram of the image, it is easy to find the segmentation threshold of the image, but if the histogram boundary of the image is not obvious, it is difficult to find a suitable threshold, and it may even be impossible to find a fixed threshold to effectively segment the image.

When there is noise in the image, it is often difficult to completely separate the boundaries of the image by global thresholding. If the boundaries of the image occur under local contrast, using global thresholding will perform poorly.

The function cv.threshold in OpenCV is used to threshold images.

function prototype

cv.threshold(src, thresh, maxval, type[, dst]) → retval, dst

Parameter Description

  • src: Input image, which is a multi-dimensional Numpy array, allowing single-channel images or multi-channel images.
  • dst: Output image, the same size and number of channels as src.
  • thresh: Threshold, which is floating point data and ranges from 0 to 255.
  • maxval: maximum value, refers to the saturation limit, used for some transformation types, generally 255.
  • type: Threshold transformation type.
    • THRESH_BINARY: Set to maxval when greater than the threshold thresh, otherwise set to 0.
    • THRESH_BINARY_INV: Set to 0 when greater than the threshold thresh, otherwise set to maxval.
    • THRESH_TRUNC: When it is greater than the threshold thresh, it is set to the threshold thresh, otherwise it remains unchanged.
    • THRESH_TOZERO: remains unchanged when it is greater than the threshold thresh, otherwise it is set to 0.
    • THRESH_TOZERO_INV: Set to 0 when it is greater than the threshold thresh, otherwise it remains unchanged.
    • THRESH_OTSU: Use the OTSU algorithm to automatically determine the threshold and can be used in combination.
    • THRESH_TRIANGLE: Use the Triangle algorithm to automatically determine the threshold, which can be used in combination.
  • retval: The returned threshold image.

Note the problem
(1) retval is usually a binary threshold image, but in some types (such as TRUNC, TOZERO, TOZERO_INV) it returns a threshold saturated image.
(2) The function allows the input of single-channel or multi-channel images, but when inputting multi-channel images, each channel must be thresholded independently. The returned threshold image is also a multi-channel image, not a black and white binary image, so be particularly careful when using it.
(3) When the threshold transformation type is using OTSU algorithm or Triangle algorithm, it can only process 8-bit single-channel input images.
(4) When the threshold transformation type is using OTSU algorithm or Triangle algorithm, threshold threshold does not work.


[Routine 0901] Fixed threshold method of threshold processing

This routine uses a fixed threshold method to threshold grayscale images. For multimodal grayscale distribution images, the threshold size will seriously affect the results of threshold processing.


# 【0901】阈值处理之固定阈值法
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

if __name__ == '__main__':
    # 生成灰度图像
    hImg, wImg = 512, 512
    img = np.zeros((hImg, wImg), np.uint8)  # 创建黑色图像
    cv.rectangle(img, (60, 60), (450, 320), (127, 127, 127), -1)  # 矩形填充
    cv.circle(img, (256, 256), 120, (205, 205, 205), -1)  # 圆形填充
    # 添加高斯噪声
    mu, sigma = 0.0, 20.0
    noiseGause = np.random.normal(mu, sigma, img.shape)
    imgNoise = np.add(img, noiseGause)
    imgNoise = np.uint8(cv.normalize(imgNoise, None, 0, 255, cv.NORM_MINMAX))

    # 阈值处理
    _, imgBin1 = cv.threshold(imgNoise, 63, 255, cv.THRESH_BINARY)  # thresh=63
    _, imgBin2 = cv.threshold(imgNoise, 125, 255, cv.THRESH_BINARY)  # thresh=125
    _, imgBin3 = cv.threshold(imgNoise, 175, 255, cv.THRESH_BINARY)  # thresh=175

    plt.figure(figsize=(9, 6))
    plt.subplot(231), plt.axis('off'), plt.title("1. Original"), plt.imshow(img, 'gray')
    plt.subplot(232), plt.axis('off'), plt.title("2. Noisy image"), plt.imshow(imgNoise, 'gray')
    histCV = cv.calcHist([imgNoise], [0], None, [256], [0, 256])
    plt.subplot(233, yticks=[]), plt.title("3. Gray hist")
    plt.bar(range(256), histCV[:, 0]), plt.axis([0, 255, 0, np.max(histCV)])
    plt.subplot(234), plt.axis('off'), plt.title("4. threshold=63"), plt.imshow(imgBin1, 'gray')
    plt.subplot(235), plt.axis('off'), plt.title("5. threshold=125"), plt.imshow(imgBin2, 'gray')
    plt.subplot(236), plt.axis('off'), plt.title("6. threshold=175"), plt.imshow(imgBin3, 'gray')
    plt.tight_layout()
    plt.show()


Program description:
(1) The running results, using the fixed threshold method to threshold the image, are shown in Figure 9-1. Figure 9-1(1) shows the test pattern generated by the program, Figure 9-1(2) shows the Gaussian noise added to Figure 9-1(1), and Figure 9-1(3) shows The grayscale histogram in Figure 9-1(2) has three significant grayscale peak distributions.
(2) Figures 9-1(4) to (6) show the binary images obtained by thresholding Figure 9-1(2) with different thresholds. The results are completely different. It shows that for multimodal gray distribution images, the threshold size will seriously affect the results of threshold processing.
(3) The segmentation results in Figure 9-1(4)~(6) all contain a large amount of noise, indicating that noise will also affect the results of threshold processing.

Figure 9-1 Thresholding images using fixed threshold methodInsert image description here
**


Copyright statement:
youcans@xupt Original work, reprints must be marked with the original link: (https://blog.csdn.net/youcans/article/details/135164986)
Copyright 2023 youcans, XUPT
Crated: 2023-12-23

"Digital Image Processing-OpenCV/Python" exclusive serial column : https://blog.csdn.net/youcans/category_12418787.html

Guess you like

Origin blog.csdn.net/youcans/article/details/135164986