Commit | Line | Data |
---|---|---|
c724deb3 | 1 | import java.util.ArrayList; |
877f5b61 | 2 | import java.util.Iterator; |
c724deb3 | 3 | |
0e9212ae | 4 | import org.opencv.core.Core; |
fb24aa85 | 5 | import org.opencv.core.Mat; |
c724deb3 | 6 | import org.opencv.core.MatOfPoint; |
a5abe89b | 7 | import org.opencv.core.Point; |
877f5b61 | 8 | import org.opencv.core.Rect; |
0e9212ae | 9 | import org.opencv.core.Scalar; |
a5abe89b | 10 | import org.opencv.core.Size; |
7a245e8b | 11 | import org.opencv.imgcodecs.Imgcodecs; |
a5abe89b | 12 | import org.opencv.imgproc.Imgproc; |
0e9212ae | 13 | |
bf95a16f | 14 | public 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 | |
0e1260e3 | 38 | findContours(); |
0e9212ae | 39 | } |
7a245e8b | 40 | |
c621c378 E |
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 | ||
c621c378 | 53 | Imgproc.cvtColor(matOriginal, filtered, Imgproc.COLOR_BGR2GRAY); |
0e1260e3 E |
54 | // takes out unnecessary colors |
55 | ||
c621c378 | 56 | Imgproc.GaussianBlur(matOriginal, filtered, new Size(5, 5), Core.BORDER_DEFAULT); |
0e1260e3 E |
57 | |
58 | // bing bing bong bong | |
59 | // bing bing bong bong | |
60 | ||
61 | // converts image to grayscale, blurs it, and detects edges | |
62 | ||
c621c378 E |
63 | Imgproc.Canny(matOriginal, filtered, 35, 125); |
64 | ||
0e1260e3 E |
65 | Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); |
66 | ||
67 | for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) { | |
68 | MatOfPoint matOfPoint = iterator.next(); | |
69 | Rect rec = Imgproc.boundingRect(matOfPoint); | |
70 | if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) { | |
71 | ||
72 | iterator.remove(); | |
73 | } | |
74 | ||
75 | } | |
76 | Rect largestRec = null; | |
77 | double largestRecArea = 0; | |
78 | ||
79 | for (MatOfPoint mop : contours) { | |
80 | double recArea = Imgproc.contourArea(mop); | |
81 | if (largestRecArea < recArea) { | |
82 | largestRec = Imgproc.boundingRect(mop); | |
83 | largestRecArea = recArea; | |
84 | } | |
85 | } | |
86 | Imgproc.rectangle(matOriginal, largestRec.br(), largestRec.tl(), GRAY, 1); | |
87 | ||
88 | Imgcodecs.imwrite("output.png", filtered); | |
89 | ||
90 | } | |
91 | ||
92 | public static double distanceToCamera(double knownWidth, int focalLength, double perWidth) { | |
93 | return (knownWidth * ((double) (focalLength))) / perWidth; | |
c621c378 E |
94 | } |
95 | ||
7a245e8b | 96 | public static void processImage() { |
877f5b61 | 97 | |
c724deb3 | 98 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); |
877f5b61 E |
99 | int frameCount = 0; |
100 | int THE_BEST_MAGIC_NUMBER = 300; | |
101 | while (frameCount < 1) { | |
102 | contours.clear(); | |
c621c378 | 103 | matOriginal = Imgcodecs.imread("imgs/goal2.png"); // convert image |
877f5b61 E |
104 | // into matrix |
105 | ||
106 | filtered = matOriginal; | |
107 | ||
108 | Imgproc.GaussianBlur(matOriginal, filtered, new Size(25, 25), Core.BORDER_DEFAULT); | |
109 | ||
110 | Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered); | |
111 | Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 5); | |
112 | Imgproc.dilate(matOriginal, filtered, kernel, new Point(-1, -1), 0); | |
113 | Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); | |
114 | ||
115 | for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) { | |
116 | MatOfPoint matOfPoint = iterator.next(); | |
117 | Rect rec = Imgproc.boundingRect(matOfPoint); | |
118 | if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) { | |
c621c378 | 119 | |
877f5b61 E |
120 | iterator.remove(); |
121 | } | |
122 | ||
123 | } | |
124 | ||
125 | for (MatOfPoint mop : contours) { | |
126 | Rect rec = Imgproc.boundingRect(mop); | |
127 | Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), GRAY, 1); | |
c621c378 | 128 | System.out.println(rec.toString()); |
877f5b61 E |
129 | } |
130 | ||
131 | frameCount++; | |
132 | } | |
133 | ||
8f580bc8 | 134 | Imgcodecs.imwrite("output.png", filtered); |
877f5b61 E |
135 | |
136 | } | |
137 | ||
138 | /** | |
139 | * @param angle | |
140 | * a nonnormalized angle | |
141 | */ | |
8f580bc8 | 142 | |
877f5b61 E |
143 | public static double normalize360(double angle) { |
144 | while (angle >= 360.0) { | |
145 | angle -= 360.0; | |
146 | } | |
147 | while (angle < 0.0) { | |
148 | angle += 360.0; | |
149 | } | |
150 | return angle; | |
7a245e8b | 151 | } |
0e9212ae | 152 | } |