Image Processing and Computer vision in java (Point Operators part 1)

Image point operators are the simplest kind of image transform in which each output pixel’s value depends on only the corresponding input pixel value. Converting color, changing image brightness, gamma correction, thresholding etc. are the example of image point operation.

We are going to apply these transform to the image given below:

Capture

Convert RGB to Grey: As we are performing image processing in greyscale images then we need to convert a color image into a greyscale image. There are many ways to do this but I am going to describe two of them one is averaging and second one is luminosity.

  1. Averaging: in this method we average the rgb value of each pixel and set those values to corresponding pixels.

GreyScale = (R+G+B)/3

To get a clear view you can look at the code snippet below


for(int i=0; i<image.getWidth(); i++){

    for(int j=0; j<image.getHeight(); j++){

       int pixel[] = image.getRaster().getPixel(i, j, new int[3]);
       //get the average of RGB value
       int k = (pixel[0]+pixel[1]+pixel[2])/3;
       //set the average to the pixel
       Color color = new Color(k, k, k);

       int rgb = color.getRGB();

       image.setRGB(i, j, rgb);

    }
}

output Image:

grey

Though it is very easy to implement but most of the time output image is not as good as we expect. To give greyscale image a natural look we can use luminosity.

  1. Luminosity or Weighted method: The philosophy behind luminosity or weighted method is the contribution of color to form an image. We all know that red color has more wavelength of all the three of colors. For that red color dominates in image color composition. On the other hand green color not only has less wavelength but it also very soothing for our eyes. So to make the grey image more natural we can reduce the contribution of red color and increase the contribution of green color. We put the blue color in between them. So the equation will be:

GreyScale = ( (0.3 * R) + (0.59 * G) + (0.11 * B) )

Here in this equation red has contributed 33% ,green 59% and blue has contributed 11%

To implement this in java this in java we can do something like this:


for(int i=0; i<img.getWidth(); i++){

    for(int j=0; j<img.getHeight(); j++){

        int pixel[] = img.getRaster().getPixel(i, j, new int[3]);
        //get the new value
        int k =(int)((0.3*pixel[0])+(0.59*pixel[1])+(0.11*pixel[2]));

        Color color = new Color(k, k, k);

        int rgb = color.getRGB();

       img.setRGB(i, j, rgb);

    }

}

output Image:
Capturelum

Gamma Transform: it is also known as power-law transform. This transform used in CRT monitors to correct the image intensity. It is mathematically denoted as follows:

gamma eq

where c and ɣ are two constants. This transform can make image brighter or darker depending on the value of ɣ. If ɣ is lager it will make the image darker and if ɣ is smaller than the image will brighter. To implement this in java with the ɣ value 0.45 we can use the code given below:


for(int i=0; i<img.getWidth(); i++){

    for(int j=0; j<img.getHeight(); j++){

       int pixel[] = img.getRaster().getPixel(i, j, new int[3]);
       //get the gamma corrected value
       int k = (int) (255*(Math.pow((double)pixel[0]/(double)255, 2.2)));

       Color color = new Color(k, k, k);

       int rgb = color.getRGB();

       img.setRGB(i, j, rgb);

    }

}

We can take a look at the picture below to get a clear idea

conv
Output Image:
gamma
Photographic Negative: negative is just the inverse of a picture. To create a negative of a binary image we can transform the black points(0s) into white(1s) and white points(1s) into black(0s). for that the equation will be:

binary neg

For greyscale image the transform can be accomplish by

negateeq1

And the java implementation is


for(int i=0; i<img.getWidth(); i++){

    for(int j=0; j<img.getHeight(); j++){

       int pixel[] = img.getRaster().getPixel(i, j, new int[3]);
       //get the new value
       k = 255 - pixel[0];

       Color color = new Color(k, k, k);

       int rgb = color.getRGB();

       img.setRGB(i, j, rgb);

   }

}

output Image:
negat

Brightness: it can be achieve by adding or deleting constant value from image pixel. If you add value image will be bright and if we delete image will dark. The equation for that is:

P’ = P+C or P’ = P-C here “P” is the old value and “P’ ” is the new pixel value

The java code is:

for(int i=0; i<img.getWidth(); i++){

    for(int j=0; j<img.getHeight(); j++){

        pixel = img.getRaster().getPixel(i, j, new int[3]);
        //adding constant we can also subtract constant from here
        int k = pixel[0]+50;
        //check if the value out side of the boundary
        if(k>255)

            k = 255;

        else if(k<0)

            k = 0;

        Color color = new Color(k, k,k);

        int rgb = color.getRGB();

        img.setRGB(i, j, rgb);

    }

}

Here we have to check the value limit. The cannot be higher than 255 or less than 0.

Output Image:

bright

Thresholding: transforming a grey image into binary image is thresholding. There are many ways to do that but I am going to show you one. In this method we are going to calculate the mean of  the pixel values of an image and compare the mean with each pixel value. If the value is bigger than mean we will set the pixel value to 255 and if the pixel value is less than mean we will put 0 in it. The method will be more clearer if you take a look at the code snippet below:


public void threshold(BufferedImage img){

    int pixel[];

    int k = 0;

    Color color;

    int rgb;
    //calculating the number of pixel
    int mean = img.getWidth()* img.getHeight();

    for(int i=0; i<img.getWidth(); i++){

        for(int j=0; j<img.getHeight(); j++){

            pixel = img.getRaster().getPixel(i, j, new int[3]);
            //adding the value of pixel
            k += pixel[0];

        }

    }
    //calculating the mean
    mean = k/mean;

    for(int i=0; i<img.getWidth(); i++){

        for(int j=0; j<img.getHeight(); j++){

            pixel = img.getRaster().getPixel(i, j, new int[3]);
            //check the pixel value is greater or less then mean
            if(pixel[0]>mean)
                //if value is bigger than set 255
                k = 255;

            else
            //else set the value 0
                k = 0;

            color = new Color(k, k, k);

            rgb = color.getRGB();

            img.setRGB(i, j, rgb);

        }

    }

}

output Image:
threshold
On our next tutorial we are going to discuss Histogram and histogram equalization

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s