How to convert a color image into grayscale image in Java

Image Processing Project

In this project we will learn to convert a color image into grayscale image using Java programming language.

Prerequisite

It is assumed that you have completed the projects titled How to read and write image file in Java and How to get and set pixel value in Java before starting this project.

Color image to Grayscale image

Converting a color image into grayscale image is very simple. All we have to do is repeat 3 simple steps for each pixels of the image.

  1. Get the RGB value of the pixel.
  2. Find the average of RGB i.e., Avg = (R+G+B)/3
  3. Replace the R, G and B value of the pixel with average (Avg) calculated in step 2.

Example:

Consider a color pixel with the following values

A = 255

R = 100

G = 150

B = 200

Where A, R, G and B represents the Alpha, Red, Green and Blue value of the pixel.

Remember! ARGB will have an integer value in the range 0 to 255.

So, to convert the color pixel into grayscale pixel we have to first find the average of R, G and B.

Average, Avg = (R+G+B)/3

i.e., Avg = (100+150+200)/3 = 150

Now we will replace the value of R, G and B with the average value that we calculated for the pixel.

So, the new pixel value will be

A = 255

R = 150

G = 150

B = 150

Note! We don't have to change the alpha value because it only controls the transparency of the pixel.

Hence, for a grayscale image the RED, GREEN and BLUE component of a given pixel is same.

Import the required classes

Create a new file and save it by the name Grayscale.java. Open the file and import the following:

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

Create the class

Next we will create the class by the name Grayscale and inside the class we will have the main() method. For this we will write:

public class Grayscale{
public static void main(String args[])throws IOException{

// some code goes here...

}//main() ends here
}//class ends here

Variables

Inside the main() method we create a BufferedImage variable img and a File variable f and set both of them to null. The img variable will hold the image file while the f variable will hold the image file path.

BufferedImage img = null;
File f = null;

Read the image file

For this project we will read the color image of Taj Mahal. The file name is Taj.jpg and it is inside the Image folder which is inside D: drive on a Windows PC. So, we will first create an object of File class and pass as parameter the image file path. And then we will read the image file by calling the read() method of ImageIO class. So, our code will look something like the following:

//read image
try{
f = new File("D:\\Image\\Taj.jpg");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}

Get image dimensions

Now that we have the image stored in the img variable, it's time for us to find the dimensions of the image. For this we will create two integer variable width and height and use the getWidth() andgetHeight() method to get the width and height of the image respectively. So, we will write the following lines.

//get image width and height
int width = img.getWidth();
int height = img.getHeight();

Grayscale code

We know that an image is made up of pixels which we can represent in 2D co-ordinate. So, we will create two variables x and y and use two for loops to traverse each pixels.

For this we will write:

for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){

// some code goes here...

}
}

Get pixel value and extract ARGB value

Inside the for loop we will first get the value of the pixel at co-ordinate (x,y) using thegetRGB(x,y) method. Then we will extract the Alpha, Red, Green and Blue value from the pixel value. For this purpose we will create 5 integer variables p, a, r, g and b to hold the pixel, alpha, red, green and blue value respectively.

int p = img.getRGB(x,y);
int a = (p>>24)&0xff;
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p&0xff;

Find average and set new pixel value

Now we will find the average of R, G and B value. For this we will write:

int avg = (r+g+b)/3;

Note! we are only interested with the integer value hence we have saved the average value in an integer variable avg.

We will now find the new pixel value and set it to the pixel at co-ordinate (x,y). For this we will write:

p = (a<<24) | (avg<<16) | (avg<<8) | avg;
img.setRGB(x, y, p);

Write image

After the for loop ends we will write the image file. For this we will write the following code:

try{
f = new File("D:\\Image\\Output.jpg");
ImageIO.write(img, "jpg", f);
}catch(IOException e){
System.out.println(e);
}

Final code

/**
* File: Grayscale.java
*
* Description:
* Convert color image into grayscale image.
*
* @author Yusuf Shakeel
* Date: 26-01-2014 sun
*
* www.github.com/yusufshakeel/Java-Image-Processing-Project
*/
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Grayscale{
public static void main(String args[])throws IOException{
BufferedImage img = null;
File f = null;

//read image
try{
f = new File("D:\\Image\\Taj.jpg");
img = ImageIO.read(f);
}catch(IOException e){
System.out.println(e);
}

//get image width and height
int width = img.getWidth();
int height = img.getHeight();

//convert to grayscale
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int p = img.getRGB(x,y);

int a = (p>>24)&0xff;
int r = (p>>16)&0xff;
int g = (p>>8)&0xff;
int b = p&0xff;

//calculate average
int avg = (r+g+b)/3;

//replace RGB value with avg
p = (a<<24) | (avg<<16) | (avg<<8) | avg;

img.setRGB(x, y, p);
}
}

//write image
try{
f = new File("D:\\Image\\Output.jpg");
ImageIO.write(img, "jpg", f);
}catch(IOException e){
System.out.println(e);
}
}//main() ends here
}//class ends here