OpenCV - 寫入影像



OpenCV 的 `Imgcodecs` 類中的 `write()` 方法用於寫入影像。要寫入影像,請重複上一個示例中的前三個步驟。

要寫入影像,您需要呼叫 `Imgcodecs` 類的 `imwrite()` 方法。

以下是此方法的語法。

imwrite(filename, mat)

此方法接受以下引數:

  • **filename** - 一個表示儲存檔案路徑的 **String** 變數。

  • **mat** - 一個表示要寫入影像的 **Mat** 物件。

示例

下面的程式是使用 Java 程式和 OpenCV 庫 **寫入影像** 的示例。

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 ="C:/EXAMPLES/OpenCV/sample.jpg";   
      Mat matrix = imageCodecs.imread(file); 

      System.out.println("Image Loaded ..........");
      String file2 = "C:/EXAMPLES/OpenCV/sample_resaved.jpg"; 

      //Writing the image 
      imageCodecs.imwrite(file2, matrix); 
      System.out.println("Image Saved ............"); 
   } 
}

執行上述程式後,您將獲得以下輸出:

Image Loaded .......... 
Image Saved ...........

如果您開啟指定的路徑,您可以觀察到如下所示的已儲存影像:

Write Image
廣告
© . All rights reserved.