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