[Reproduced] OTSU algorithm of OpenCV-Python series (17)

In the last tutorial, we talked about two methods of image binarization, one is the fixed threshold method, and the other is the adaptive threshold method. In general, the adaptive threshold method is due to fixed threshold in some aspects. Threshold method, but it is not over yet, this time we will introduce our heavyweight player, that is, OTSU algorithm (also known as Otsu algorithm and maximum between-class variance method).

The maximum between-class variance method was proposed by Japanese scholar Otsu in 1979. It is an adaptive threshold determination method, also called Otsu method, or OTSU for short. It is a global-based binarization algorithm, which is based on the gray scale of the image. Degree characteristics, the image is divided into two parts: foreground and background. When the optimal threshold is taken, the difference between the two parts should be the largest. The standard used in the OTSU algorithm to measure the difference is the more common maximum between-class variance. If the inter-class variance between the foreground and the background is larger, it means that the difference between the two parts of the image is greater. When part of the target is mistakenly classified as the background or part of the background is mistakenly classified as the target, it will cause the difference between the two parts. Decrease, when the threshold segmentation makes the variance between classes the largest, it means that the probability of misclassification is the smallest.

OSTU principle

Otsu algorithm, we can make an exhaustive search of minimum variance of the class threshold, defined as the weighted variance and two classes:
Insert picture description here
the weight is the probability threshold t two separate classes, and is the variance of these two classes of Otsu proved that minimizing the within-class variance and maximizing the between-class variance are the same:
Insert picture description here

It is represented by class probability and class mean .

The class probability is calculated using a histogram with a threshold of t:
Insert picture description here
and the class mean is:
Insert picture description here

The class probability and class mean can be calculated iteratively, and Otsu's algorithm has obtained a threshold in the range of 0:1. This threshold is used for the dynamic range of pixel intensities that appear in the image. For example, if the image contains only pixel intensities between 155 and 255, the Otsu threshold of 0.75 will be mapped to the gray threshold of 230 (instead of 192, because the image contains pixels not in the full range of 0-255).

For image I(x,y), the segmentation threshold of foreground (target) and background is denoted as T, and the proportion of pixels belonging to the foreground to the entire image is denoted as ω0, and its average gray level is μ0; the number of background pixels occupies the entire frame The ratio of the image is ω1, and its average gray scale is μ1. The total average gray level of the image is denoted as μ, and the inter-class variance is denoted as g.

Assuming that the background of the image is dark and the size of the image is M×N, the number of pixels in the image whose gray value is less than the threshold T is recorded as N0, and the number of pixels whose grayscale is greater than the threshold T is recorded as N1, then there is :

(1) ω0 = N0 / (M × N)

(2) ω1 = N1 / (M × N)

(3) N0 + N1 = M×N

(4) ω0 + ω1 = 1

(5) μ = ω0 * μ0 + ω1 * μ1

(6) g = ω0 * (μ0 - μ) 2 + ω1 * (μ1 - μ) 2

Substituting formula (5) into formula (6), the equivalent formula is obtained:

(7) g = ω0 * ω1 * (μ0 - μ1) 2

The traversal method is used to obtain the threshold T that maximizes the variance g between classes.

Implementation of OTSU algorithm

OTSU is used for bimodal images, and the grayscale histogram of the image that is usually applicable for fixed threshold is unimodal:
Insert picture description here

And if you encounter an image of this histogram, then extremely important information will be filtered out:
Insert picture description here

OTSU is suitable for this kind of bimodal picture. It can find the optimal threshold by itself. In order to show the effect of the test, we found a picture of bimodal:
Insert picture description here

First use the fixed threshold method to experiment:

	view plaincopy to clipboardprint?
import cv2 as cv  
import matplotlib.pyplot as plt  
  
img = cv.imread('sh.jpg',0)  
ret,threshold = cv.threshold(img,60,127,cv.THRESH_BINARY)  
cv.imshow("res",threshold)  
cv.waitKey(0)  
image.png

Insert picture description here
Let's experiment with the OTSU method:

	view plaincopy to clipboardprint?
import cv2 as cv  
import matplotlib.pyplot as plt  
  
img = cv.imread('sh.jpg',0)  
ret,threshold = cv.threshold(img,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)  
cv.imshow("res",threshold)  
cv.waitKey(0)  

Regarding the code, it should be noted that when we use OTSU, we don’t need to set the threshold. We only need to set a minimum value of 0 and a maximum value of 255. The algorithm will automatically find the optimal threshold for calculation:
Insert picture description here
you can see that the effect is still relatively good. Well, to sum up, the advantages and disadvantages of OTSU:

Advantages: The algorithm is simple, and the image can be effectively segmented when the area of ​​the target and the background is not much different.

Disadvantages: When the area of ​​the target and the background in the image is very different, it appears that the histogram has no obvious double peaks, or the size of the two peaks are very different, the segmentation effect is not good, or the grayscale of the target and the background is relatively different When there is a large overlap, the target and the background cannot be accurately separated.

Reason: This method ignores the spatial information of the image, and at the same time uses the gray distribution of the image as the basis for segmenting the image, and is also quite sensitive to noise.

Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/108233646