为了使用OpenCV将彩色图像转换为灰度图像,我们将图像读入BuffereImage并将其转换为Mat对象。
null
Syntax: File input = new File("digital_image_processing.jpg"); BufferedImage image = ImageIO.read(input);
使用Imgproc类中的方法cvtColor()将图像从RGB转换为灰度格式。
语法: Imgproc。CVT颜色(源mat、目标mat1、Imgproc.COLOR_RGB2GRAY); 参数: 方法cvtColor()采用三个参数,即源图像矩阵、目标图像矩阵和颜色转换类型。
// 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 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 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 :
![]()
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END