Java | Converting an Image into Grayscale using cvtColor()

To convert a color image to Grayscale image using OpenCV, we read the image into BufferedImage and convert it into Mat Object.

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

To transform the image from RGB to Grayscale format by using method cvtColor() in the Imgproc class.

Imgproc.cvtColor(source mat, destination mat1, Imgproc.COLOR_RGB2GRAY);
Parameters: The method cvtColor() takes three parameters which are the source image matrix, the destination image matrix, and the color conversion type.





// Java program to convert a color image to gray scale
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class GeeksforGeeks {
    public static void main(String args[]) throws Exception
        // To load  OpenCV core library
        String input = "C:/opencv/GeeksforGeeks.jpg";
        // To Read the image
        Mat source = Imgcodecs.imread(input);
        // Creating the empty destination matrix
        Mat destination = new Mat();
        // Converting the image to gray scale and 
        // saving it in the dst matrix
        Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
        // Writing the image
        Imgcodecs.imwrite("C:/opencv/GeeksforGeeks.jpg", destination);
        System.out.println("The image is successfully to Grayscale");


Input : 

Output :

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : nidhi_biet

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.