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?
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.
TomTom's answer was very helpful and solved the problem, but Highgui does no longer have any bindings in java.
As of OpenCV 3.0.0, the up-to-date code is more like:
MatOfByte byteMat = new MatOfByte();
Imgcodecs.imencode(".bmp", mat, byteMat);
return new Image(new ByteArrayInputStream(byteMat.toArray()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With