Turn from: http://www.cnblogs.com/Imageshop/p/3302850.html
Body:
This is a very old paper in the algorithm, published in 1994, is Tsinghua University Huang Liangkei (Liang-kai Huang) wrote, so some foreign papers and code called Huang ' s fuzzy thresholding method. Although ancient is also very simple, but the principle of its algorithm is still worth learning.
The original text of the paper can be downloaded from here: Image thresholding by minimizing the measure of fuzziness.
This paper combines the fuzzy set theory which is in the research upsurge at that time, puts forward a kind of image binary algorithm with good effect, this article mainly is to carry on the simple translation and the annotation, and provides the test code.
A fuzzy set and its membership function
First, we assume that X represents an L-level grayscale image with a size of MXN, the xmn represents the pixel grayscale value at the x midpoint (m,n) of the image, and the definition of μx (XMN) indicates that the point has a membership value of some attribute, that is, we define a fuzzy subset that maps from the image x to the [0,1] interval , expressed in a professional fuzzy set, that is:
of which 0≤μx (XMN) ≤1,m=0,1,... m-1,n=0,1,... N-1. For a binary, each pixel should have a close relationship to the category it belongs to (foreground or background), so we can represent the value of μx (XMN) in this relationship.
The definition of H (g) indicates the number of pixels with grayscale g in the image, and for a given threshold T, the average μ0 and μ1 of the respective levels of the background and foreground values can be expressed as follows:
The above μ0 and μ1 can be considered as the target values for the foreground and background of the specified threshold T, and the relationship between a point in image X and the region described in it should be intuitively related to the difference between the point's level value and the target value of the zone in which it belongs. Therefore, for the point (m,n), we propose the following membership definition function:
where c is a constant that makes 0.5≤μx (XMN) ≤1. Therefore, for a given threshold T, any pixel in the image is either a background or a foreground, so the membership of each pixel should not be less than 0.5.
C value in the actual programming, you can use the maximum gray value of the image minus the minimum gray value to express, that is, c=gmax-gmin;
The measurement of ambiguity degree and the principle of threshold value
The fuzziness indicates the degree of ambiguity of a fuzzy set, and several measures have been mentioned, in this paper only the Shannon entropy function is used to measure the ambiguity degree.
Based on Shannon entropy function, the entropy of a fuzzy set A is defined as:
Among them Shannon function:
Extended to 2-D images, the entropy of the image X can be expressed as:
Because grayscale images are at most only l levels, the histogram (7) can be further written:
It can be proved that the formula (6) is monotonically increasing between the interval [0,0.5] and is monotonically decreasing between [0.5,1], and E (X) has the following properties:
(1) 0≤e (X) ≤1;
(2) if Μx (xmn) =0 or μx (xmn) =1, E (X) has a minimum value of 0, in this article μx (XMN) can only be 1, at which point the classification has the best clarity.
(3) when μx (XMN) =0.5,e (X) obtains the maximum value of 1, the classification at this time is the most ambiguous.
So for image X, the principle of the best threshold T is: for all possible thresholds T, the T with the lowest Shannon entropy is the final threshold for segmentation.
The skills in programming
With the above principle, programming is also a very easy thing, you can follow your ideas to do, but the author of the paper will make the code more clear, more effective.
First, for the sake of ease of expression, we define some of the following expressions:
According to the expression above, it is possible to know that s (L-1) and W (L-1) are constants for an image, where S (L-1) is obviously the total number of pixels.
Our algorithm steps are as follows:
(1), compute S (L-1), W (L-1), set the initial threshold value t=gmin, make S (t-1) =0, W (t-1) = 0;
(2), calculate the following formula:
People with a little bit of math should be able to understand the derivation of the above formula.
According to the formula (2) and (3), the average gray value of the area where the background and foreground can be known is:
The int in the upper-type represents the rounding operation.
(3) Calculating the fuzzy degree of the image according to the formula (4) and the formula (11);
(4) Make t=t+1, and then repeat step 2 until t=gmax-1;
(5) Find the threshold t corresponding to the minimum ambiguity value in the whole process, and act as the best segmentation threshold.
To get a little faster, the calculations in the above 4 can be implemented in step 1 with a lookup table.
Four, the reference code:
public static int Gethuangfuzzythreshold (int[] histgram) {int X, Y;
INT-A, last;
int Threshold =-1;
Double bestentropy = Double.maxvalue, Entropy; Find the first and last color level value of non 0 for (A/0; < Histgram.length && Histgram[first] = = 0;
first++); for (last = histgram.length-1; Last > I && histgram[last] = = 0;
last--); if (i = last) return to the last; There is only one color in the image if (i + 1 = last).
The image has only two colors//compute the cumulative histogram and the corresponding cumulative histogram with weights int[] S = new Int[last + 1]; int[] W = new Int[last + 1];
For oversized graphs, the saved data for this array may exceed the representation range of int, consider a long type instead of s[0] = histgram[0]; for (Y = > 1? First:1; Y <= last;
y++) {S[y] = S[y-1] + histgram[y];
W[y] = w[y-1] + Y * Histgram[y];
Create a formula (4) and (6) the lookup table used double[] Smu = new Double[last + 1-first]; for (Y = 1; Y < Smu.length; y++) {Double mu = 1/(1 + (double) Y/(Last-first)); Formula (4) smu[y] =-mu * Math.Log (MU)-(1-MU) * MATH.LOG (1-MU); The formula (6)}///iteration calculates the optimal threshold for (Y = i; Y <= last;
y++) {Entropy = 0; int mu = (int) math.round ((double) w[y]/s[y]); Formula for (X = i; X <= Y;
x + +) Entropy + + = Smu[math.abs (X-MU)] * histgram[x]; MU = (int) math.round (double) (W[last]-w[y])/(S[last]-s[y)); Formula for (X = Y + 1; X <= last; x + +) Entropy + + = Smu[math.abs (X-MU)] * histgram[x]; Formula 8 if (Bestentropy > Entropy) {bestentropy = Entropy;
Taking the minimum entropy as the best threshold Threshold = Y;
} return Threshold; }
The code is actually very simple.
Five, the effect:
For some images, we did the following tests:
Original two-value graph, threshold value =175
The above image can not obtain the ideal effect of the above image using classical algorithms such as Ostu.
Original two-value graph, threshold value =67
The results of the above and some other binary algorithms are very similar.
Basically I do not provide the source code, but I will try to use text to describe the corresponding algorithm clearly or provide reference documentation *********************
Because rely on their own efforts and practice to write out the effect is really their own things, people must rely on their own ****************************
********************************* Author: laviewpbt time: 2013.9.5 contact QQ: 33184777 Reprint Please keep the bank information ************************