如何使用 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

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP