TomTom TomTom - 2 months ago 21
Java Question

Display OpenCV Mat with JavaFX

I would like to display

Mat
objects from OpenCV directly with JavaFX. I have seen that it is possible to convert a
Mat
object into a
BufferedImage
. But as far as I know you can't display a
BufferedImage
with JavaFX, so another conversion would have to take place.

Is there a way to directly convert it into a data structure that is displayable by JavaFX?

Answer

I have 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 to .jpg but .bmp is faster.

Comments