如何使用 Java OpenCV 庫翻轉影像?


OpenCV Core 類的flip()方法沿 x/y 軸翻轉影像。此方法接受 −

  • 包含原始影像資料的源矩陣。

  • 用於儲存結果影像資料的空目標矩陣。

  • 翻轉程式碼,用於指定影像的方向(0 –x 軸,+ve – y 軸,– ve 兩條軸)。

若要翻轉影像 −

  • 使用 loadLibrary() 方法載入 OpenCV 核心本機庫。

  • 使用 imread() 方法讀取影像檔案的內容到矩陣中。

  • 建立一個空矩陣來儲存結果。

  • 透過傳遞上述建立的矩陣來呼叫 flip() 方法。

  • 使用imwrite()方法建立影像,將目標矩陣作為引數傳遞。

示例

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
public class ChangingOrientation {
   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 src = Imgcodecs.imread(file);
      //Creating an empty matrix to store the result
      Mat dst = new Mat();
      //Changing the orientation of an image
      Core.flip(src, dst, -1);
      //Writing the image
      Imgcodecs.imwrite("D:\Images\flipping.jpg", dst);
      System.out.println("Image Processed");
   }
}

輸入

輸出

更新日期: 09-Apr-2020

817 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.