Opencv-gui

提供:Dev Guides
移動先:案内検索

OpenCV-GUI

前の章で、OpenCV Javaライブラリを使用して画像を読み込んで保存する方法について説明しました。 それに加えて、AWT/SwingsやJavaFXなどのGUIライブラリを使用して、読み込んだ画像を別のウィンドウに表示することもできます。

マットをバッファリングされたイメージに変換する

画像を読み取るには、メソッド* imread()を使用します。 このメソッドは、読み取った画像を *Matrix の形式で返します。 ただし、このイメージを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 -画像のマットオブジェクト
  • 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オブジェクトの準備

前の手順で作成した入力ストリームオブジェクトを 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ライブラリを使用して、画像を*読み取り*、スイングウィンドウで*表示*する方法を示しています。

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

それに加えて、次のように、ロードされた画像を表示するウィンドウを見ることができます-

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

それに加えて、次のように、ロードされた画像を表示するウィンドウを見ることができます-

JavaFXを使用した画像の表示