1 | /*
|
---|
2 | StartAndEndPoint l1 = parseArray[i];
|
---|
3 | StartAndEndPoint l2 = parseArray[i+ 1];
|
---|
4 | //CHECK WHICH line starts after the other
|
---|
5 | //If l1 is starting after, then comparisons are based around l1.s
|
---|
6 | //System.out.println("l1: " + l1.getP1().x);
|
---|
7 | //System.out.println("l2: " + l2.getP1().x);
|
---|
8 |
|
---|
9 | System.out.println("1.0: L1S: " + l1.getP1().x + " larger than L2S: " + l2.getP1().x);
|
---|
10 | if(l1.getP1().x > l2.getP1().x) {
|
---|
11 | System.out.println("1.1: Comparing L1S: " + l1.getP1().x + " less than L2E: " + l2.getP2().x);
|
---|
12 | if (l1.getP1().x < l2.getP2().x) {
|
---|
13 | //AND
|
---|
14 | System.out.println("1.2: Comparing L1S: " + l1.getP1().x + " larger than L2S: " + l2.getP1().x);
|
---|
15 | if (l1.getP1().x > l2.getP1().x) {
|
---|
16 | System.out.println("1: Success. NEXT");
|
---|
17 | //IT IS INTERSECTED
|
---|
18 | continue;
|
---|
19 | }
|
---|
20 | else {
|
---|
21 | //FAILED SECOND COMPARISON
|
---|
22 | System.out.println("1: Fail");
|
---|
23 | }
|
---|
24 | }
|
---|
25 | else {
|
---|
26 | System.out.println("Checking other line");
|
---|
27 | }
|
---|
28 | System.out.println("2.0: L2S: " + l2.getP1().x + " larger than L1S: " + l1.getP1().x);
|
---|
29 | }
|
---|
30 | //If l2 is starting after, then comparisons are based around l2.s
|
---|
31 | else if(l2.getP1().x > l1.getP1().x) {
|
---|
32 | System.out.println("2.1: Comparing L2S: " + l1.getP1().x + " less than L1E: " + l2.getP2().x);
|
---|
33 | if (l2.getP1().x < l1.getP2().x) {
|
---|
34 | //AND
|
---|
35 | System.out.println("2.2: Comparing L2S: " + l2.getP1().x + " larger than L1S: " + l1.getP1().x);
|
---|
36 | if (l2.getP1().x > l1.getP1().x) {
|
---|
37 | System.out.println("2: Success");
|
---|
38 | //IT IS INTERSECTED
|
---|
39 | //continue;
|
---|
40 | }
|
---|
41 | else {
|
---|
42 | //FAILED SECOND COMPARISON
|
---|
43 | System.out.println("2: Fail");
|
---|
44 | //return false;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | System.out.println("Failed second comparison RETURN FALSE");
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | //return false;
|
---|
52 | }
|
---|
53 | else{
|
---|
54 | System.out.println("NEITHER RETURN FALSE");
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | */
|
---|
58 |
|
---|
59 | import org.opencv.core.*;
|
---|
60 | import org.opencv.core.Point;
|
---|
61 |
|
---|
62 | import org.opencv.highgui.HighGui;
|
---|
63 | import org.opencv.imgcodecs.Imgcodecs;
|
---|
64 | import org.opencv.imgproc.Imgproc;
|
---|
65 | import org.opencv.imgproc.Moments;
|
---|
66 |
|
---|
67 | import static org.opencv.core.Core.FILLED;
|
---|
68 | import static org.opencv.core.CvType.CV_8UC3;
|
---|
69 | import static org.opencv.highgui.HighGui.createJFrame;
|
---|
70 | import static org.opencv.highgui.HighGui.imshow;
|
---|
71 | import static org.opencv.imgcodecs.Imgcodecs.imwrite;
|
---|
72 |
|
---|
73 | import java.io.File;
|
---|
74 | import java.lang.reflect.Array;
|
---|
75 | import java.util.ArrayList;
|
---|
76 |
|
---|
77 | //REFERENCES:
|
---|
78 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
|
---|
79 | //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
|
---|
80 | //https://www.wikihow.com/Calculate-Percentage-in-Java
|
---|
81 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
82 | //https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
|
---|
83 | //https://www.programiz.com/java-programming/examples/standard-deviation
|
---|
84 | //https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/
|
---|
85 | //https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556
|
---|
86 | //https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class
|
---|
87 | //https://stackoverflow.com/questions/37946482/crop-images-area-with-opencv-java
|
---|
88 | //https://docs.opencv.org/3.4/dd/dd7/tutorial_morph_lines_detection.html
|
---|
89 | //https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html
|
---|
90 | //https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/moments/moments.html
|
---|
91 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
92 | //http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html
|
---|
93 | //https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
|
---|
94 | //https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values
|
---|
95 | //https://stackoverflow.com/questions/18345969/how-to-get-the-mass-center-of-a-contour-android-opencv
|
---|
96 | //https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
|
---|
97 |
|
---|
98 |
|
---|
99 | //GOAL for 21st
|
---|
100 |
|
---|
101 |
|
---|
102 | //Classifier 01
|
---|
103 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
104 | //args can be parameters in algorthim such as threshold or theta?
|
---|
105 | //Run on 5000 images.
|
---|
106 | //Record success rates
|
---|
107 | //All done with makefile
|
---|
108 |
|
---|
109 |
|
---|
110 | //But first understand houghline transform
|
---|
111 | //Know what the algorithm being used is doing.
|
---|
112 | //MAke constants for this classifier
|
---|
113 | //Make java be able to run on CMD line
|
---|
114 |
|
---|
115 | public class MainMorph {
|
---|
116 | static public class Pair{
|
---|
117 | //Privates
|
---|
118 | private Boolean _b;
|
---|
119 | private Integer _i;
|
---|
120 |
|
---|
121 | //Constructor
|
---|
122 | public Pair(Boolean b, Integer i){
|
---|
123 | _b = b;
|
---|
124 | _i = i;
|
---|
125 | }
|
---|
126 | public Pair(){
|
---|
127 | _b = null;
|
---|
128 | _i = null;
|
---|
129 | }
|
---|
130 |
|
---|
131 | //Getters
|
---|
132 | public Boolean getBoolean() {return _b;}
|
---|
133 | public Integer getInteger() {return _i;}
|
---|
134 |
|
---|
135 | //Setters
|
---|
136 | public void setBoolean (Boolean b){_b = b;}
|
---|
137 | public void setInteger (Integer i){_i = i;}
|
---|
138 |
|
---|
139 | //ToString
|
---|
140 | public String toString() {return "Boolean: " + _b + " Integer: " + _i;}
|
---|
141 | }
|
---|
142 |
|
---|
143 | //CODE VERSIONS
|
---|
144 | static int CODE_VERSION = 9;
|
---|
145 | static int IMAGE_VERSION = 3;
|
---|
146 | //GLOBAL_CONSTANTS
|
---|
147 |
|
---|
148 | static double THRESHOLD_C = 4;
|
---|
149 | static double THRESHOLD_AREA_SIZE = 10000;
|
---|
150 | static double THRESHOLD_AREA_COUNT = 10;
|
---|
151 |
|
---|
152 | //
|
---|
153 |
|
---|
154 | private static void imageViewer(String winName, Mat img) {
|
---|
155 | try {
|
---|
156 | //Internal display - Overview - Will Distort High Res images
|
---|
157 | if(IMAGE_VERSION == 1) {
|
---|
158 | HighGui.namedWindow(winName, HighGui.WINDOW_NORMAL);
|
---|
159 | HighGui.resizeWindow(winName, 1000, 1000);
|
---|
160 | imshow(winName, img);
|
---|
161 |
|
---|
162 | HighGui.moveWindow(winName, 500, 0);
|
---|
163 | HighGui.waitKey(0);
|
---|
164 |
|
---|
165 | HighGui.destroyWindow(winName);
|
---|
166 | }
|
---|
167 | //Internal display - Segmented - Will _NOT_ Distort High Res images
|
---|
168 | if(IMAGE_VERSION == 2) {
|
---|
169 | HighGui.namedWindow(winName, HighGui.WINDOW_AUTOSIZE);
|
---|
170 | HighGui.resizeWindow(winName, 1000, 1000);
|
---|
171 | imshow(winName, img);
|
---|
172 | HighGui.moveWindow(winName, 500, 0);
|
---|
173 | HighGui.waitKey(0);
|
---|
174 |
|
---|
175 | HighGui.destroyWindow(winName);
|
---|
176 | }
|
---|
177 | //External display - Save Images for manual viewing
|
---|
178 | if(IMAGE_VERSION == 3) {
|
---|
179 | //save file (testing purposes)
|
---|
180 | imwrite(winName+".jpg", img);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | catch (Exception e){
|
---|
184 | e.printStackTrace();
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | //MAIN
|
---|
189 | public static void main(String[] args) {
|
---|
190 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
191 | try {
|
---|
192 | //Variables
|
---|
193 | System.out.println("Running Code version: " + CODE_VERSION + " Image Version: " +IMAGE_VERSION);
|
---|
194 | Mat mid = new Mat();
|
---|
195 | Mat edgesDetectedRGB = new Mat();
|
---|
196 | Mat clustersFoundRGB = new Mat();
|
---|
197 |
|
---|
198 | String testDirectory = "/Scratch/cpb16/is-sheet-music-encore/image-identification-dev-02/image-identification-development/";
|
---|
199 | String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
|
---|
200 | String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
|
---|
201 |
|
---|
202 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
203 | //mdp.39015097852365-2.png 176 lines Contents page.
|
---|
204 | //mdp.39015097852555-3.png 76 lines
|
---|
205 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
206 | //coo.31924062612282-9.png 8 lines
|
---|
207 | //String default_file = directory+"NotSheetMusic/coo.31924062612282-9.png";
|
---|
208 | //String default_file = directory+"NotSheetMusic/mdp.39015097852365-2.png";
|
---|
209 | //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY? DUE TO IMAGE LARGE, ZOOM IN
|
---|
210 | //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
|
---|
211 | //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
|
---|
212 | //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
|
---|
213 | //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png";
|
---|
214 | //String default_file =hiresDirectory+"MU/SheetMusic/bc.ark+=13960=t2j72dt1p-10.png";
|
---|
215 | //String default_file =hiresDirectory+"MU/SheetMusic/bc.ark+=13960=t2j72dt1p-7.png";
|
---|
216 | String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
|
---|
217 | //String default_file =hiresDirectory+"MU/NotSheetMusic/mdp.39015096363935-1.png";
|
---|
218 | //String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
|
---|
219 | //String default_file = "/Scratch/cpb16/is-sheet-music-encore/image-identification-terminal/TestImages/hi-res-test-coo.31924062612282-9.png";
|
---|
220 | //TestNew images used
|
---|
221 |
|
---|
222 | //String default_file = hiresDirectory+"/MU/SheetMusic/aeu.ark+=13960=t93787r1w-10.png";
|
---|
223 | //String default_file = "hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-10.png";
|
---|
224 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-7.png"; //Handwritten
|
---|
225 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-8.png"; //Handwritten
|
---|
226 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-9.png"; //Handwritten
|
---|
227 | //String default_file = hiresDirectory+"/MU/SheetMusic/coo.31924062612282-9.png";
|
---|
228 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-10.png";
|
---|
229 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-7.png";
|
---|
230 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-8.png";
|
---|
231 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-9.png";
|
---|
232 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-10.png";
|
---|
233 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
|
---|
234 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-2.png";
|
---|
235 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-3.png";
|
---|
236 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-4.png";
|
---|
237 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-5.png";
|
---|
238 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-6.png";
|
---|
239 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-7.png";
|
---|
240 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-8.png";
|
---|
241 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-9.png";
|
---|
242 |
|
---|
243 |
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 |
|
---|
256 |
|
---|
257 | //String default_file = testDirectory+"TestImages/MorphTester.png";
|
---|
258 | //String default_file = testDirectory+"TestImages/NotSheetMusic01.png";
|
---|
259 | //String default_file = testDirectory+"TestImages/NotSheetMusic02.png";
|
---|
260 | //String default_file = testDirectory+"TestImages/SheetMusic01.png";
|
---|
261 | //String default_file = testDirectory+"TestImages/SheetMusic02.png";
|
---|
262 | //String default_file = testDirectory+"TestImages/vLine.png";
|
---|
263 | String filename = ((args.length > 0) ? args[0] : default_file);
|
---|
264 | File file = new File(filename);
|
---|
265 | if(!file.exists()){System.err.println("Image not found: "+ filename);}
|
---|
266 |
|
---|
267 | int horizontalLineCount =0;
|
---|
268 |
|
---|
269 | // Load an image
|
---|
270 | Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
271 | System.out.println("Width: " + original1.width() + " Height: " + original1.height());
|
---|
272 | Mat original = original1.clone();
|
---|
273 |
|
---|
274 | Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
275 | //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
|
---|
276 | //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY);
|
---|
277 |
|
---|
278 |
|
---|
279 | //****************MORPHOLOGY****************************************************************************************
|
---|
280 | //ADDIOTIONAL FILTERING TO STOP STREAKS
|
---|
281 | //LOOK INTO STREAKS MORPHOGOLY.
|
---|
282 | //****************MORPHOLOGY****************************************************************************************
|
---|
283 |
|
---|
284 | // Create the images that will use to extract the horizontal and vertical lines
|
---|
285 |
|
---|
286 | //dynamic morphology??
|
---|
287 | if(CODE_VERSION == 1) {
|
---|
288 | int hori = original.width();
|
---|
289 | int vert = original.height();
|
---|
290 | //Find ratio between 100 and width and 100 and height
|
---|
291 | int divX = hori/10;
|
---|
292 | int divY = vert/10;
|
---|
293 | int sizeX = (hori/divX) * 10;
|
---|
294 | int sizeY = (vert/divY) * 10;
|
---|
295 |
|
---|
296 | Mat test = original.clone();
|
---|
297 | imageViewer("Original", test);
|
---|
298 |
|
---|
299 | System.out.println("hori: " + hori + '\t' + "vert: " + vert);
|
---|
300 | System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY);
|
---|
301 |
|
---|
302 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
|
---|
303 | Imgproc.erode(test,test,kernelErode);
|
---|
304 | imageViewer("01 Erode", test);
|
---|
305 |
|
---|
306 | Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
|
---|
307 | Imgproc.dilate(test, test, kernelDialate);
|
---|
308 | imageViewer("02 Dialate", test);
|
---|
309 |
|
---|
310 | Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
|
---|
311 | Imgproc.erode(test,test,kernelErodeAgain);
|
---|
312 | imageViewer(" 03 Erode Again", test);
|
---|
313 |
|
---|
314 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
|
---|
315 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
|
---|
316 | imageViewer("04 Close", test);
|
---|
317 |
|
---|
318 | Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
319 | imageViewer("05 Binarized", test);
|
---|
320 |
|
---|
321 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
|
---|
322 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
323 | imageViewer(" 06 Open", test);
|
---|
324 |
|
---|
325 | Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
|
---|
326 | Imgproc.dilate(test, test, kernelDialateAgain);
|
---|
327 | imageViewer("07 Dialate", test);
|
---|
328 |
|
---|
329 |
|
---|
330 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
|
---|
331 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
332 | imageViewer(" 08 Close Again (Final)", test);
|
---|
333 | }
|
---|
334 | //Successful hardcode for morhpology
|
---|
335 | if (CODE_VERSION == 2) {
|
---|
336 |
|
---|
337 | //MAKE SURE BLACK & WHITE
|
---|
338 | Mat test = original.clone();
|
---|
339 | imageViewer("00 Binarized Original", test);
|
---|
340 |
|
---|
341 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
|
---|
342 | Imgproc.erode(test,test,kernelErode);
|
---|
343 | imageViewer("01 Erode", test);
|
---|
344 |
|
---|
345 | Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
|
---|
346 | Imgproc.dilate(test, test, kernelDialate);
|
---|
347 | imageViewer("02 Dialate", test);
|
---|
348 |
|
---|
349 | Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
|
---|
350 | Imgproc.erode(test,test,kernelErodeAgain);
|
---|
351 | imageViewer(" 03 Erode Again", test);
|
---|
352 |
|
---|
353 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
|
---|
354 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
|
---|
355 | imageViewer("04 Close", test);
|
---|
356 |
|
---|
357 | // Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
|
---|
358 | // imageViewer("05 Binarized", test);
|
---|
359 |
|
---|
360 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
361 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
362 | imageViewer(" 06 Open", test);
|
---|
363 |
|
---|
364 | // Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
|
---|
365 | // Imgproc.dilate(test, test, kernelDialateAgain);
|
---|
366 | // imageViewer("07 Dialate", test);
|
---|
367 |
|
---|
368 | //FIGURE OUT FLOOD FILL!!
|
---|
369 | Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2));
|
---|
370 |
|
---|
371 |
|
---|
372 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
|
---|
373 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
374 | imageViewer(" 08 Close Again (Final)", test);
|
---|
375 |
|
---|
376 | }
|
---|
377 | //Tutorial/Demo Code
|
---|
378 | if (CODE_VERSION == 3) {
|
---|
379 | Mat horizontal = original.clone();
|
---|
380 | Mat vertical = original.clone();
|
---|
381 | // Specify size on horizontal axis
|
---|
382 | int horizontal_size = horizontal.cols() / 50;
|
---|
383 | // Create structure element for extracting horizontal lines through morphology operations
|
---|
384 | Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2));
|
---|
385 | // Apply morphology operations
|
---|
386 | Imgproc.erode(horizontal, horizontal, horizontalStructure);
|
---|
387 | Imgproc.dilate(horizontal, horizontal, horizontalStructure);
|
---|
388 | // Show extracted horizontal lines
|
---|
389 | imageViewer("horizontal", horizontal);
|
---|
390 | // Specify size on vertical axis
|
---|
391 | int vertical_size = vertical.rows() / 30;
|
---|
392 | // Create structure element for extracting vertical lines through morphology operations
|
---|
393 | Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size));
|
---|
394 | // Apply morphology operations
|
---|
395 | Imgproc.erode(vertical, vertical, verticalStructure);
|
---|
396 | Imgproc.dilate(vertical, vertical, verticalStructure);
|
---|
397 | // Show extracted vertical lines
|
---|
398 | imageViewer("vertical", vertical);
|
---|
399 | // Inverse vertical image
|
---|
400 | Core.bitwise_not(vertical, vertical);
|
---|
401 | imageViewer("vertical_bit", vertical);
|
---|
402 | // Extract edges and smooth image according to the logic
|
---|
403 | // 1. extract edges
|
---|
404 | // 2. dilate(edges)
|
---|
405 | // 3. src.copyTo(smooth)
|
---|
406 | // 4. blur smooth img
|
---|
407 | // 5. smooth.copyTo(src, edges)
|
---|
408 | // Step 1
|
---|
409 | Mat edges = new Mat();
|
---|
410 | Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
|
---|
411 | imageViewer("edges", edges);
|
---|
412 | // Step 2
|
---|
413 | Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
|
---|
414 | Imgproc.dilate(edges, edges, kernel);
|
---|
415 | imageViewer("dilate", edges);
|
---|
416 | // Step 3
|
---|
417 | Mat smooth = new Mat();
|
---|
418 | vertical.copyTo(smooth);
|
---|
419 | // Step 4
|
---|
420 | Imgproc.blur(smooth, smooth, new Size(2, 2));
|
---|
421 | // Step 5
|
---|
422 | smooth.copyTo(vertical, edges);
|
---|
423 | // Show final result
|
---|
424 | imageViewer("smooth - final", vertical);
|
---|
425 | System.exit(0);
|
---|
426 | }
|
---|
427 | //Better morphology attempt - static
|
---|
428 | if(CODE_VERSION == 4) {
|
---|
429 |
|
---|
430 | //Display Original
|
---|
431 | imageViewer("original", original1);
|
---|
432 |
|
---|
433 | Mat test = original.clone();
|
---|
434 | Mat pre = original.clone();
|
---|
435 | Mat dst = new Mat();
|
---|
436 |
|
---|
437 | imageViewer("00 Inverse Binarized Original", test);
|
---|
438 |
|
---|
439 | //remove large items of no interest pre proc
|
---|
440 | //denoize
|
---|
441 | //heal
|
---|
442 | //fnd large images, write to a seperate mat.
|
---|
443 | //draw these onto orignal image(binerized) in red
|
---|
444 | //turn all red pixels in image to black
|
---|
445 |
|
---|
446 |
|
---|
447 | //denoize
|
---|
448 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
|
---|
449 | Imgproc.morphologyEx(pre,pre, Imgproc.MORPH_OPEN, denoize);
|
---|
450 | imageViewer("Denoize - PRE", pre);
|
---|
451 |
|
---|
452 | //close up gaps
|
---|
453 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
454 | Imgproc.morphologyEx(pre,pre,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
455 | imageViewer("gap closer - PRE", pre);
|
---|
456 |
|
---|
457 | Mat kernelHighlightLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));
|
---|
458 | Imgproc.erode(pre,pre, kernelHighlightLarge);
|
---|
459 | imageViewer("Highlight Large - PRE", pre);
|
---|
460 |
|
---|
461 | //change white pixels to red
|
---|
462 | ArrayList<MatOfPoint> contoursPre = new ArrayList<MatOfPoint>();
|
---|
463 | Mat hierarchyPre = new Mat();
|
---|
464 |
|
---|
465 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
466 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
467 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
468 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
469 |
|
---|
470 | Imgproc.findContours(pre, contoursPre, hierarchyPre, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
471 |
|
---|
472 | //Draw contours and record areas
|
---|
473 |
|
---|
474 | Mat drawingPre = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
475 | Mat mask = new Mat(drawingPre.size(), CV_8UC3,new Scalar(0));
|
---|
476 | Scalar colorPre = new Scalar(255, 255, 255);
|
---|
477 | Imgproc.drawContours(mask, contoursPre, -1, new Scalar(255, 255, 255), FILLED);
|
---|
478 | Imgproc.fillPoly(drawingPre, contoursPre,colorPre);
|
---|
479 | imageViewer("DRAWINGPRE", drawingPre);
|
---|
480 |
|
---|
481 |
|
---|
482 | //FIIIIIIIIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXX
|
---|
483 | //Remove from main Mat
|
---|
484 | Core.bitwise_not(mask,mask);
|
---|
485 | imageViewer("MASK", mask);
|
---|
486 | imageViewer("TEST", test);
|
---|
487 |
|
---|
488 | drawingPre.copyTo(test, mask);
|
---|
489 | imageViewer("COMBINE", test);
|
---|
490 |
|
---|
491 | //start staff line detection
|
---|
492 |
|
---|
493 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
|
---|
494 | Imgproc.erode(test,test,kernelErode);
|
---|
495 | imageViewer("01 Erode plus pre", test);
|
---|
496 |
|
---|
497 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
|
---|
498 | Imgproc.dilate(test,test,kernelDilate);
|
---|
499 | imageViewer("02 Dilate", test);
|
---|
500 |
|
---|
501 | Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
502 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
|
---|
503 | imageViewer("03 Open", test);
|
---|
504 |
|
---|
505 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
|
---|
506 | Imgproc.erode(test,test,kernelErode02);
|
---|
507 | imageViewer("04 Erode (Final)", test);
|
---|
508 |
|
---|
509 |
|
---|
510 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
511 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
512 | Mat hierarchy = new Mat();
|
---|
513 |
|
---|
514 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
515 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
516 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
517 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
518 |
|
---|
519 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
520 |
|
---|
521 | //Draw contours and record areas
|
---|
522 | Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
523 | Mat drawing2 = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
524 | int areaCounter = 0;
|
---|
525 | for (int i = 0; i < contours.size(); i++) {
|
---|
526 | double area = Imgproc.contourArea(contours.get(i));
|
---|
527 | if(area > THRESHOLD_AREA_SIZE ) {
|
---|
528 | areaCounter++;
|
---|
529 | Scalar color = new Scalar(0, 0, 255);
|
---|
530 | Imgproc.drawContours(drawing, contours, i, color, 1);
|
---|
531 | System.out.println("AREA: " + area);
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | //Classifier Calculation
|
---|
536 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
537 | System.out.println("THIS IS SHEET MUSIC");
|
---|
538 | System.out.println(areaCounter);
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | //Show in a window
|
---|
543 | imageViewer("Contours", drawing);
|
---|
544 | }
|
---|
545 | //Better morphology attempt - dynamic
|
---|
546 | if(CODE_VERSION == 5) {
|
---|
547 | int hori = original.width();
|
---|
548 | int vert = original.height();
|
---|
549 | //Find ratio between 100 and width and 100 and height
|
---|
550 | int sizeX100 = 10 * (hori/68);
|
---|
551 | int sizeY100 = 10 * (vert/46);
|
---|
552 | int sizeX10 = (hori/68);
|
---|
553 | int sizeY10 = (vert/46);
|
---|
554 | int sizeX1 = (hori/68)/10;
|
---|
555 | int sizeY1 = (vert/46)/10;
|
---|
556 |
|
---|
557 | //SizeX should always be a 68th * 10. Based off the defualt tester image "coo.*"
|
---|
558 | //SizeT should always be a 46th * 10
|
---|
559 |
|
---|
560 | System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1);
|
---|
561 | //Display Original
|
---|
562 | imageViewer("original", original1);
|
---|
563 |
|
---|
564 | Mat test = original.clone();
|
---|
565 | imageViewer("00 Inverse Binarized Original", test);
|
---|
566 |
|
---|
567 | //Remove very large and wide black spaces (8th of the page)
|
---|
568 | //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8));
|
---|
569 | //Imgproc.erode(test,test, kernelRemoveLarge);
|
---|
570 | //imageViewer("Remove Large", test);
|
---|
571 |
|
---|
572 | //Eliminate things that are not long and thin
|
---|
573 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
|
---|
574 | Imgproc.erode(test,test,kernelErode);
|
---|
575 | imageViewer("01 Erode", test);
|
---|
576 |
|
---|
577 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
|
---|
578 | Imgproc.dilate(test,test,kernelDilate);
|
---|
579 | imageViewer("02 Dilate", test);
|
---|
580 |
|
---|
581 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
|
---|
582 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
|
---|
583 | imageViewer("03 Open", test);
|
---|
584 |
|
---|
585 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
|
---|
586 | Imgproc.erode(test,test,kernelErode02);
|
---|
587 | imageViewer("04 Erode (Final)", test);
|
---|
588 |
|
---|
589 |
|
---|
590 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
591 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
592 | Mat hierarchy = new Mat();
|
---|
593 |
|
---|
594 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
595 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
596 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
597 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
598 |
|
---|
599 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
600 |
|
---|
601 | //Draw contours and record areas
|
---|
602 | Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
603 | int areaCounter = 0;
|
---|
604 | for (int i = 0; i < contours.size(); i++) {
|
---|
605 | double area = Imgproc.contourArea(contours.get(i));
|
---|
606 | if(area > THRESHOLD_AREA_SIZE ) {
|
---|
607 | areaCounter++;
|
---|
608 | Scalar color = new Scalar(0, 0, 255);
|
---|
609 | Imgproc.drawContours(drawing, contours, i, color, 1);
|
---|
610 | System.out.println("AREA: " + area);
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | //Classifier Calculation
|
---|
615 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
616 | System.out.println("THIS IS SHEET MUSIC");
|
---|
617 | System.out.println(areaCounter);
|
---|
618 | }
|
---|
619 |
|
---|
620 |
|
---|
621 | //Show in a window
|
---|
622 | imageViewer("Contours", drawing);
|
---|
623 | }
|
---|
624 | //MASK UNDERSTANDING
|
---|
625 | if(CODE_VERSION == 6) {
|
---|
626 | String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
|
---|
627 | Mat mask = new Mat();
|
---|
628 | Mat dst = new Mat();
|
---|
629 | //Get source image and binerize
|
---|
630 | Mat src = Imgcodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
631 | Imgproc.adaptiveThreshold(original1, src,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
632 | imageViewer("src", src);
|
---|
633 |
|
---|
634 | //Find unwanted material, then invert it so mask removes not keeps.
|
---|
635 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
|
---|
636 | Imgproc.erode(src,mask,kernelErode);
|
---|
637 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
|
---|
638 | Imgproc.dilate(mask, mask, kernelDilate);
|
---|
639 | Core.bitwise_not(mask,mask);
|
---|
640 | imageViewer("mask", mask);
|
---|
641 |
|
---|
642 | //Copy source image to new Mat, with mask in use
|
---|
643 | src.copyTo(dst, mask);
|
---|
644 | imageViewer("dst", dst);
|
---|
645 |
|
---|
646 |
|
---|
647 |
|
---|
648 |
|
---|
649 | }
|
---|
650 | //Mask implementation
|
---|
651 | if(CODE_VERSION == 7) {
|
---|
652 |
|
---|
653 | //Display Original
|
---|
654 | imageViewer("original", original1);
|
---|
655 |
|
---|
656 | Mat src = original.clone();
|
---|
657 | Mat test = original.clone();
|
---|
658 | Mat mask = new Mat();
|
---|
659 | Mat dst = new Mat();
|
---|
660 |
|
---|
661 | imageViewer("00 Inverse Binarized Original", src);
|
---|
662 |
|
---|
663 | //denoize
|
---|
664 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
|
---|
665 | Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
|
---|
666 | imageViewer("01 Denoize - mask", mask);
|
---|
667 |
|
---|
668 | //close up gaps
|
---|
669 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
670 | Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
671 | imageViewer("02 gap closer - mask", mask);
|
---|
672 |
|
---|
673 | //Isolate large items
|
---|
674 | Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
|
---|
675 | Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
|
---|
676 | imageViewer("03 Isolate Large - mask", mask);
|
---|
677 | Core.bitwise_not(mask,mask);
|
---|
678 |
|
---|
679 | //Remove unwanted large items from image
|
---|
680 | src.copyTo(dst, mask);
|
---|
681 | imageViewer("04 Large Items Removed", dst);
|
---|
682 |
|
---|
683 | //start staff line detection
|
---|
684 |
|
---|
685 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
|
---|
686 | Imgproc.erode(dst,test,kernelErode);
|
---|
687 | imageViewer("11 Erode plus pre", test);
|
---|
688 |
|
---|
689 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
|
---|
690 | Imgproc.dilate(test,test,kernelDilate);
|
---|
691 | imageViewer("12 Dilate", test);
|
---|
692 |
|
---|
693 | Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
694 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
|
---|
695 | imageViewer("13 Open", test);
|
---|
696 |
|
---|
697 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
|
---|
698 | Imgproc.erode(test,test,kernelErode02);
|
---|
699 | imageViewer("14 Erode (Final)", test);
|
---|
700 |
|
---|
701 |
|
---|
702 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
703 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
704 | Mat hierarchy = new Mat();
|
---|
705 |
|
---|
706 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
707 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
708 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
709 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
710 |
|
---|
711 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
712 |
|
---|
713 | //Draw contours and record areas
|
---|
714 | Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
715 | int areaCounter = 0;
|
---|
716 |
|
---|
717 |
|
---|
718 | Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), FILLED);
|
---|
719 | // for (int i = 0; i < contours.size(); i++) {
|
---|
720 | // Scalar color = new Scalar(0, i, i);
|
---|
721 | // double area = Imgproc.contourArea(contours.get(i));
|
---|
722 | // Imgproc.drawContours(drawing, contours, i, color, FILLED);
|
---|
723 | // System.out.println("AREA: " + area);
|
---|
724 | //
|
---|
725 | // }
|
---|
726 | imageViewer("Contours found", drawing);
|
---|
727 |
|
---|
728 | //Classifier Calculation
|
---|
729 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
730 | System.out.println("THIS IS SHEET MUSIC");
|
---|
731 | System.out.println(areaCounter);
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | }
|
---|
736 | //Mask implementations (LARGE AND SMALL) - HIGH RES NUMBER MOD
|
---|
737 | if(CODE_VERSION == 8) {
|
---|
738 |
|
---|
739 | //Display Original
|
---|
740 | imageViewer("original", original1);
|
---|
741 |
|
---|
742 | Mat test = original.clone();
|
---|
743 |
|
---|
744 |
|
---|
745 | imageViewer("00 Inverse Binarized Original", test);
|
---|
746 |
|
---|
747 |
|
---|
748 | //************************************
|
---|
749 | //Large Object Removal
|
---|
750 | //************************************
|
---|
751 | Mat srcLOR = original.clone();
|
---|
752 | Mat maskLOR = new Mat();
|
---|
753 | Mat dstLOR = new Mat();
|
---|
754 |
|
---|
755 | //denoize
|
---|
756 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
757 | Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
|
---|
758 | imageViewer("01 Denoize - mask", maskLOR);
|
---|
759 |
|
---|
760 | //close up gaps
|
---|
761 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
762 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
763 | imageViewer("02 gap closer - mask", maskLOR);
|
---|
764 |
|
---|
765 | //Isolate large items
|
---|
766 | Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
|
---|
767 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
|
---|
768 | imageViewer("03 Isolate Large - mask", maskLOR);
|
---|
769 |
|
---|
770 | //Remove large items from image
|
---|
771 | Core.bitwise_not(maskLOR,maskLOR);
|
---|
772 | srcLOR.copyTo(dstLOR, maskLOR);
|
---|
773 | imageViewer("04 Large Items Removed", dstLOR);
|
---|
774 |
|
---|
775 | //****************************************
|
---|
776 | //Small object removal (SOR)
|
---|
777 | //****************************************
|
---|
778 |
|
---|
779 | Mat srcSOR = dstLOR.clone();
|
---|
780 | Mat maskSOR = new Mat();
|
---|
781 | Mat dstSOR = new Mat();
|
---|
782 |
|
---|
783 | Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
784 | Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
|
---|
785 | imageViewer("11 show small - mask", maskSOR);
|
---|
786 |
|
---|
787 | Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
788 | Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
|
---|
789 | imageViewer("12 highlight small - mask", maskSOR);
|
---|
790 |
|
---|
791 | /* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
|
---|
792 | Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
|
---|
793 | imageViewer("13 isolate small - mask", maskSOR);
|
---|
794 | */
|
---|
795 |
|
---|
796 | //Remove small items from image
|
---|
797 | Core.bitwise_not(maskSOR, maskSOR);
|
---|
798 | srcSOR.copyTo(dstSOR, maskSOR);
|
---|
799 | imageViewer("14 Small Items Removed", dstSOR);
|
---|
800 |
|
---|
801 |
|
---|
802 | //****************************************
|
---|
803 | //start staff line detection
|
---|
804 | //****************************************
|
---|
805 |
|
---|
806 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
|
---|
807 | Imgproc.erode(dstSOR,test,kernelErode);
|
---|
808 | imageViewer("21 Erode plus pre", test);
|
---|
809 |
|
---|
810 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
|
---|
811 | Imgproc.dilate(test,test,kernelDilate);
|
---|
812 | imageViewer("22 Dilate", test);
|
---|
813 |
|
---|
814 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
815 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
|
---|
816 | imageViewer("23 Close", test);
|
---|
817 |
|
---|
818 |
|
---|
819 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
|
---|
820 | Imgproc.erode(test,test,kernelErode02);
|
---|
821 | imageViewer("24 Erode (Final)", test);
|
---|
822 |
|
---|
823 | //********************************************************************************
|
---|
824 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
825 | //********************************************************************************
|
---|
826 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
827 | ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
|
---|
828 | ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
|
---|
829 | Mat hierarchy = new Mat();
|
---|
830 |
|
---|
831 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
832 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
833 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
834 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
835 |
|
---|
836 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
837 |
|
---|
838 | System.out.println(contours.size());
|
---|
839 | //Draw contours and record areas
|
---|
840 | Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
841 | Mat largeContoursFound = allContoursFound.clone() ;
|
---|
842 | Mat postContoursFound = allContoursFound.clone();
|
---|
843 | int areaCounter = 0;
|
---|
844 |
|
---|
845 | //Have created a preprocess to remove large objects.
|
---|
846 | //Need to now finalized Classifier, re try area detection.
|
---|
847 | //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
|
---|
848 | //Just use area and periemter to determine if sheet music
|
---|
849 | //Discuss with david before weekend perhaps?
|
---|
850 |
|
---|
851 | Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
|
---|
852 | for (int i = 0; i < contours.size(); i++) {
|
---|
853 | double area = Imgproc.contourArea(contours.get(i));
|
---|
854 | if(area > 100) {
|
---|
855 | System.out.println("AREA: " + area);
|
---|
856 | Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
|
---|
857 | //create list of large coutours found
|
---|
858 | largeContours.add(contours.get(i));
|
---|
859 | }
|
---|
860 | }
|
---|
861 | imageViewer("80 All Contours found", allContoursFound);
|
---|
862 | imageViewer("81 Large Contours Found", largeContoursFound);
|
---|
863 |
|
---|
864 | //*****************************************************************
|
---|
865 | //Circles on processed images
|
---|
866 | //*****************************************************************
|
---|
867 | // //Init arrays
|
---|
868 | // Mat circleOutput = allContoursFound.clone();
|
---|
869 | // MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()];
|
---|
870 | // Point[] centers = new Point[largeContours.size()];
|
---|
871 | // float[][] radius = new float[largeContours.size()][1];
|
---|
872 | //
|
---|
873 | // //Fill arrays
|
---|
874 | // for (int i = 0; i < largeContours.size(); i++) {
|
---|
875 | // contoursPoly[i] = new MatOfPoint2f();
|
---|
876 | // Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
|
---|
877 | // centers[i] = new Point();
|
---|
878 | // Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
|
---|
879 | //
|
---|
880 | // }
|
---|
881 | // //Draw circle for each large contour
|
---|
882 | // for (int i = 0; i < largeContours.size(); i++) {
|
---|
883 | // Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
|
---|
884 | // }
|
---|
885 | // imageViewer("82 Circles found", circleOutput);
|
---|
886 |
|
---|
887 | //********************************************************************************
|
---|
888 | //Centroids -Everything must be to scale
|
---|
889 | //********************************************************************************
|
---|
890 |
|
---|
891 | ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
|
---|
892 | Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
|
---|
893 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
894 | mu.add(i, Imgproc.moments(largeContours.get(i), false));
|
---|
895 | Moments p = mu.get(i);
|
---|
896 | int x = (int) (p.get_m10() / p.get_m00());
|
---|
897 | int y = (int) (p.get_m01() / p.get_m00());
|
---|
898 | Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
|
---|
899 | }
|
---|
900 | imageViewer("83 Centres found", centreOutput);
|
---|
901 |
|
---|
902 |
|
---|
903 | //***********************************************
|
---|
904 | //PostProcessing - Morphology Classifier
|
---|
905 | // Use dilation to "Connect the dots"
|
---|
906 | // Testing showed the centroids were clustered together
|
---|
907 | // Then use area or perimeter as a classifier filter
|
---|
908 | //REFINEREFINEREIFEN
|
---|
909 | //REFINEREFINEREIFEN
|
---|
910 | //REFINEREFINEREIFEN
|
---|
911 | //REFINEREFINEREIFEN
|
---|
912 | //REFINEREFINEREIFEN
|
---|
913 | //REFINEREFINEREIFEN
|
---|
914 | // FIX UP CLASSIFIER COMPARISON.
|
---|
915 | // MORPHOLOGIES FUNCTIONS RETURN NULL AS THEIR RETURN VARIABLES. WHY?
|
---|
916 | //REFINEREFINEREIFEN
|
---|
917 | //REFINEREFINEREIFEN
|
---|
918 | //REFINEREFINEREIFEN
|
---|
919 | //REFINEREFINEREIFEN
|
---|
920 | //REFINEREFINEREIFEN
|
---|
921 | //***********************************************
|
---|
922 |
|
---|
923 | Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
|
---|
924 | Imgproc.dilate(centreOutput,centreOutput,postDilate);
|
---|
925 | imageViewer("91 PostDilated", centreOutput);
|
---|
926 |
|
---|
927 | Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
928 | Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
|
---|
929 | imageViewer("92 PostClose", centreOutput);
|
---|
930 |
|
---|
931 | Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
|
---|
932 | Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
|
---|
933 | imageViewer("93 PostDenoize", centreOutput);
|
---|
934 |
|
---|
935 | //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50));
|
---|
936 | //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline);
|
---|
937 |
|
---|
938 |
|
---|
939 |
|
---|
940 | //Find area
|
---|
941 | Mat centreOutputGrey = new Mat();
|
---|
942 | Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
|
---|
943 | Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
944 |
|
---|
945 | for (int i = 0; i < postContours.size(); i++) {
|
---|
946 | double area = Imgproc.contourArea(postContours.get(i));
|
---|
947 | if(area > THRESHOLD_AREA_SIZE) {
|
---|
948 | System.out.println("POST AREA: " + area);
|
---|
949 | Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
|
---|
950 | areaCounter++;
|
---|
951 | }
|
---|
952 | }
|
---|
953 |
|
---|
954 |
|
---|
955 |
|
---|
956 | imageViewer("93 PostEND", postContoursFound);
|
---|
957 |
|
---|
958 | //Classifier Calculation
|
---|
959 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
960 | System.out.println("THIS IS SHEET MUSIC");
|
---|
961 | System.out.println(areaCounter);
|
---|
962 | }
|
---|
963 |
|
---|
964 |
|
---|
965 | }
|
---|
966 | //ClassifierComparison - Using code version 8
|
---|
967 | if(CODE_VERSION == 81) {
|
---|
968 |
|
---|
969 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
970 | Pair returnVariables = new Pair();
|
---|
971 | int FILLED = -1;
|
---|
972 | try{
|
---|
973 | //Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
974 | //System.out.println("Width: " + original1.width() + " Height: " + original1.height());
|
---|
975 | //Mat original = original1.clone();
|
---|
976 | //Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
977 |
|
---|
978 | Mat test = original.clone();
|
---|
979 | //************************************
|
---|
980 | //Large Object Removal
|
---|
981 | //************************************
|
---|
982 | Mat srcLOR = original.clone();
|
---|
983 | Mat maskLOR = new Mat();
|
---|
984 | Mat dstLOR = new Mat();
|
---|
985 |
|
---|
986 | //denoize
|
---|
987 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
988 | Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
|
---|
989 |
|
---|
990 | //close up gaps
|
---|
991 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
992 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
993 |
|
---|
994 | //Isolate large items
|
---|
995 | Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
|
---|
996 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
|
---|
997 |
|
---|
998 | //Remove large items from image
|
---|
999 | Core.bitwise_not(maskLOR,maskLOR);
|
---|
1000 | srcLOR.copyTo(dstLOR, maskLOR);
|
---|
1001 |
|
---|
1002 | //****************************************
|
---|
1003 | //Small object removal (SOR)
|
---|
1004 | //****************************************
|
---|
1005 |
|
---|
1006 | Mat srcSOR = dstLOR.clone();
|
---|
1007 | Mat maskSOR = new Mat();
|
---|
1008 | Mat dstSOR = new Mat();
|
---|
1009 |
|
---|
1010 | Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
1011 | Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
|
---|
1012 |
|
---|
1013 | Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
1014 | Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
|
---|
1015 |
|
---|
1016 |
|
---|
1017 | /* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
|
---|
1018 | Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
|
---|
1019 | imageViewer("13 isolate small - mask", maskSOR);
|
---|
1020 | */
|
---|
1021 |
|
---|
1022 | //Remove small items from image
|
---|
1023 | Core.bitwise_not(maskSOR, maskSOR);
|
---|
1024 | srcSOR.copyTo(dstSOR, maskSOR);
|
---|
1025 |
|
---|
1026 |
|
---|
1027 | //****************************************
|
---|
1028 | //start staff line detection
|
---|
1029 | //****************************************
|
---|
1030 |
|
---|
1031 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
|
---|
1032 | Imgproc.erode(dstSOR,test,kernelErode);
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
|
---|
1036 | Imgproc.dilate(test,test,kernelDilate);
|
---|
1037 |
|
---|
1038 |
|
---|
1039 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
1040 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
|
---|
1041 |
|
---|
1042 |
|
---|
1043 |
|
---|
1044 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
|
---|
1045 | Imgproc.erode(test,test,kernelErode02);
|
---|
1046 |
|
---|
1047 |
|
---|
1048 | //********************************************************************************
|
---|
1049 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
1050 | //********************************************************************************
|
---|
1051 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
1052 | ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
|
---|
1053 | ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
|
---|
1054 | Mat hierarchy = new Mat();
|
---|
1055 |
|
---|
1056 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
1057 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
1058 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
1059 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
1060 |
|
---|
1061 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
1062 |
|
---|
1063 | System.out.println(contours.size());
|
---|
1064 | //Draw contours and record areas
|
---|
1065 | Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
1066 | Mat largeContoursFound = allContoursFound.clone() ;
|
---|
1067 | Mat postContoursFound = allContoursFound.clone();
|
---|
1068 | int areaCounter = 0;
|
---|
1069 |
|
---|
1070 | //Have created a preprocess to remove large objects.
|
---|
1071 | //Need to now finalized Classifier, re try area detection.
|
---|
1072 | //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
|
---|
1073 | //Just use area and periemter to determine if sheet music
|
---|
1074 | //Discuss with david before weekend perhaps?
|
---|
1075 |
|
---|
1076 | Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
|
---|
1077 | for (int i = 0; i < contours.size(); i++) {
|
---|
1078 | double area = Imgproc.contourArea(contours.get(i));
|
---|
1079 | if(area > 100) {
|
---|
1080 | //System.out.println("AREA: " + area);
|
---|
1081 | Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
|
---|
1082 | //create list of large coutours found
|
---|
1083 | largeContours.add(contours.get(i));
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | //*****************************************************************
|
---|
1088 | //Circles and centres on processed images
|
---|
1089 | //*****************************************************************
|
---|
1090 |
|
---|
1091 | //Init arrays
|
---|
1092 | Mat circleOutput = allContoursFound.clone();
|
---|
1093 | MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()];
|
---|
1094 | Point[] centers = new Point[largeContours.size()];
|
---|
1095 | float[][] radius = new float[largeContours.size()][1];
|
---|
1096 |
|
---|
1097 | //Fill arrays
|
---|
1098 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
1099 | contoursPoly[i] = new MatOfPoint2f();
|
---|
1100 | Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
|
---|
1101 | centers[i] = new Point();
|
---|
1102 | Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
|
---|
1103 |
|
---|
1104 | }
|
---|
1105 | //Draw circle for each large contour
|
---|
1106 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
1107 | Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | //********************************************************************************
|
---|
1112 | //Centroids - Everything must be to scale
|
---|
1113 | //********************************************************************************
|
---|
1114 |
|
---|
1115 | ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
|
---|
1116 | Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
|
---|
1117 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
1118 | mu.add(i, Imgproc.moments(largeContours.get(i), false));
|
---|
1119 | Moments p = mu.get(i);
|
---|
1120 | int x = (int) (p.get_m10() / p.get_m00());
|
---|
1121 | int y = (int) (p.get_m01() / p.get_m00());
|
---|
1122 | Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | //***********************************************
|
---|
1126 | //PostProcessing - Morphology Classifier
|
---|
1127 | // Use dilation to "Connect the dots"
|
---|
1128 | // Testing showed the centroids were clustered together
|
---|
1129 | // Then use area or perimeter as a classifier filter
|
---|
1130 | //***********************************************
|
---|
1131 |
|
---|
1132 | Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
|
---|
1133 | Imgproc.dilate(centreOutput,centreOutput,postDilate);
|
---|
1134 |
|
---|
1135 | Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
1136 | Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
|
---|
1137 |
|
---|
1138 | Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
|
---|
1139 | Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
|
---|
1140 |
|
---|
1141 | //Find area
|
---|
1142 | Mat centreOutputGrey = new Mat();
|
---|
1143 | Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
|
---|
1144 | Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
1145 |
|
---|
1146 | for (int i = 0; i < postContours.size(); i++) {
|
---|
1147 | double area = Imgproc.contourArea(postContours.get(i));
|
---|
1148 | if(area > THRESHOLD_AREA_SIZE) {
|
---|
1149 | //System.err.println("POST AREA: " + area);
|
---|
1150 | Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
|
---|
1151 | areaCounter++;
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 | //Classifier Calculation
|
---|
1155 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
1156 | returnVariables.setBoolean(true);
|
---|
1157 | returnVariables.setInteger(areaCounter);
|
---|
1158 | }
|
---|
1159 | }
|
---|
1160 | catch(Exception e){
|
---|
1161 | System.err.println(e);
|
---|
1162 | }
|
---|
1163 | //return returnVariables;
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | //Reset after david chat
|
---|
1167 | if(CODE_VERSION == 9){
|
---|
1168 | //1. Remove large items (needs to be larger kernel, should not remove bar lines)
|
---|
1169 | //2. Remove text
|
---|
1170 | //3. Standard plan from presentation
|
---|
1171 | //4. Classify like line clusters.
|
---|
1172 | }
|
---|
1173 | //USE stuc element, to rule out large wide and long pieces of black and white.
|
---|
1174 |
|
---|
1175 |
|
---|
1176 | //****************MORPHOLOGY****************************************************************************************
|
---|
1177 |
|
---|
1178 | //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
1179 |
|
---|
1180 | //Display Results
|
---|
1181 | //HighGui.imshow("Source", original);
|
---|
1182 | //HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
1183 |
|
---|
1184 |
|
---|
1185 | //imshow("LINESFOUND", edgesDetectedRGB);
|
---|
1186 | //HighGui.resizeWindow("LINESFOUND", 1000,1000);
|
---|
1187 |
|
---|
1188 | //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
|
---|
1189 | //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
|
---|
1190 |
|
---|
1191 | //COUNT OF LINES CLASSIFICATION
|
---|
1192 | //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
|
---|
1193 | //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
|
---|
1194 | //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
|
---|
1195 |
|
---|
1196 | //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
|
---|
1197 |
|
---|
1198 | // Wait and Exit
|
---|
1199 | //HighGui.waitKey();
|
---|
1200 | System.exit(0);
|
---|
1201 | }
|
---|
1202 | catch(Exception e){
|
---|
1203 | System.err.println(e);
|
---|
1204 | }
|
---|
1205 | }
|
---|
1206 | }
|
---|