OpenCV computer vision -- image threshold processing and adaptive threshold Otsu

Threshold processing refers to eliminating pixels in the image that are higher or lower than the threshold. Image thresh...
Adaptive threshold processing
Otsu processing

Threshold processing refers to eliminating pixels in the image that are higher or lower than the threshold. Image threshold processing is mainly to set a threshold: greater than this number is given a value, less than a number is given another value, and the pixel value of the image is changed into the middle of two gray values to realize image segmentation. Image thresholding segmentation is a traditional and most commonly used image segmentation method. It has become the most basic and widely used segmentation technology in image segmentation because of its simple implementation, small amount of calculation and stable performance.

catalogue

1. Introduction to image threshold processing function threshold()

  2. Five common threshold processing methods

3. Adaptive threshold and Otsu processing

Adaptive threshold processing

Otsu processing

1. Introduction to image threshold processing function threshold()

The cv.threshold() function in OpenCV library is mainly used for threshold processing

cv.threshold(src,thresh,maxval,type,dst=None)

src: input picture

thresh: threshold

maxval: maximum threshold, only for THRESH_BINARY and THRESH_BINARY_INV useful

type: mainly includes the following types:

  The corresponding types are explained as follows: (picture from Detailed explanation of opencv threshold function _leenux0810blog CSDN blog _opencvthreshold)

  2. Five common threshold processing methods

The five threshold processing methods are mainly binarization (cv.THRESH_BINARY), inverse binarization (cv.THRESH_BINARY_INV), truncation (cv.THRESH_TRUNC), over threshold zeroing (cv.THRESH_TOZERO_INV) and low threshold zeroing (cv.THRESH_TOZERO) The five threshold processing methods are to compare the pixel value of each point with the threshold value, and then give a fixed pixel value to realize image segmentation. The specific significance is shown in the table above. Without much nonsense, look at the code first:

import cv2 as cv # Read picture img = cv.imread('zzf10.jpg') img = cv.resize(img, None, fx=0.4, fy=0.4) img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) cv.imshow('img', img) # Image binarization threshold processing t, img1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY) cv.imshow('THRESH_BINARY', img1) # Inverse binarization threshold processing of image t, img2 = cv.threshold(img, 127, 255, cv.THRESH_BINARY_INV) cv.imshow('THRESH_BINARY_INV', img2) # Image truncation thresholding t, img3 = cv.threshold(img, 127, 255, cv.THRESH_TRUNC) cv.imshow('THRESH_TRUNC', img3) # Image super zero processing t, img4 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO) cv.imshow('THRESH_TOZERO', img4) # Low threshold zero processing of image t, img5 = cv.threshold(img, 127, 255, cv.THRESH_TOZERO_INV) cv.imshow('THRESH_TOZERO_INV', img5) cv.waitKey(0) cv.destroyAllWindows()

The operation results are as follows:

Binarization threshold processing is to compare the threshold of each point, which is greater than thresh, which is given maxval, and less than thresh, which is given 0; inverse binarization threshold processing, which is greater than thresh, which is given thresh, which is less than thresh; truncation threshold processing, which is greater than thresh, which is less than thresh, which maintains the original pixel value; over threshold zeroing processing, which is greater than thresh, which is given 0, which is less than thresh Original pixel value; low threshold zeroing processing, keep the original pixel value if it is greater than thresh, and give 0 if it is less than thresh. The image effect is obvious, and you can understand the specific functions of each operation yourself.

So the problem comes. Our threshold processing deals with grayscale images. Can we deal with color images? What is the effect of processing? Just note out the grayscale images above. The operation results are as follows:

As you can see, this seems to be inconsistent with the purpose and characteristics of threshold processing. I searched some data and don't quite understand why. However, I have some ideas. I think that the threshold processing of images is to compare the size with a single data, so it should be compared with a one-dimensional single channel image; while the color image is a three channel image, which may be in a certain area One channel processing or multiple channel processing. The specific threshold comparison method is not clear, so it is very different from the effect and purpose introduced by threshold processing  . If you have any ideas, you can discuss them in the comment area!

3. Adaptive threshold and Otsu processing

Adaptive threshold processing

Adaptive threshold segmentation is also called local thresholding. It determines the threshold at the pixel position according to the pixel value distribution of the pixel's neighborhood block. The advantage of this is that the threshold at each pixel position is not fixed, but determined by the distribution of the surrounding neighborhood pixels. The threshold of the image area with higher brightness is usually higher, while the image with lower brightness is usually higher When the color of the image is uneven, only one threshold can not effectively segment the image, so adaptive threshold processing is needed.

The cv.adaptiveThreshold() function needs to be used for adaptive threshold processing. The specific parameters are described as follows:

cv.adaptiveThreshold(src,maxValue,adaptiveMethod,thresholdType,blockSize,C,dst=None)

src: input image

maxValue: maximum value

Adaptive method: there are two adaptive methods: one is cv.ADAPTIVE_THRESH_MEAN_C, where the weights of pixel values in the field are the same; the other is cv.ADAPTIVE_THRESH_GAUSSIAN_C, where the weights of pixel values in the field are different, which is related to the distance to the center, showing Gaussian distribution, and the weights of each point are obtained through Gaussian equation.

thresholdType: represents the threshold processing method. The value must be one of cv.THRESH_BINARY or cv.THRESH_BINARY_INV

blockSize: the size of the block, the size used to calculate the threshold, 3, 5, 7, etc

C: Constant subtracts the constant C from the weighted average of the fields specified by each blockSize

The specific codes are as follows:

import cv2 as cv img = cv.imread('zzf10.jpg') img = cv.resize(img, None, fx=0.3, fy=0.3) img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) cv.imshow('img_', img) # Image binarization threshold processing t, img1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY) # The method of consistent weight of domain pixels is used to adapt the threshold img2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 5, 3) # The weight of domain pixels is used to calculate the weight through Gaussian equation to adapt the threshold img3 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 5, 3) cv.imshow('THRESH_BINARY', img1) cv.imshow('ADAPTIVE_THRESH_MEAN_C', img2) cv.imshow('ADAPTIVE_THRESH_GAUSSIAN_C', img3) cv.waitKey(0) cv.destroyAllWindows()

The operation results are as follows:

It can be seen that adaptive threshold segmentation can segment more details and features than binary threshold segmentation  . Setting multiple thresholds in the image can better process the pixel values of each region of the image, and the segmentation effect is better.

Otsu processing

When we perform cv.threshold() threshold processing on an image, this threshold is often set randomly by ourselves and cannot segment the image well. The selection of threshold is very important in threshold processing and image segmentation. Otsu processing can traverse all pixel values to find the best inter class segmentation threshold, and then perform cv.threshold operation. The specific code is as follows:

import cv2 as cv img = cv.imread('zzf10.jpg') img = cv.resize(img, None, fx=0.3, fy=0.3) img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) cv.imshow('img', img) # Image binarization threshold processing t, img1 = cv.threshold(img, 127, 255, cv.THRESH_BINARY) # Binary threshold processing using Otsu processing t2, img2 = cv.threshold(img, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU) cv.imshow('THRESH_BINARY', img1) cv.imshow('OTSU', img2) cv.waitKey(0) cv.destroyAllWindows()

The operation results are as follows:

It can be seen that the threshold obtained by Otsu processing is 145, and the segmentation effect is better than ordinary binarization processing. The picture is clearer and the features are clearer.

Summary: This paper describes five basic methods of image threshold processing, and introduces the adaptive threshold method. For the basic image threshold processing, Otsu is used to obtain and iterate the best inter class segmentation threshold to obtain the best threshold processing effect, so as to make the features of image segmentation more obvious and effective.

3 December 2021, 09:03 | Views: 5060

Add new comment

For adding a comment, please log in
or create account

0 comments