Image processing Integration Graph application Four (image binary algorithm based on local mean value)

Source: Internet
Author: User

Image processing Integration Graph application Four (image binary algorithm based on local mean value)

Basic principle
Mean, the selected threshold is the grayscale mean of the pixels in the local range (gray mean), a variant of this method is to subtract the mean value mean with a constant C, and then do the following according to the mean value:
Pixel = (Pixel > (mean-c))? Object:background
By default, the parameter C is evaluated as 0. object represents the foreground pixel, and background represents the background pixel.

Implementation steps
1. Color image to grayscale image
2. Get the pixel data of grayscale image, calculate the integral graph
3. Get the sum of the pixels from the integration graph according to the parameter window radius of the input, and obtain the average
4. Loop each pixel to achieve a two value assignment of the center pixel based on local mean value
5. Input binary image

Operation Result:

Code implementation:

 PackageCom.gloomyfish.ii.demo;ImportJava.awt.image.BufferedImage; Public  class fastmeanbinaryfilter extends abstractimageoptionfilter {    Private intconstantPrivate intRadius Public Fastmeanbinaryfilter() {constant =Ten; Radius =7;//1,2,3,4,5,6,7,8} Public int getconstant() {returnconstant } Public void setconstant(intConstant) { This. constant = constant; } Public int Getradius() {returnRadius } Public void Setradius(intRADIUS) { This. radius = radius; }@Override     PublicBufferedImageProcess(BufferedImage image) {intwidth = Image.getwidth ();intHeight = image.getheight (); BufferedImage dest = createcompatibledestimage (image,NULL);//Grayscale of images        int[] Inpixels =New int[Width*height];int[] Outpixels =New int[Width*height];byte[] Bindata =New byte[Width*height]; getRGB (Image,0,0, width, height, inpixels);intindex =0; for(introw=0; rowintTA =0, tr =0, TG =0, TB =0; for(intCol=0; col<width;                col++) {index = row * width + col; Ta = (Inpixels[index] >> -) &0xFF; TR = (Inpixels[index] >> -) &0xFF; TG = (Inpixels[index] >>8) &0xFF; TB = Inpixels[index] &0xFF;intGray= (int)(0.299*TR +0.587*TG +0.114*TB); Bindata[index] = (byte) Gray; }        }//per-calculate integral imageIntintegralimage Grayii =NewIntintegralimage ();        Grayii.setimage (Bindata); Grayii.process (width, height);intyr = radius;intXR = radius;intSize = (YR *2+1) * (XR *2+1); for(introw =0; Row < height; row++) { for(intCol =0; Col < width; col++) {index = row * width + col;//Calculate mean value                intsr = Grayii.getblocksum (col, Row, (YR *2+1), (XR *2+1));intmean = Sr/size;intPixel = bindata[index]&0xFF;//Two value                if(Pixel > (mean-constant)) {Outpixels[row * width + col] = (0xFF<< -) | (0xFF<< -) | (0xFF<<8) |0xFF; }Else{Outpixels[row * width + col] = (0xFF<< -) | (0x00<< -) | (0x00<<8) |0x00; }            }        }//Return resultsSetrgb (dest,0,0, width, height, outpixels);returnDest }}

2017 has begun! Blog every month will have image processing related technical articles updated, welcome to continue to pay attention!

Image processing Integration Graph application Four (image binary algorithm based on local mean value)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.