This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update to Java 8 and improve the conversion between Mat and Image
- Loading branch information
Showing
4 changed files
with
156 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package it.polito.elite.teaching.cv.utils; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.awt.image.DataBufferByte; | ||
|
||
import org.opencv.core.Mat; | ||
|
||
import javafx.application.Platform; | ||
import javafx.beans.property.ObjectProperty; | ||
import javafx.embed.swing.SwingFXUtils; | ||
import javafx.scene.image.Image; | ||
|
||
/** | ||
* Provide general purpose methods for handling OpenCV-JavaFX data conversion. | ||
* Moreover, expose some "low level" methods for matching few JavaFX behavior. | ||
* | ||
* @author <a href="mailto:luigi.derussis@polito.it">Luigi De Russis</a> | ||
* @author <a href="http://max-z.de">Maximilian Zuleger</a> | ||
* @version 1.0 (2016-09-17) | ||
* @since 1.0 | ||
* | ||
*/ | ||
public final class Utils | ||
{ | ||
/** | ||
* Convert a Mat object (OpenCV) in the corresponding Image for JavaFX | ||
* | ||
* @param frame | ||
* the {@link Mat} representing the current frame | ||
* @return the {@link Image} to show | ||
*/ | ||
public static Image mat2Image(Mat frame) | ||
{ | ||
try | ||
{ | ||
return SwingFXUtils.toFXImage(matToBufferedImage(frame), null); | ||
} | ||
catch (Exception e) | ||
{ | ||
System.err.println("Cannot convert the Mat obejct: " + e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Generic method for putting element running on a non-JavaFX thread on the | ||
* JavaFX thread, to properly update the UI | ||
* | ||
* @param property | ||
* a {@link ObjectProperty} | ||
* @param value | ||
* the value to set for the given {@link ObjectProperty} | ||
*/ | ||
public static <T> void onFXThread(final ObjectProperty<T> property, final T value) | ||
{ | ||
Platform.runLater(() -> { | ||
property.set(value); | ||
}); | ||
} | ||
|
||
/** | ||
* Support for the {@link mat2image()} method | ||
* | ||
* @param original | ||
* the {@link Mat} object in BGR or grayscale | ||
* @return the corresponding {@link BufferedImage} | ||
*/ | ||
private static BufferedImage matToBufferedImage(Mat original) | ||
{ | ||
// init | ||
BufferedImage image = null; | ||
int width = original.width(), height = original.height(), channels = original.channels(); | ||
byte[] sourcePixels = new byte[width * height * channels]; | ||
original.get(0, 0, sourcePixels); | ||
|
||
if (original.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 image; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters