如何使用Java将图像转换为灰度。(How to convert an image to grayscale using Java.)
优质
小牛编辑
126浏览
2023-12-01
问题描述 (Problem Description)
如何使用Java将图像转换为灰度。
解决方案 (Solution)
以下是使用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");
}
}