Home  >  Article  >  Java  >  How to convert OpenCV's Mat object to JavaFX's WritableImage object?

How to convert OpenCV's Mat object to JavaFX's WritableImage object?

WBOY
WBOYforward
2023-08-18 18:34:211582browse

How to convert OpenCVs Mat object to JavaFXs WritableImage object?

If you try to read an image using OpenCV's imread() method, it will return a Mat object. If you want to use a JavaFX window to display the contents of the resulting Mat object, you need to convert the Mat object to an object of the javafx.scene.image.WritableImage class. In order to do this, you need to follow these steps:

  • Encode Mat to MatOfByte - First, you need to convert the matrix to a byte matrix. You can use the imencode() method of the Imgcodecs class to achieve this.

  • This method accepts a string parameter (specifies the image format), a Mat object (representing the image), and a MatOfByte object.

  • Convert MatOfByte object to byte array - Use toArray() method to convert MatOfByte object to byte array.

  • Instantiate ByteArrayInputStream - Instantiate the ByteArrayInputStream class by passing the byte array created in the previous step to one of its constructors.

  • Create a BufferedImage object - Pass the input stream object created in the previous step to the read() method of the ImageIO class. This will return a BufferedImage object.

  • Finally, call the toFXImage() method of the SwingFXUtils class by taking the BufferedImage object obtained in the previous step as a parameter.

Example

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.imgcodecs.Imgcodecs;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.WritableImage;
public class Mat2WritableImage {
   public static WritableImage Mat2WritableImage(Mat mat) throws IOException{
      //Encoding the image
      MatOfByte matOfByte = new MatOfByte();
      Imgcodecs.imencode(".jpg", mat, 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);
      System.out.println("Image Loaded");
      WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
      return writableImage;
   }
   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
      String file = "C:/EXAMPLES/OpenCV/sample.jpg";
      Mat image = Imgcodecs.imread(file);
      WritableImage obj = Mat2WritableImage(image);
      System.out.println(obj);
   }
}

Output

Image Loaded
javafx.scene.image.WritableImage@142269f2

The above is the detailed content of How to convert OpenCV's Mat object to JavaFX's WritableImage object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete