This paper describes how to calculate the gray histogram of image and the binary processing of image in the image sampling and quantization and straight square diagram. In this article is described in the two value of the treatment of the valve value are set themselves, set their own threshold is often inaccurate, and different images of the best valve value is not the same. So can the computer to calculate the image of the best valve value it. The answer is yes, here is an iterative method to calculate the value of the image: algorithm thought
Iterative method is based on the idea of approximation, the steps are as follows:
1. The maximum gray value and the minimum gray value of the image are calculated, respectively, as Pmax and Pmin, so that the initial threshold t0= (pmax+pmin)/2;
2. According to the Threshold T (k) (k=0,1,2...,k), the image is segmented into foreground and background, and the mean gray value H1 and H2 are obtained respectively.
3. The new threshold value T (k+1) = (H1+H2)/2 is calculated.
4. If T (k) =t (k+1), then the gain is the threshold value, otherwise it will be 2, iterative calculation.
source Code (Java)
/** * Using iterative method to find the best valve value * @param pix Grayscale Pixel Group * @return best valve/public int iterationgetthreshold (int[] pix) {int min =
Pix[0], max = pix[0];
for (int i=0; i<pix.length; i++) {if (pix[i) > 255) {pix[i] = 255;
} if (Pix[i] < 0) {Pix[i] = 0;
} if (min >pix[i]) min = pix[i];
if (Max <pix[i]) max = Pix[i];
Double histo[] = Gethisto (pix);
int threshold = 0;
int newthreshold = (int) ((Min+max)/2);;
while (threshold!= newthreshold) {Double sum1=0, sum2=0, w1=0, w2=0;
int AVG1, AVG2;
for (int i=min; i<newthreshold; i++) {sum1 = = Histo[i]*i;
W1 + = Histo[i];
} AVG1 = (int) (SUM1/W1);
for (int i=newthreshold; i<max; i++) {sum2 = = Histo[i]*i;
W2 + = Histo[i];
} AVG2 = (int) (SUM2/W2);
System.out.println ("AVG1:" + avg1 + "AVG2:" + avg2 + "Newthreshold:" + newthreshold);
threshold = Newthreshold;
Newthreshold = (AVG1+AVG2)/2;
return newthreshold; /*if (min==0 &&Amp
max = = 255) {return (Min+max)/2;
else {int t = (Min+max)/2;
Double sum1=0, sum2=0, w1=0, w2=0;
int AVG1, AVG2;
for (int i=min; i<t; i++) {sum1 = = Histo[i]*i;
W1 + = Histo[i];
} AVG1 = (int) (SUM1/W1);
for (int i=t; i<max; i++) {sum2 = = Histo[i]*i;
W2 + = Histo[i];
} AVG2 = (int) (SUM2/W2);
Return (AVG1+AVG2)/2; */}/** * Image grayscale histogram * @param pix one-dimensional grayscale image pixel value * @return 0-255 of the pixel value of the ratio * * Public double[] Gethisto (int pix[])
{double histo[] = new double[256];
for (int i=0; i<pix.length; i++) {//system.out.println ("pix[i]:" + pix[i]);
if (Pix[i] > 255) {pix[i] = 255;
} if (Pix[i] < 0) {Pix[i] = 0;
} Histo[pix[i]] + +;
for (int i=0; i<255; i++) {histo[i] = (double) histo[i]/pix.length;
return histo;
/** * Binary image * @param pix pixel Matrix array * @param w Matrix width * @param h Matrix High * @param threshold threshold * @return processed array * * Public int[] Threshold (int pix[], int tHreshold) {for (int i=0; i<pix.length; i++) {if (Pix[i] <= threshold) {Pix[i] = 0;
else {pix[i] = 255;
} return pix; }