Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+38 votes
asked in CSC285_Fall2018 by (508 points)

1 Answer

+16 votes

In tests on my Linux laptop, it appears that Imgencode using .PNG format is the slowest, Imgencode using .BMP format is about 2.5 times faster, and using the BufferedImage with array copying approach in the tutorial author's Utils class (on GitHub) was 2.5 times faster than the .BMP method!

Thus, for the best performance/smoothest video-displaying, you should use that approach in your apps. I will be including the following file in the SharedClassRepo soon...

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;

import org.opencv.core.Mat;

import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;

public class UtilsForOpenCV {

	 * Converts an OpenCV Mat (matrix) image into a JavaFX Image object.
	 * @param matImg - the OpenCV matrix to convert
	 * @return an equivalent JavaFX Image
	public static Image matToJavaFXImage(Mat matImg) {
		// Note: the code in this method is thanks to Luigi De Russis and/or Alberto Sacco
		// https://github.com/opencv-java/getting-started/tree/master/FXHelloCV
		int width = matImg.width(), height = matImg.height(), channels = matImg.channels();
		byte[] sourcePixels = new byte[width * height * channels];
		matImg.get(0, 0, sourcePixels);

		BufferedImage image = null;
		if (matImg.channels() > 1) {
			image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
		} else {
			image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
		final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
		System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);

		return SwingFXUtils.toFXImage(image, null);		
answered by (508 points)