A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/convert_image_into_grayscale.htm below:

How to convert an image to grayscale using Java

How to convert an image to grayscale using Java Problem Description

How to convert an image to grayscale using Java.

Solution

Following is the program to convert an image to grayscale using Java.

import org.opencv.core.Core;
import org.opencv.core.Mat;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class ConvertingImageToGrayScale {
   public static void main(String args[]) throws Exception {
      
      //Loading the OpenCV core library
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME );
      String input = "C:/opencv/sample.jpg";

      //Reading the image
      Mat src = Imgcodecs.imread(input);

      //Creating the empty destination matrix
      Mat dst = new Mat();

      //Converting the image to gray scale and saving it in the dst matrix
      Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY);

      //Writing the image
      Imgcodecs.imwrite("C:/opencv/imageToGrayScale.jpg", dst);
      System.out.println("Converted to Grayscale");
   }
}
Input Output

java_opencv


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4