attempts to find the largest area of contours to draw bounding rectangle around the...
authorEvanYap <evanyap.14@gmail.com>
Tue, 15 Nov 2016 04:37:00 +0000 (20:37 -0800)
committerEvanYap <evanyap.14@gmail.com>
Tue, 15 Nov 2016 04:37:00 +0000 (20:37 -0800)
IsolateImage.java

index 18b76a2c11c9908f6b6066e8171e63b161d66176..7586c82f25a9d9753c0623f54c56c9c4e4db400b 100644 (file)
@@ -35,7 +35,7 @@ public class IsolateImage {
 
                kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
 
-               processImage();
+               findContours();
        }
 
        public static void findContours() {
@@ -50,12 +50,47 @@ public class IsolateImage {
 
                filtered = matOriginal;
 
-               // bing bing bong bong
-               // bing bing bong bong
                Imgproc.cvtColor(matOriginal, filtered, Imgproc.COLOR_BGR2GRAY);
+               // takes out unnecessary colors
+
                Imgproc.GaussianBlur(matOriginal, filtered, new Size(5, 5), Core.BORDER_DEFAULT);
+
+               // bing bing bong bong
+               // bing bing bong bong
+
+               // converts image to grayscale, blurs it, and detects edges
+
                Imgproc.Canny(matOriginal, filtered, 35, 125);
 
+               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) {
+
+                               iterator.remove();
+                       }
+
+               }
+               Rect largestRec = null;
+               double largestRecArea = 0;
+
+               for (MatOfPoint mop : contours) {
+                       double recArea = Imgproc.contourArea(mop);
+                       if (largestRecArea < recArea) {
+                               largestRec = Imgproc.boundingRect(mop);
+                               largestRecArea = recArea;
+                       }
+               }
+               Imgproc.rectangle(matOriginal, largestRec.br(), largestRec.tl(), GRAY, 1);
+
+               Imgcodecs.imwrite("output.png", filtered);
+
+       }
+
+       public static double distanceToCamera(double knownWidth, int focalLength, double perWidth) {
+               return (knownWidth * ((double) (focalLength))) / perWidth;
        }
 
        public static void processImage() {