如何使用 Java OpenCV 库编写图像?
使用 OpenCV 库,你可以执行图像处理操作,例如图像滤镜、几何图像转换、色彩空间转换、柱状图等。
写入图像
每当你使用 Imgcodecs 类的 imread() 方法读取图像内容时,结果都会读入矩阵对象。
你可以使用 imwrite() 方法来写入/保存图像。它接受两个参数,即:
文件 - 表示要将结果存储到的文件路径的字符串值。
图像 - 包含要保存的图像数据的矩阵对象。
示例
以下 Java 示例将图像 cat.jpg 的内容作为灰度图像读取,并以另一个名称重新保存。
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; public class WritingImages { public static void main(String args[]) { //Loading the OpenCV core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Reading the Image from the file and storing it in to a Matrix object String file ="D://images//cat.jpg"; Mat matrix = Imgcodecs.imread(file); System.out.println("Image Loaded"); String file2 = "D://images//sample_resaved.jpg"; //Writing the image Imgcodecs.imwrite(file2, matrix); System.out.println("Image Saved"); } }
输入:cat.jpg
输出:sample_resaved.jpg
广告