├── .classpath ├── .gitignore ├── .project ├── README.md ├── build.fxbuild ├── screenshots ├── one-tennis-ball.png └── two-tennis-balls.png └── src └── it └── polito ├── elite └── teaching │ └── cv │ └── utils │ └── Utils.java └── teaching └── cv ├── ObjRecognition.fxml ├── ObjRecognition.java ├── ObjRecognitionController.java └── application.css /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lab7 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.xtext.ui.shared.xtextBuilder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.xtext.ui.shared.xtextNature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Object Recognition with OpenCV and JavaFX 2 | 3 | [![No Maintenance Intended](http://unmaintained.tech/badge.svg)](http://unmaintained.tech/) 4 | 5 | *Computer Vision course - [Politecnico di Torino](http://www.polito.it)* 6 | 7 | A project, made in Eclipse (Neon), for identify and track one or more tennis balls. It performs the detection of the tennis balls upon a webcam video stream by using the color range of the balls, erosion and dilation, and the `findContours` method. Some screenshots of the running project are available in the `screenshots` folder. 8 | 9 | Please, note that the project is an Eclipse project, made for teaching purposes. Before using it, you need to install the OpenCV library (version 3.x) and JavaFX 8 and create a `User Library` named `opencv` that links to the OpenCV jar and native libraries. 10 | 11 | A guide for getting started with OpenCV and Java is available at [http://opencv-java-tutorials.readthedocs.org/en/latest/index.html](http://opencv-java-tutorials.readthedocs.org/en/latest/index.html). 12 | -------------------------------------------------------------------------------- /build.fxbuild: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /screenshots/one-tennis-ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opencv-java/object-detection/7f96acce8265cef673788186d2323519b09c20fe/screenshots/one-tennis-ball.png -------------------------------------------------------------------------------- /screenshots/two-tennis-balls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/opencv-java/object-detection/7f96acce8265cef673788186d2323519b09c20fe/screenshots/two-tennis-balls.png -------------------------------------------------------------------------------- /src/it/polito/elite/teaching/cv/utils/Utils.java: -------------------------------------------------------------------------------- 1 | package it.polito.elite.teaching.cv.utils; 2 | 3 | import java.awt.image.BufferedImage; 4 | import java.awt.image.DataBufferByte; 5 | 6 | import org.opencv.core.Mat; 7 | 8 | import javafx.application.Platform; 9 | import javafx.beans.property.ObjectProperty; 10 | import javafx.embed.swing.SwingFXUtils; 11 | import javafx.scene.image.Image; 12 | 13 | /** 14 | * Provide general purpose methods for handling OpenCV-JavaFX data conversion. 15 | * Moreover, expose some "low level" methods for matching few JavaFX behavior. 16 | * 17 | * @author Luigi De Russis 18 | * @author Maximilian Zuleger 19 | * @version 1.0 (2016-09-17) 20 | * @since 1.0 21 | * 22 | */ 23 | public final class Utils 24 | { 25 | /** 26 | * Convert a Mat object (OpenCV) in the corresponding Image for JavaFX 27 | * 28 | * @param frame 29 | * the {@link Mat} representing the current frame 30 | * @return the {@link Image} to show 31 | */ 32 | public static Image mat2Image(Mat frame) 33 | { 34 | try 35 | { 36 | return SwingFXUtils.toFXImage(matToBufferedImage(frame), null); 37 | } 38 | catch (Exception e) 39 | { 40 | System.err.println("Cannot convert the Mat obejct: " + e); 41 | return null; 42 | } 43 | } 44 | 45 | /** 46 | * Generic method for putting element running on a non-JavaFX thread on the 47 | * JavaFX thread, to properly update the UI 48 | * 49 | * @param property 50 | * a {@link ObjectProperty} 51 | * @param value 52 | * the value to set for the given {@link ObjectProperty} 53 | */ 54 | public static void onFXThread(final ObjectProperty property, final T value) 55 | { 56 | Platform.runLater(() -> { 57 | property.set(value); 58 | }); 59 | } 60 | 61 | /** 62 | * Support for the {@link mat2image()} method 63 | * 64 | * @param original 65 | * the {@link Mat} object in BGR or grayscale 66 | * @return the corresponding {@link BufferedImage} 67 | */ 68 | private static BufferedImage matToBufferedImage(Mat original) 69 | { 70 | // init 71 | BufferedImage image = null; 72 | int width = original.width(), height = original.height(), channels = original.channels(); 73 | byte[] sourcePixels = new byte[width * height * channels]; 74 | original.get(0, 0, sourcePixels); 75 | 76 | if (original.channels() > 1) 77 | { 78 | image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); 79 | } 80 | else 81 | { 82 | image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); 83 | } 84 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 85 | System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length); 86 | 87 | return image; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/it/polito/teaching/cv/ObjRecognition.fxml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 28 | 29 |
30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
41 | 42 | 43 | 44 | 45 | 46 |