manage some rectangles to be drawn, with some very rough magic numbers
authorEvanYap <evanyap.14@gmail.com>
Tue, 25 Oct 2016 02:33:00 +0000 (19:33 -0700)
committerEvanYap <evanyap.14@gmail.com>
Tue, 25 Oct 2016 02:33:00 +0000 (19:33 -0700)
IsolateImage.java

index 64a82a0b183950bd16f664f05a29743679aa2c6f..5768d5b786d2a539e1bcdf51f06f660f7c5e1f6c 100644 (file)
@@ -1,9 +1,11 @@
 import java.util.ArrayList;
+import java.util.Iterator;
 
 import org.opencv.core.Core;
 import org.opencv.core.Mat;
 import org.opencv.core.MatOfPoint;
 import org.opencv.core.Point;
+import org.opencv.core.Rect;
 import org.opencv.core.Scalar;
 import org.opencv.core.Size;
 import org.opencv.imgcodecs.Imgcodecs;
@@ -16,10 +18,10 @@ public class IsolateImage {
        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(80, 80, 80);
+       public static final Scalar UPPER_BOUNDS = new Scalar(140, 180, 140);
        public static final Scalar BLACK = new Scalar(0, 0, 0);
-       // public static final Scalar UPPER_BOUNDS = new Scalar(0, 255, 255);
+       public static final Scalar GRAY = new Scalar(100, 100, 100);
 
        static {
                System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
@@ -31,31 +33,65 @@ public class IsolateImage {
                filtered = new Mat();
                matHierarchy = new Mat();
 
-               kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
+               kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
 
                processImage();
        }
 
        public static void processImage() {
+
                ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
-               contours.clear();
-               matOriginal = Imgcodecs.imread("imgs/testpicture.png"); // convert image
-               // 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), 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);
-               // }
+               int frameCount = 0;
+               int THE_BEST_MAGIC_NUMBER = 300;
+               while (frameCount < 1) {
+                       contours.clear();
+                       matOriginal = Imgcodecs.imread("imgs/goal.png"); // convert image
+                       // into matrix
+
+                       filtered = matOriginal;
+
+                       Imgproc.GaussianBlur(matOriginal, filtered, new Size(25, 25), Core.BORDER_DEFAULT);
+
+                       Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered);
+                       Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 5);
+                       Imgproc.dilate(matOriginal, filtered, kernel, new Point(-1, -1), 0);
+                       Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+                       for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) {
+                               MatOfPoint matOfPoint = iterator.next();
+                               Rect rec = Imgproc.boundingRect(matOfPoint);
+                               if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) {
+                                       System.out.println(rec.toString());
+                                       iterator.remove();
+                               }
+
+                       }
+
+                       for (MatOfPoint mop : contours) {
+                               Rect rec = Imgproc.boundingRect(mop);
+                               Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), GRAY, 1);
+                       }
+
+                       frameCount++;
+               }
+
                Imgcodecs.imwrite("output.png", filtered);
+               System.out.println("done");
+
+       }
+
+       /**
+        * @param angle
+        *            a nonnormalized angle
+        */
 
+       public static double normalize360(double angle) {
+               while (angle >= 360.0) {
+                       angle -= 360.0;
+               }
+               while (angle < 0.0) {
+                       angle += 360.0;
+               }
+               return angle;
        }
 }