OpenCV - 圖形使用者介面



在前面的章節中,我們討論瞭如何使用 OpenCV Java 庫讀取和儲存影像。此外,我們還可以使用 AWT/Swings 和 JavaFX 等 GUI 庫在單獨的視窗中顯示載入的影像。

將 Mat 轉換為 Buffered Image

要讀取影像,我們使用 imread() 方法。此方法以 矩陣 的形式返回讀取的影像。但是,要將此影像與 GUI 庫(AWT/Swings 和 JavaFX)一起使用,應將其轉換為 java.awt.image.BufferedImage 包中的 BufferedImage 類的物件。

以下是將 OpenCV 的 Mat 物件轉換為 BufferedImage 物件的步驟。

步驟 1:將 Mat 編碼為 MatOfByte

首先,您需要將矩陣轉換為位元組矩陣。您可以使用 Imgcodecs 類的 imencode() 方法來實現。以下是此方法的語法。

imencode(ext, image, matOfByte);

此方法接受以下引數:

  • Ext − 指定影像格式(.jpg、.png 等)的字串引數

  • image − 影像的 Mat 物件

  • matOfByte − MatOfByte 類的空物件

使用此方法對影像進行編碼,如下所示。

//Reading the image 
Mat image = Imgcodecs.imread(file);

//instantiating an empty MatOfByte class 
MatOfByte matOfByte = new MatOfByte();

//Converting the Mat object to MatOfByte 
Imgcodecs.imencode(".jpg", image, matOfByte);

步驟 2:將 MatOfByte 物件轉換為位元組陣列

使用 toArray() 方法將 MatOfByte 物件轉換為位元組陣列。

byte[] byteArray = matOfByte.toArray();

步驟 3:準備 InputStream 物件

透過將上一步中建立的位元組陣列傳遞給 ByteArrayInputStream 類的建構函式來準備 InputStream 物件。

//Preparing the InputStream object 
InputStream in = new ByteArrayInputStream(byteArray);

步驟 4:準備 InputStream 物件

將上一步中建立的 Input Stream 物件傳遞給 ImageIO 類的 read() 方法。這將返回一個 BufferedImage 物件。

//Preparing the BufferedImage 
BufferedImage bufImage = ImageIO.read(in);

使用 AWT/Swings 顯示影像

要使用 AWT/Swings 框架顯示影像,首先使用 imread() 方法讀取影像,並按照上述步驟將其轉換為 BufferedImage

然後,例項化 JFrame 類並將建立的緩衝影像新增到 JFrame 的 ContentPane 中,如下所示:

//Instantiate JFrame 
JFrame frame = new JFrame();
 
//Set Content to the JFrame 
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); 
frame.pack(); 
frame.setVisible(true);

示例

以下程式程式碼演示瞭如何使用 OpenCV 庫讀取影像並透過 Swing 視窗顯示它。

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

public class DisplayingImagesUsingSwings {
   public static void main(String args[]) throws Exception { 
      //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 = "C:/EXAMPLES/OpenCV/sample.jpg"; 
      Mat image = Imgcodecs.imread(file); 
    
      //Encoding the image 
      MatOfByte matOfByte = new MatOfByte();       
      Imgcodecs.imencode(".jpg", image, matOfByte); 

      //Storing the encoded Mat in a byte array 
      byte[] byteArray = matOfByte.toArray(); 

      //Preparing the Buffered Image 
      InputStream in = new ByteArrayInputStream(byteArray); 
      BufferedImage bufImage = ImageIO.read(in); 

      //Instantiate JFrame 
      JFrame frame = new JFrame(); 

      //Set Content to the JFrame 
      frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); 
      frame.pack(); 
      frame.setVisible(true);
      
      System.out.println("Image Loaded");     
   } 
}

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

Image Loaded

此外,您還可以看到一個顯示載入影像的視窗,如下所示:

Displaying Image using Swings

使用 JavaFX 顯示影像

要使用 JavaFX 顯示影像,首先使用 imread() 方法讀取影像並將其轉換為 BufferedImage。然後,將 BufferedImage 轉換為 WritableImage,如下所示。

WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);

將此 WritableImage 物件傳遞給 ImageView 類的建構函式。

ImageView imageView = new ImageView(writableImage);

示例

以下程式程式碼演示瞭如何使用 OpenCV 庫讀取影像並透過 JavaFX 視窗顯示它。

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.stage.Stage;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;

public class DisplayingImagesJavaFX extends Application {
   @Override 
   public void start(Stage stage) throws IOException {   
      WritableImage writableImage = loadImage(); 
  
      //Setting the image view 
      ImageView imageView = new ImageView(writableImage); 
        
      //Setting the position of the image 
      imageView.setX(50); 
      imageView.setY(25); 
        
      //setting the fit height and width of the image view 
      imageView.setFitHeight(400); 
      imageView.setFitWidth(500);
      
      //Setting the preserve ratio of the image view
      imageView.setPreserveRatio(true);
      
      //Creating a Group object  
      Group root = new Group(imageView);
      
      //Creating a scene object
      Scene scene = new Scene(root, 600, 400);
      
      //Setting title to the Stage 
      stage.setTitle("Loading an image");
      
      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   } 
   public WritableImage loadImage() throws IOException {
      //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 ="C:/EXAMPLES/OpenCV/sample.jpg";
      Mat image = Imgcodecs.imread(file);
      
      //Encoding the image
      MatOfByte matOfByte = new MatOfByte();
      Imgcodecs.imencode(".jpg", image, matOfByte);

      //Storing the encoded Mat in a byte array
      byte[] byteArray = matOfByte.toArray();
      
      //Displaying the image
      InputStream in = new ByteArrayInputStream(byteArray); 
      BufferedImage bufImage = ImageIO.read(in);

      System.out.println("Image Loaded");
      WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
      return writableImage; 
   }
   public static void main(String args[]) {
      launch(args); 
   } 
}

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

Image Loaded

此外,您還可以看到一個顯示載入影像的視窗,如下所示:

Displaying Image using JavaFX
廣告