GrayScale Conversion Tutorial

Advertisements


In order to convert an colour image to GrayScale image, first thing you need to do is to read pixels or data of the image Using File and ImageIO object and store it an BufferedImage object. Its syntax is given below:

File input = new File("digital_image_processing.jpg");
BufferedImage image = ImageIO.read(input);	

After that , get the value of pixel frm getRGB method to grab a pixel value and perform GrayScale method on it. The method getRGB() takes row and column index as parameter

Color c = new Color(image.getRGB(j, i));
int red = (c.getRed() * 0.299);
int green =(c.getGreen() * 0.587);
int blue = (c.getBlue() *0.114);

Apart from these three methods, there are other methods availaible in the Color class which are listed below.

Sr.NoMethod & Description
1brighter()
Creates a new Color that is a brighter version of this Color
2darker()
Creates a new Color that is a darker version of this Color
3getAlpha()
Returns the alpha component in the range 0-255
4getHSBColor(float h, float s, float b)
Creates a Color object based on the specified values for the HSB color model
5HSBtoRGB(float hue, float saturation, float brightness)
Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model
6toString()
Returns a string representation of this Color

The last step is to add all these three values and set it again to the corresponding pixel value. Its syntax is given below:

int sum = red+green+blue;
Color newColor = new Color(sum,sum,sum);
image.setRGB(j,i,newColor.getRGB());

Example

The following example demonstrates the use of java BufferedImage class that to convert an image to GrayScale.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GrayScale {
   BufferedImage  image;
   int width;
   int height;
   public GrayScale() {
      try {
         File input = new File("digital_image_processing.jpg");
         image = ImageIO.read(input);
         width = image.getWidth();
         height = image.getHeight();
         for(int i=0; i<height; i++){
            for(int j=0; j<width; j++){
               Color c = new Color(image.getRGB(j, i));
               int red = (int)(c.getRed() * 0.299);
               int green = (int)(c.getGreen() * 0.587);
               int blue = (int)(c.getBlue() *0.114);
               Color newColor = new Color(red+green+blue,
               red+green+blue,red+green+blue);
               image.setRGB(j,i,newColor.getRGB());
               }
          }
         File ouptut = new File("grayscale.jpg");
         ImageIO.write(image, "jpg", ouptut);
      } catch (Exception e) {}
   }
   static public void main(String args[]) throws Exception 
   {
      GrayScale obj = new GrayScale();
   }
}

Output

When you run the above example, it would convert an image name digital_image_processing.jpg to its equivalent GrayScale image and write it on hard disk with name grayscale.jpg.

Original Image

Grayscale Conversion Tutorial

GrayScale Image

Java Image Processing Tutorial

Advertisements
Advertisements