How to convert OpenCV Mat object to JavaFX WritableImage?

JavaObject Oriented ProgrammingProgramming

If you try to read an image using the OpenCV imread() method it returns a Mat object. If you want to display the contents of the resultant Mat object using a JavaFX window You need to convert the Mat object to an object of the class javafx.scene.image.WritableImage. To do so, you need to follow the steps given below −

  • Encode the Mat to MatOfByte − First of all, you need to convert the matrix to the matrix of a byte. You can do it using the method imencode() of the class Imgcodecs.

  • This method accepts a String parameter(specifying the image format), a Mat object (representing the image), a MatOfByte object.

  • Convert the MatOfByte object to byte array − Convert the MatOfByte object into a byte array using the method toArray().

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

  • Creating 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, invoke the toFXImage() method of the SwingFXUtils class by passing 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
raja
Published on 10-Apr-2020 07:46:02
Advertisements