manage some rectangles to be drawn, with some very rough magic numbers
[3501/OpenCVShowImage] / IsolateImage.java
CommitLineData
c724deb3 1import java.util.ArrayList;
877f5b61 2import java.util.Iterator;
c724deb3 3
0e9212ae 4import org.opencv.core.Core;
fb24aa85 5import org.opencv.core.Mat;
c724deb3 6import org.opencv.core.MatOfPoint;
a5abe89b 7import org.opencv.core.Point;
877f5b61 8import org.opencv.core.Rect;
0e9212ae 9import org.opencv.core.Scalar;
a5abe89b 10import org.opencv.core.Size;
7a245e8b 11import org.opencv.imgcodecs.Imgcodecs;
a5abe89b 12import org.opencv.imgproc.Imgproc;
0e9212ae 13
bf95a16f 14public class IsolateImage {
0e9212ae 15
fb24aa85 16 public static Mat matOriginal;
7a245e8b 17 public static Mat filtered;
a5abe89b 18 public static Mat kernel;
c724deb3 19 public static Mat matHierarchy;
fb24aa85 20
877f5b61
E
21 public static final Scalar LOWER_BOUNDS = new Scalar(80, 80, 80);
22 public static final Scalar UPPER_BOUNDS = new Scalar(140, 180, 140);
c724deb3 23 public static final Scalar BLACK = new Scalar(0, 0, 0);
877f5b61 24 public static final Scalar GRAY = new Scalar(100, 100, 100);
0e9212ae
E
25
26 static {
27 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
28 }
29
0e9212ae 30 public static void main(String args[]) throws Exception {
fb24aa85
E
31
32 matOriginal = new Mat();
7a245e8b 33 filtered = new Mat();
c724deb3 34 matHierarchy = new Mat();
fb24aa85 35
877f5b61 36 kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
a5abe89b 37
8f580bc8 38 processImage();
0e9212ae 39 }
7a245e8b
E
40
41 public static void processImage() {
877f5b61 42
c724deb3 43 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
877f5b61
E
44 int frameCount = 0;
45 int THE_BEST_MAGIC_NUMBER = 300;
46 while (frameCount < 1) {
47 contours.clear();
48 matOriginal = Imgcodecs.imread("imgs/goal.png"); // convert image
49 // into matrix
50
51 filtered = matOriginal;
52
53 Imgproc.GaussianBlur(matOriginal, filtered, new Size(25, 25), Core.BORDER_DEFAULT);
54
55 Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered);
56 Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 5);
57 Imgproc.dilate(matOriginal, filtered, kernel, new Point(-1, -1), 0);
58 Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
59
60 for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) {
61 MatOfPoint matOfPoint = iterator.next();
62 Rect rec = Imgproc.boundingRect(matOfPoint);
63 if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) {
64 System.out.println(rec.toString());
65 iterator.remove();
66 }
67
68 }
69
70 for (MatOfPoint mop : contours) {
71 Rect rec = Imgproc.boundingRect(mop);
72 Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), GRAY, 1);
73 }
74
75 frameCount++;
76 }
77
8f580bc8 78 Imgcodecs.imwrite("output.png", filtered);
877f5b61
E
79 System.out.println("done");
80
81 }
82
83 /**
84 * @param angle
85 * a nonnormalized angle
86 */
8f580bc8 87
877f5b61
E
88 public static double normalize360(double angle) {
89 while (angle >= 360.0) {
90 angle -= 360.0;
91 }
92 while (angle < 0.0) {
93 angle += 360.0;
94 }
95 return angle;
7a245e8b 96 }
0e9212ae 97}