Make guassian blur work with BORDER_DEFAULT
authorEvanYap <evanyap.14@gmail.com>
Tue, 18 Oct 2016 04:00:25 +0000 (21:00 -0700)
committerEvanYap <evanyap.14@gmail.com>
Tue, 18 Oct 2016 04:00:25 +0000 (21:00 -0700)
IsolateImage.java

index d2462da93c164557e00b634c794f757d7ea03117..64a82a0b183950bd16f664f05a29743679aa2c6f 100644 (file)
@@ -1,5 +1,8 @@
+import java.util.ArrayList;
+
 import org.opencv.core.Core;
 import org.opencv.core.Mat;
+import org.opencv.core.MatOfPoint;
 import org.opencv.core.Point;
 import org.opencv.core.Scalar;
 import org.opencv.core.Size;
@@ -11,10 +14,11 @@ public class IsolateImage {
        public static Mat matOriginal;
        public static Mat filtered;
        public static Mat kernel;
+       public static Mat matHierarchy;
 
        public static final Scalar LOWER_BOUNDS = new Scalar(169, 164, 103);
        public static final Scalar UPPER_BOUNDS = new Scalar(255, 229, 140);
-       // public static final Scalar LOWER_BOUNDS = new Scalar(255, 0, 0);
+       public static final Scalar BLACK = new Scalar(0, 0, 0);
        // public static final Scalar UPPER_BOUNDS = new Scalar(0, 255, 255);
 
        static {
@@ -25,21 +29,32 @@ public class IsolateImage {
 
                matOriginal = new Mat();
                filtered = new Mat();
+               matHierarchy = new Mat();
 
-               kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
+               kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
 
                processImage();
        }
 
        public static void processImage() {
+               ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
+               contours.clear();
                matOriginal = Imgcodecs.imread("imgs/testpicture.png"); // convert image
-                                                                                                                               // into matrix
+               // into matrix
 
                filtered = matOriginal;
+               Imgproc.GaussianBlur(matOriginal, filtered, new Size(17, 17), Core.BORDER_DEFAULT);
 
                Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered);
-               Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 2);
-
+               // Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 4);
+               Imgproc.dilate(matOriginal, filtered, kernel, new Point(-1, -1), 4);
+               // Imgproc.findContours(filtered, contours, matHierarchy,
+               // Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+               // for (MatOfPoint mop : contours) {
+               // Rect rec = Imgproc.boundingRect(mop);
+               // Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), BLACK, 5);
+               // }
                Imgcodecs.imwrite("output.png", filtered);
 
        }