From 877f5b61a1438e3076b69d0351adb52159f065fa Mon Sep 17 00:00:00 2001 From: EvanYap Date: Mon, 24 Oct 2016 19:33:00 -0700 Subject: [PATCH] manage some rectangles to be drawn, with some very rough magic numbers --- IsolateImage.java | 78 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/IsolateImage.java b/IsolateImage.java index 64a82a0..5768d5b 100644 --- a/IsolateImage.java +++ b/IsolateImage.java @@ -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 contours = new ArrayList(); - 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 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; } } -- 2.30.2