This article mainly focuses on the binarization of images, that is, threshold segmentation based on grayscale. There are many methods of binarization. In fact, if the target area can be perfectly segmented during image processing, then more than half of the success is achieved, which shows the importance of binarization. This article mainly introduces three binarization methods.
1. Fixed threshold method, set the threshold, and set the pixels greater than the threshold to 255, otherwise it is 0. This is the simplest, and the effect is the most unstable.
<span style="font-size:14px;"><span style="font-size:10px;">public void SimBinary(){ toGray();//灰度化 int Threshold = 128; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if(data[x + y * w] < Threshold){ data[x + y * w] = 0; }else{ data[x + y * w] = 255; } } } }</span></span>
The operation effect is as follows:
2. Loop method (reference: Common binarization methods of image processing Summary)
1. An initialization threshold T, which can be set by yourself or generated based on a random method.
2. According to the threshold map, each pixel data P(n,m) is divided into object pixel data G1 and background pixel data G2. (n is the
row, m is the column)
3. The average value of G1 is m1, and the average value of G2 is m2
4. A new threshold T' = (m1 + m2)/2
5. Return to the second step, use the new threshold to continue dividing the pixel data into objects and Beijing pixel data, continue steps 2 to 4,
until The calculated new threshold is equal to the previous threshold.
The code is as follows:
<span style="font-size:14px;"> public void IterBinary(){ toGray(); int Threshold = 128; int preThreshold = 256; while (Math.abs(Threshold-preThreshold) > 4){ int s1 = 0; int s2 = 0; int f1 = 0; int f2 = 0; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if(data[x + y * w] < Threshold){ s1 += data[x + y * w]; f1++; }else{ s2 += data[x + y * w]; f2++; } } } preThreshold = Threshold; Threshold = (int)((s1/f1+s2/f2)/2); } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if(data[x + y * w] < Threshold){ data[x + y * w] = 0; }else{ data[x + y * w] = 255; } } } }</span>
The effect is as follows:
3. Otsu Threshold Method (Reference: Adaptive Threshold Algorithm (Otsu Threshold Method ))
The maximum inter-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 divides the image into two parts: background and target according to the grayscale characteristics of the image. The greater the inter-class variance between the background and the target, the greater the difference between the two parts that make up the image. When part of the target is mistakenly classified as background or part of the background is mistakenly classified as target, the difference between the two parts will become smaller. Therefore, the segmentation that maximizes the variance between classes means the smallest probability of misclassification. For image I (x, y), the segmentation threshold of the foreground (i.e. target) and background is denoted as T, the proportion of the number of pixels belonging to the foreground to the entire image is denoted as ω0, and its average gray level μ0; the number of background pixels to the entire image is denoted The scale of the image is ω1, and its average gray level is μ1. The overall average
gray level of the image is recorded as μ, and the inter-class variance is recorded as g. Assume 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 gray value is greater than the threshold T is recorded as N1, then there is :
ω0=N0/ M×N (1)
ω1=N1/ M×N (2)
N0+N1=M×N (3)
ω0+ω1=1 (4)
μ=ω0*μ0+ω1*μ1 (5)
g=ω0(μ0-μ)^2+ω1( μ1-μ)^2 (6)
Substituting equation (5) into equation (6), the equivalent formula is obtained:
g=ω0ω1(μ0-μ1)^2 (7)
Use the traversal method to obtain the threshold T that maximizes the variance between classes, which is what you want.
The code is as follows:
<span style="font-size:14px;">public void Otsu(){ toGray(); int num = h*w; int[] hist = hist(); int sum = math.sum(hist); double[] res = new double[256]; double m1=0,m2=0,w1=0,w2=0; for(int k=0;k<256;k++){ for(int i=0;i<k;i++){ m1 +=hist[i]; } w1 = m1/num; w2 = 1-w1; m2 = (sum-m1)/(255-k); m1 = m1/(k+1); res[k] = w1*w2*Math.abs(m1 - m2)*Math.abs(m1 - m2); } int Threshold = math.maxIndex(res); //获得最大值的下标 for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if(data[x + y * w] < Threshold){ data[x + y * w] = 0; }else{ data[x + y * w] = 255; } } } }</span>
The running effect is as follows:
The above is the content of the binarization of java images and the Otsu threshold method. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!