如何使用 Java OpenCV 库将彩色图像转换为蓝色/绿色/红色图像?
javaobject oriented programmingprogramming
Imgproc 类的 cvtColor() 方法将图像的颜色从一种颜色更改为另一种颜色。此方法接受三个参数 −
src − 表示源的 Matrix 对象。
dst − 表示目标的 Matrix 对象。
code − 表示目标图像颜色的整数值。
要将彩色图像转换为灰度图像,您需要将 Imgproc.COLOR_RGB2BGR 作为第三个参数传递给此方法。
示例
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ColorToGrayscale { public static void main(String args[]) throws Exception { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the image Mat src = Imgcodecs.imread("spiderman.jpg"); //Creating the empty destination matrix Mat dst = new Mat(); //Converting the image to grey scale Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY); //Instantiating the Imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); //Writing the image imageCodecs.imwrite("rgb2bgr.jpg", dst); System.out.println("Image Saved"); } }