OpenCV寫入圖像

Imgcodecs類的write()方法用於使用OpenCV編寫圖像。 要寫圖像,請重複前面示例中的前三個步驟。

要編寫圖像,需要調用Imgcodecs類的imwrite()方法。

以下是此方法的語法。

imwrite(filename, mat)

該方法接受以下參數 -

  • filename - 一個String變數,表示保存檔的路徑。
  • mat - 表示要寫入的圖像的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 ~

如果您打開指定的路徑,可以觀察保存的圖像。


上一篇: OpenCV讀取圖像 下一篇: OpenCV用戶介面