├── .gitignore
├── results
├── original.png
├── background-removal.png
└── foreground-removal.png
├── src
└── it
│ └── polito
│ └── elite
│ └── teaching
│ └── cv
│ ├── application.css
│ ├── ImageSegController.java
│ ├── ImageSegmentation.java
│ ├── ImageSeg.fxml
│ └── utils
│ └── Utils.java
├── .classpath
├── .project
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin
2 |
--------------------------------------------------------------------------------
/results/original.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/opencv-java/image-segmentation/HEAD/results/original.png
--------------------------------------------------------------------------------
/results/background-removal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/opencv-java/image-segmentation/HEAD/results/background-removal.png
--------------------------------------------------------------------------------
/results/foreground-removal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/opencv-java/image-segmentation/HEAD/results/foreground-removal.png
--------------------------------------------------------------------------------
/src/it/polito/elite/teaching/cv/application.css:
--------------------------------------------------------------------------------
1 | /* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
--------------------------------------------------------------------------------
/src/it/polito/elite/teaching/cv/ImageSegController.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/opencv-java/image-segmentation/HEAD/src/it/polito/elite/teaching/cv/ImageSegController.java
--------------------------------------------------------------------------------
/src/it/polito/elite/teaching/cv/ImageSegmentation.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/opencv-java/image-segmentation/HEAD/src/it/polito/elite/teaching/cv/ImageSegmentation.java
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Lab6
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 | ## Image Segmentation with OpenCV and JavaFX
2 |
3 | [](http://unmaintained.tech/)
4 |
5 | *Computer Vision course - [Politecnico di Torino](http://www.polito.it)*
6 |
7 | A project, made in Eclipse (Neon), for experimenting with edge detection, erosion and dilatation. It performs image segmentation upon a webcam video stream. Some screenshots of the running project are available in the `results` 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 |
--------------------------------------------------------------------------------
/src/it/polito/elite/teaching/cv/ImageSeg.fxml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------