Imgcodecs
类的write()
方法用于使用OpenCV编写图像。 要写图像,请重复前面示例中的前三个步骤。
要编写图像,需要调用Imgcodecs
类的imwrite()
方法。
以下是此方法的语法。
imwrite(filename, mat)
该方法接受以下参数 -
String
变量,表示保存文件的路径。Mat
对象。以下程序是使用OpenCV库使用Java程序编写图像的示例。
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); //Instantiating the imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); //Reading the Image from the file and storing it in to a Matrix object String file ="F:/worksp/opencv/images/sample.jpg"; Mat matrix = imageCodecs.imread(file); System.out.println("Image Loaded .........."); String file2 = "F:/worksp/opencv/images/sample_resaved.jpg"; //Writing the image imageCodecs.imwrite(file2, matrix); System.out.println("Image Saved ~ "); } }
在执行上述程序时,您将得到以下输出 -
Image Loaded .......... Image Saved ~
如果您打开指定的路径,可以观察保存的图像。