implement erode to get rid of unwanted colors
[3501/OpenCVShowImage] / IsolateImage.java
1 import org.opencv.core.Core;
2 import org.opencv.core.Mat;
3 import org.opencv.core.Point;
4 import org.opencv.core.Scalar;
5 import org.opencv.core.Size;
6 import org.opencv.imgcodecs.Imgcodecs;
7 import org.opencv.imgproc.Imgproc;
8
9 public class IsolateImage {
10
11 public static Mat matOriginal;
12 public static Mat filtered;
13 public static Mat kernel;
14
15 public static final Scalar LOWER_BOUNDS = new Scalar(169, 164, 103);
16 public static final Scalar UPPER_BOUNDS = new Scalar(255, 229, 140);
17 // public static final Scalar LOWER_BOUNDS = new Scalar(255, 0, 0);
18 // public static final Scalar UPPER_BOUNDS = new Scalar(0, 255, 255);
19
20 static {
21 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
22 }
23
24 public static void main(String args[]) throws Exception {
25
26 matOriginal = new Mat();
27 filtered = new Mat();
28
29 kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
30
31 processImage();
32 }
33
34 public static void processImage() {
35 matOriginal = Imgcodecs.imread("imgs/testpicture.png"); // convert image
36 // into matrix
37
38 filtered = matOriginal;
39
40 Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered);
41 Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 2);
42
43 Imgcodecs.imwrite("output.png", filtered);
44
45 }
46 }