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