Brief introduction:
The Dajing method (OTSU) is an algorithm for determining the threshold value of image binary segmentation, which was proposed by Japanese scholar Otsu in 1979. According to the principle of Dajing method, this method is also called the Maximal inter-class variance method, because the difference between the foreground and the background image is the largest (what is the inter-class variance) After the binary segmentation of the image based on the threshold obtained by the Dajing method? The principle is introduced).
Otsu algorithm
Otsu algorithm, also known as the maximum inter-class difference method, sometimes referred to as the Dajing algorithm, proposed by Dajing in 1979, is considered to be the best algorithm for threshold selection in image segmentation, which is simple in calculation and unaffected by the brightness and contrast of images, so it has been widely used in digital image processing. It is based on the grayscale characteristics of the image, the image into the background and foreground two parts. Because variance is a measure of uniformity of gray scale distribution, the greater the variance between the background and the foreground, the greater the difference between the two parts of the image, the difference between the partial foreground and the background is divided into the foreground, which causes the two parts to be smaller. Therefore, the division of the maximum variance between classes means that the probability of error is minimized.
Principle:
For the image I (x, y), the foreground (that is, the target) and the background of the segmentation threshold is recorded as T, the percentage of pixels that belong to the foreground is recorded as ω0, its average grayscale μ0, and the proportion of the background pixel to the whole image is ω1, and its average grayscale is μ1. The total average grayscale of the image is recorded as μ, and the inter-class variance is recorded as G.
If the background of the image is darker, and the size of the image is MXN, the number of pixels in the image that is less than the threshold T is recorded as N0, and the pixel grayscale is greater than the threshold T number of pixels as N1, there are:
Ω0=N0/MXN (1)
Ω1=N1/MXN (2)
N0+N1=MXN (3)
Ω0+ω1=1 (4)
Μ=ω0*μ0+ω1*μ1 (5)
G=ω0 (μ0-μ) ^2+ω1 (μ1-μ) ^2 (6)
Substituting formula (5) into the equation (6) to obtain an equivalent
G=ω0ω1 (μ0-μ1) ^2 (7) This is the Inter-class variance
By using the traversal method, the threshold value T, which is the maximum variance between the classes, is obtained.
MATLAB functions:
In Matlab, the function Graythresh is not only used Dajing method to obtain the segmentation threshold T. Use the following:
T = Graythresh (IMG);
BW = IM2BW (img,t);
The image understanding of Dajing law:
For images with two peaks in the histogram, the T approximation obtained by the Dajing method equals the trough between two peaks.
Imhist (IMG);
T = Graythresh (IMG);
such as the histogram of the image, so that the Dajing method to obtain the t=0.5294, the conversion between [0,255] is 134.9970, had to be two peaks between the trough position.
In the OpenCV two value operation, there is a "Dajing threshold processing" method, using the function Cvthreshold (Image,image2,0,255,cv_thresh_otsu) Implementation, The function uses the global adaptive threshold obtained by the large law Otsu to perform the binary picture, and the threshold in the parameter no longer works.
The following is a C language program implemented in OpenCV, a function that extracts an image threshold using the Otsu algorithm, the input parameter is an image pointer, and the optimal threshold for splitting the image is returned.
Where the variable description: When the threshold value of the split is T
W0 the proportions of the entire image for the background pixel point
U0 for W0 average grayscale
W1 The ratio of the total image to the foreground pixel point
U1 for W1 Average grayscale
U is the average grayscale of the whole image
Formula: G = W0*pow ((u-u0), 2) + W1*pow ((U-U1), 2)
intMyautofocusdll::otsuthreshold (Iplimage *frame) { Const intGrayscale = the; intwidth = frame->width; intHeight = frame->height; intPixelcount[grayscale]; floatPixelpro[grayscale]; intI, J, pixelsum = width * height, threshold =0; Uchar* data = (uchar*) frame->imagedata;//pointer to pixel data for(i =0; I < grayscale; i++) {Pixelcount[i]=0; Pixelpro[i]=0; } //Count the number of pixels in the entire image in the gray level for(i =0; i < height; i++) { for(j =0; J < width; J + +) {pixelcount[(int) data[i * width + j]]++;//Subscript the pixel value as a count array } } //calculate the proportions of each pixel in the entire image floatMaxPro =0.0; intKK =0; for(i =0; I < grayscale; i++) {Pixelpro[i]= (float) Pixelcount[i]/pixelsum; if(Pixelpro[i] >MaxPro) {MaxPro=Pixelpro[i]; KK=i; } } //traverse Gray level [0,255] floatW0, W1, u0tmp, U1tmp, u0, U1, U, deltatmp, Deltamax =0; for(i =0; I < grayscale; i++)//I as threshold value{w0= W1 = U0tmp = U1tmp = u0 = u1 = U = deltatmp =0; for(j =0; J < Grayscale; J + +) { if(J <= i)//Background Section{w0+=Pixelpro[j]; U0tmp+ = J *Pixelpro[j]; } Else //Foreground part{W1+=Pixelpro[j]; U1tmp+ = J *Pixelpro[j]; }} u0= U0TMP/W0; U1= U1TMP/W1; U= U0tmp +u1tmp; Deltatmp= W0 * POW ((u0-u),2) + W1 * POW ((u1-u),2); if(Deltatmp >Deltamax) {Deltamax=deltatmp; Threshold=i; } } returnthreshold;}
Dajing Method---Otsu algorithm