The basic concept of binary: popular is to treat a pair of color image processing into a black and white image, which is generally used as a preprocessing for subsequent complex image processing operations.
Binary algorithm idea: traverse all the pixels of the image, calculate the gray value of each pixel point. An optimal threshold value is obtained by iterative convergence, the pixels with gray values greater than the best threshold are set to white, and the pixels with grayscale values smaller than the optimal threshold are set to black. (The result of my two value processing here is that the background is white and the foreground is black)
Iterative method to get the best threshold idea:
1. Set the minimum gray value to Gmin, the maximum gray value is Gmax, the threshold value is initialized to t (0) = (Gmin+gmax)/2.
2. Divide the image into foreground and background by threshold T (k) to find the average gray value of the entire foreground pixel Gf and the average grayscale of the entire background pixel Gb, when threshold t (k) = (GF+GB)/2 (k=0,1,2 ...);
3. If T (k) =t (k+1) at this time, then convergence, get the best threshold value. Otherwise, go back to step 2 until the threshold converges to a certain value.
1 Public classBinary {2 Public Static int[] Getbinaryimg (intWintHint[] inputs) {3 int[] Gray =New int[w *h];4 int[] Newpixel =New int[w *h];5 for(intindex = 0; Index < W * H; index++) {6 intRed = (Inputs[index] & 0x00ff0000) >> 16;7 intGreen = (Inputs[index] & 0x0000FF00) >> 8;8 intBlue = Inputs[index] & 0x000000ff;9Gray[index] = (int) ((float) Red * 0.3 + (float) Green * 0.59 + (float) Blue * 0.11);Ten } One //Find the maximum gray value Zmax and the minimum gray value Zmin A intGmax = Gray[0], Gmin = gray[0]; - for(intindex = 0; Index < W * H; index++) { - if(Gray[index] >Gmax) { theGmax =Gray[index]; - } - if(Gray[index] <Gmin) { -Gmin =Gray[index]; + } - } + A //Get grayscale histogram at intI, J, t, Count1 = 0, Count2 = 0, sum1 = 0, sum2 = 0; - intBP, FP; - int[] histogram =New int[256]; - for(t = Gmin; t <= Gmax; t++) { - for(intindex = 0; Index < W * H; index++) { - if(Gray[index] = =t) inhistogram[t]++; - } to } + - /* the * Iterative method to find the best segmentation threshold value * * */ $ intT = 0;Panax Notoginseng intNewT = (Gmax + Gmin)/2;//Initial threshold Value - while(T! =NewT) the //Find the average gray value of background and foreground BP and FP + { A for(i = 0; i < T; i++) { theCount1 + = Histogram[i];//total number of background pixel points +Sum1 + = histogram[i] * i;//Gray value of the background pixel point - } $bp = (Count1 = = 0)? 0: (SUM1/COUNT1);//average gray value of background pixel points $ - for(j = i; J < Histogram.length; J + +) { -Count2 + = Histogram[j];//total number of foreground pixel points theSum2 + = histogram[j] * j;//gray value of foreground pixel points - }WuyiFP = (Count2 = = 0)? 0: (Sum2/count2);//average gray value of foreground pixel points theT =NewT; -NewT = (bp + fp)/2; Wu } - intFinestyzt = NewT;//Optimal threshold value About $ //two value of - for(intindex = 0; Index < W * H; index++) { - if(Gray[index] >finestyzt) -Newpixel[index] =Color.White; A ElseNewpixel[index] =Color.Black; + } the returnNewpixel; - } $}
Image binary Processing Java