Displaying an OpenCV Layout with JavaFX

I would like to display Mat objects from OpenCV directly using JavaFX. I saw that you can convert a Mat object to a BufferedImage . But as far as I know, you cannot display BufferedImage using JavaFX, so another conversion will be required.

Is there a way to directly convert it to the data structure displayed by JavaFX?

+6
source share
4 answers

I found a direct way to convert a Mat object to a JavaFX Image object.

 MatOfByte byteMat = new MatOfByte(); Highgui.imencode(".bmp", mat, byteMat); return new Image(new ByteArrayInputStream(byteMat.toArray())); 

You can also encode it .jpg, but .bmp is faster.

+6
source

The stupid way to do this is to convert Mat to BufferedImage and then to Image so that it appears inside the ImageView :

 Mat >> BufferedImage >> Image >> ImageView 

Assuming you know how to do the first conversion, the rest will be something like this:

 import javafx.embed.swing.SwingFXUtils; import javafx.scene.image.Image; import javafx.scene.image.ImageView; Image image = SwingFXUtils.toFXImage(bufImage, null); ImageView imgView = new ImageView(); imgView.setImage(image); 

I have not tested the code, but that’s the general idea.

+2
source

TomTom's answer was very helpful and solved the problem, but Highgui no longer has bindings in java.

As with OpenCV 3.0.0, the updated code is more similar:

 MatOfByte byteMat = new MatOfByte(); Imgcodecs.imencode(".bmp", mat, byteMat); return new Image(new ByteArrayInputStream(byteMat.toArray())); 
+2
source

Instead of encoding and decoding to get an array of bytes, you just need to declare the array and use the get method. This is the Scala code, but hopefully it is clear:

 val arr = new Array[Byte](w * h * 3) mat.get(0, 0, arr) pw.setPixels(0, 0, w, h, PixelFormat.getByteRgbInstance, arr, 0, w * 3) 

This code declares an arr array of type byte[] with a size corresponding to the image h on w and 3 channels. Then the data is copied from the mat object to the array and passed to the setPixels method from the PixelWriter pw object.

0
source

Source: https://habr.com/ru/post/980465/


All Articles