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;
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);
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;
}
}