source: other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java@ 33589

Last change on this file since 33589 was 33589, checked in by cpb16, 5 years ago

final01. Need Map results still

File size: 76.2 KB
Line 
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
59import org.opencv.core.*;
60import org.opencv.core.Point;
61
62import org.opencv.highgui.HighGui;
63import org.opencv.imgcodecs.Imgcodecs;
64import org.opencv.imgproc.Imgproc;
65import org.opencv.imgproc.Moments;
66
67import static org.opencv.core.Core.FILLED;
68import static org.opencv.core.Core.calcCovarMatrix;
69import static org.opencv.core.CvType.CV_8UC3;
70import static org.opencv.highgui.HighGui.createJFrame;
71import static org.opencv.highgui.HighGui.imshow;
72import static org.opencv.imgcodecs.Imgcodecs.imread;
73import static org.opencv.imgcodecs.Imgcodecs.imwrite;
74
75import java.io.File;
76import java.lang.reflect.Array;
77import java.util.ArrayList;
78
79//REFERENCES:
80//https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
81//https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
82//https://www.wikihow.com/Calculate-Percentage-in-Java
83//https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
84//https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
85//https://www.programiz.com/java-programming/examples/standard-deviation
86//https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/
87//https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556
88//https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class
89//https://stackoverflow.com/questions/37946482/crop-images-area-with-opencv-java
90//https://docs.opencv.org/3.4/dd/dd7/tutorial_morph_lines_detection.html
91//https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html
92//https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/moments/moments.html
93//https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
94//http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html
95//https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
96//https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values
97//https://stackoverflow.com/questions/18345969/how-to-get-the-mass-center-of-a-contour-android-opencv
98//https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
99
100
101//GOAL for 21st
102
103
104//Classifier 01
105//Have args so can call "java image-identification-classifier01 XX XX"
106//args can be parameters in algorthim such as threshold or theta?
107//Run on 5000 images.
108//Record success rates
109//All done with makefile
110
111
112//But first understand houghline transform
113//Know what the algorithm being used is doing.
114//MAke constants for this classifier
115//Make java be able to run on CMD line
116
117public class MainMorph {
118 static public class Pair{
119 //Privates
120 private Boolean _b;
121 private Integer _i;
122
123 //Constructor
124 public Pair(Boolean b, Integer i){
125 _b = b;
126 _i = i;
127 }
128 public Pair(){
129 _b = null;
130 _i = null;
131 }
132
133 //Getters
134 public Boolean getBoolean() {return _b;}
135 public Integer getInteger() {return _i;}
136
137 //Setters
138 public void setBoolean (Boolean b){_b = b;}
139 public void setInteger (Integer i){_i = i;}
140
141 //ToString
142 public String toString() {return "Boolean: " + _b + " Integer: " + _i;}
143 }
144
145 //CODE VERSIONS
146 static int CODE_VERSION = 9;
147 static int IMAGE_VERSION = 3;
148 //GLOBAL_CONSTANTS
149
150 static double THRESHOLD_C = 4;
151 static double THRESHOLD_AREA_SIZE = 10000;
152 static double THRESHOLD_AREA_COUNT = 10;
153
154 //
155
156 private static void imageViewer(String winName, Mat img) {
157 try {
158 //Internal display - Overview - Will Distort High Res images
159 if(IMAGE_VERSION == 1) {
160 HighGui.namedWindow(winName, HighGui.WINDOW_NORMAL);
161 HighGui.resizeWindow(winName, 1000, 1000);
162 imshow(winName, img);
163
164 HighGui.moveWindow(winName, 500, 0);
165 HighGui.waitKey(0);
166
167 HighGui.destroyWindow(winName);
168 }
169 //Internal display - Segmented - Will _NOT_ Distort High Res images
170 if(IMAGE_VERSION == 2) {
171 HighGui.namedWindow(winName, HighGui.WINDOW_AUTOSIZE);
172 HighGui.resizeWindow(winName, 1000, 1000);
173 imshow(winName, img);
174 HighGui.moveWindow(winName, 500, 0);
175 HighGui.waitKey(0);
176
177 HighGui.destroyWindow(winName);
178 }
179 //External display - Save Images for manual viewing
180 if(IMAGE_VERSION == 3) {
181 //save file (testing purposes)
182 imwrite(winName+".jpg", img);
183 }
184 }
185 catch (Exception e){
186 e.printStackTrace();
187 }
188 }
189
190 //MAIN
191 public static void main(String[] args) {
192 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
193 try {
194 //Variables
195 System.out.println("Running Code version: " + CODE_VERSION + " Image Version: " +IMAGE_VERSION);
196 Mat edgesDetectedRGB = new Mat();
197 Mat clustersFoundRGB = new Mat();
198
199 String testDirectory = "/Scratch/cpb16/is-sheet-music-encore/image-identification-dev-02/image-identification-development/";
200 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
201 String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
202
203 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
204 //mdp.39015097852365-2.png 176 lines Contents page.
205 //mdp.39015097852555-3.png 76 lines
206 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
207 //coo.31924062612282-9.png 8 lines
208 //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY? DUE TO IMAGE LARGE, ZOOM IN
209 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
210 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
211 //String default_file =hiresDirectory+"BK/NotSheetMusic/loc.ark+=13960=t50g4443d-5.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
221 //String default_file = hiresDirectory+"/BK/NotSheetMusic/aeu.ark+=13960=t2s47k537-4.png"; //centre example
222 //String default_file = hiresDirectory+"/BK/NotSheetMusic/aeu.ark+=13960=t3tt4xf2t-2.png"; //cross
223 //TestNew images used
224 //String default_file = hiresDirectory+"/MU/SheetMusic/aeu.ark+=13960=t93787r1w-10.png"; //Bleed
225 //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-10.png"; //Handwritten
226 //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-7.png"; //Handwritten 3072 4176
227 //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-8.png"; //Handwritten
228 //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-9.png"; //Handwritten
229 //String default_file = hiresDirectory+"/MU/SheetMusic/coo.31924062612282-9.png"; //Snippet
230 //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-10.png"; //Generated
231 //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-7.png";
232 //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-8.png";
233 //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-9.png";
234 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-10.png"; //contentpage
235 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; //Image evaluation
236 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-2.png"; //large numbers
237 //String default_file = hiresDirectory+ "MU/SheetMusic/mdp.39015096400919-9.png";
238 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-3.png";
239 //String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015080921409-9.png";
240 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-4.png";
241 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-5.png";
242 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-6.png";
243 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-7.png";
244 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-8.png";
245 //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-9.png";
246 String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015096398899-4.png";
247
248
249
250 //String default_file = testDirectory+"TestImages/MorphTester.png";
251 //String default_file = testDirectory+"TestImages/NotSheetMusic01.png";
252 //String default_file = testDirectory+"TestImages/NotSheetMusic02.png";
253 //String default_file = testDirectory+"TestImages/SheetMusic01.png";
254 //String default_file = testDirectory+"TestImages/SheetMusic02.png";
255 //String default_file = testDirectory+"TestImages/vLine.png";
256 String filename = ((args.length > 0) ? args[0] : default_file);
257 File file = new File(filename);
258 if(!file.exists()){System.err.println("Image not found: "+ filename);}
259
260 int horizontalLineCount =0;
261
262 // Load an image
263 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
264 System.out.println("Width: " + original.width() + " Height: " + original.height());
265 Mat binarized = original.clone();
266
267 //Imgproc.adaptiveThreshold(original, binarized,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
268 //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
269 //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY);
270 //************************
271 //Argument Setup
272 //************************
273 int tempvariable = 0;
274 if (tempvariable ==1){
275 Imgproc.adaptiveThreshold(original, binarized, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 1001, 5);
276 }
277 else {
278
279 int borderIgnoreAmount = 0;
280 double divisor;
281 switch (borderIgnoreAmount) {
282 case 5:
283 divisor = 40;
284 break;
285 case 10:
286 divisor = 20;
287 break;
288 case 20:
289 divisor = 10;
290 break;
291 case 40:
292 divisor = 5;
293 break;
294 case 0:
295 divisor = -1;
296 break;
297 default:
298 divisor = -1;
299 }
300 if (divisor != -1) {
301 Mat mid = new Mat();
302 Imgproc.adaptiveThreshold(original, mid, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
303 double maxX = mid.size().width;
304 double maxY = mid.size().height;
305 Point cp1 = new Point(maxX / divisor, maxY / divisor);
306 Point cp2 = new Point(maxX - cp1.x, maxY - cp1.y);
307 Rect rectCrop = new Rect(cp1, cp2);
308 binarized = mid.submat(rectCrop);
309 } else {
310 Imgproc.adaptiveThreshold(original, binarized, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
311 }
312
313 System.out.println("Width: " + binarized.width() + " Height: " + binarized.height());
314 }
315
316 //****************MORPHOLOGY****************************************************************************************
317 //ADDIOTIONAL FILTERING TO STOP STREAKS
318 //LOOK INTO STREAKS MORPHOGOLY.
319 //****************MORPHOLOGY****************************************************************************************
320
321 // Create the images that will use to extract the horizontal and vertical lines
322
323 //dynamic morphology??
324 if(CODE_VERSION == 1) {
325 int hori = binarized.width();
326 int vert = binarized.height();
327 //Find ratio between 100 and width and 100 and height
328 int divX = hori/10;
329 int divY = vert/10;
330 int sizeX = (hori/divX) * 10;
331 int sizeY = (vert/divY) * 10;
332
333 Mat test = binarized.clone();
334 imageViewer("Original", test);
335
336 System.out.println("hori: " + hori + '\t' + "vert: " + vert);
337 System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY);
338
339 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
340 Imgproc.erode(test,test,kernelErode);
341 imageViewer("01 Erode", test);
342
343 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
344 Imgproc.dilate(test, test, kernelDialate);
345 imageViewer("02 Dialate", test);
346
347 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
348 Imgproc.erode(test,test,kernelErodeAgain);
349 imageViewer(" 03 Erode Again", test);
350
351 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
352 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
353 imageViewer("04 Close", test);
354
355 Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
356 imageViewer("05 Binarized", test);
357
358 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
359 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
360 imageViewer(" 06 Open", test);
361
362 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
363 Imgproc.dilate(test, test, kernelDialateAgain);
364 imageViewer("07 Dialate", test);
365
366
367 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
368 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
369 imageViewer(" 08 Close Again (Final)", test);
370 }
371 //Successful hardcode for morhpology
372 if (CODE_VERSION == 2) {
373
374 //MAKE SURE BLACK & WHITE
375 Mat test = binarized.clone();
376 imageViewer("00 Binarized Original", test);
377
378 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
379 Imgproc.erode(test,test,kernelErode);
380 imageViewer("01 Erode", test);
381
382 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
383 Imgproc.dilate(test, test, kernelDialate);
384 imageViewer("02 Dialate", test);
385
386 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
387 Imgproc.erode(test,test,kernelErodeAgain);
388 imageViewer(" 03 Erode Again", test);
389
390 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
391 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
392 imageViewer("04 Close", test);
393
394 Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
395 imageViewer("05 Binarized", test);
396
397 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
398 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
399 imageViewer(" 06 Open", test);
400
401 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
402 Imgproc.dilate(test, test, kernelDialateAgain);
403 imageViewer("07 Dialate", test);
404
405 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
406 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
407 imageViewer(" 08 Close Again (Final)", test);
408
409 //FIGURE OUT FLOOD FILL!!
410 //Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2));
411
412 }
413 //Tutorial/Demo Code
414 if (CODE_VERSION == 3) {
415 Mat horizontal = binarized.clone();
416 Mat vertical = binarized.clone();
417 // Specify size on horizontal axis
418 int horizontal_size = horizontal.cols() / 50;
419 // Create structure element for extracting horizontal lines through morphology operations
420 Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2));
421 // Apply morphology operations
422 Imgproc.erode(horizontal, horizontal, horizontalStructure);
423 Imgproc.dilate(horizontal, horizontal, horizontalStructure);
424 // Show extracted horizontal lines
425 imageViewer("horizontal", horizontal);
426 // Specify size on vertical axis
427 int vertical_size = vertical.rows() / 30;
428 // Create structure element for extracting vertical lines through morphology operations
429 Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size));
430 // Apply morphology operations
431 Imgproc.erode(vertical, vertical, verticalStructure);
432 Imgproc.dilate(vertical, vertical, verticalStructure);
433 // Show extracted vertical lines
434 imageViewer("vertical", vertical);
435 // Inverse vertical image
436 Core.bitwise_not(vertical, vertical);
437 imageViewer("vertical_bit", vertical);
438 // Extract edges and smooth image according to the logic
439 // 1. extract edges
440 // 2. dilate(edges)
441 // 3. src.copyTo(smooth)
442 // 4. blur smooth img
443 // 5. smooth.copyTo(src, edges)
444 // Step 1
445 Mat edges = new Mat();
446 Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
447 imageViewer("edges", edges);
448 // Step 2
449 Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
450 Imgproc.dilate(edges, edges, kernel);
451 imageViewer("dilate", edges);
452 // Step 3
453 Mat smooth = new Mat();
454 vertical.copyTo(smooth);
455 // Step 4
456 Imgproc.blur(smooth, smooth, new Size(2, 2));
457 // Step 5
458 smooth.copyTo(vertical, edges);
459 // Show final result
460 imageViewer("smooth - final", vertical);
461 System.exit(0);
462 }
463 //Better morphology attempt - static
464 if(CODE_VERSION == 4) {
465
466 //Display Original
467 imageViewer("original", original);
468
469 Mat test = binarized.clone();
470 Mat pre = binarized.clone();
471 Mat dst = new Mat();
472
473 imageViewer("00 Inverse Binarized Original", test);
474
475 //remove large items of no interest pre proc
476 //denoize
477 //heal
478 //fnd large images, write to a seperate mat.
479 //draw these onto orignal image(binerized) in red
480 //turn all red pixels in image to black
481
482
483 //denoize
484 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
485 Imgproc.morphologyEx(pre,pre, Imgproc.MORPH_OPEN, denoize);
486 imageViewer("Denoize - PRE", pre);
487
488 //close up gaps
489 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
490 Imgproc.morphologyEx(pre,pre,Imgproc.MORPH_CLOSE, gapCloser);
491 imageViewer("gap closer - PRE", pre);
492
493 Mat kernelHighlightLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));
494 Imgproc.erode(pre,pre, kernelHighlightLarge);
495 imageViewer("Highlight Large - PRE", pre);
496
497 //change white pixels to red
498 ArrayList<MatOfPoint> contoursPre = new ArrayList<MatOfPoint>();
499 Mat hierarchyPre = new Mat();
500
501 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
502 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
503 //(hierarchy) output array: Optional output vector, containing information about the image topology.
504 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
505
506 Imgproc.findContours(pre, contoursPre, hierarchyPre, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
507
508 //Draw contours and record areas
509
510 Mat drawingPre = Mat.zeros(test.size(), CvType.CV_8UC3);
511 Mat mask = new Mat(drawingPre.size(), CV_8UC3,new Scalar(0));
512 Scalar colorPre = new Scalar(255, 255, 255);
513 Imgproc.drawContours(mask, contoursPre, -1, new Scalar(255, 255, 255), FILLED);
514 Imgproc.fillPoly(drawingPre, contoursPre,colorPre);
515 imageViewer("DRAWINGPRE", drawingPre);
516
517
518 //FIIIIIIIIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXX
519 //Remove from main Mat
520 Core.bitwise_not(mask,mask);
521 imageViewer("MASK", mask);
522 imageViewer("TEST", test);
523
524 drawingPre.copyTo(test, mask);
525 imageViewer("COMBINE", test);
526
527 //start staff line detection
528
529 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
530 Imgproc.erode(test,test,kernelErode);
531 imageViewer("01 Erode plus pre", test);
532
533 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
534 Imgproc.dilate(test,test,kernelDilate);
535 imageViewer("02 Dilate", test);
536
537 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
538 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
539 imageViewer("03 Open", test);
540
541 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
542 Imgproc.erode(test,test,kernelErode02);
543 imageViewer("04 Erode (Final)", test);
544
545
546 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
547 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
548 Mat hierarchy = new Mat();
549
550 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
551 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
552 //(hierarchy) output array: Optional output vector, containing information about the image topology.
553 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
554
555 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
556
557 //Draw contours and record areas
558 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
559 Mat drawing2 = Mat.zeros(test.size(), CvType.CV_8UC3);
560 int areaCounter = 0;
561 for (int i = 0; i < contours.size(); i++) {
562 double area = Imgproc.contourArea(contours.get(i));
563 if(area > THRESHOLD_AREA_SIZE ) {
564 areaCounter++;
565 Scalar color = new Scalar(0, 0, 255);
566 Imgproc.drawContours(drawing, contours, i, color, 1);
567 System.out.println("AREA: " + area);
568 }
569 }
570
571 //Classifier Calculation
572 if(areaCounter >= THRESHOLD_AREA_COUNT){
573 System.out.println("THIS IS SHEET MUSIC");
574 System.out.println(areaCounter);
575 }
576
577
578 //Show in a window
579 imageViewer("Contours", drawing);
580 }
581 //Better morphology attempt - dynamic
582 if(CODE_VERSION == 5) {
583 int hori = binarized.width();
584 int vert = binarized.height();
585 //Find ratio between 100 and width and 100 and height
586 int sizeX100 = 10 * (hori/68);
587 int sizeY100 = 10 * (vert/46);
588 int sizeX10 = (hori/68);
589 int sizeY10 = (vert/46);
590 int sizeX1 = (hori/68)/10;
591 int sizeY1 = (vert/46)/10;
592
593 //SizeX should always be a 68th * 10. Based off the defualt tester image "coo.*"
594 //SizeT should always be a 46th * 10
595
596 System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1);
597 //Display Original
598 imageViewer("original", original);
599
600 Mat test = binarized.clone();
601 imageViewer("00 Inverse Binarized Original", test);
602
603 //Remove very large and wide black spaces (8th of the page)
604 //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8));
605 //Imgproc.erode(test,test, kernelRemoveLarge);
606 //imageViewer("Remove Large", test);
607
608 //Eliminate things that are not long and thin
609 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
610 Imgproc.erode(test,test,kernelErode);
611 imageViewer("01 Erode", test);
612
613 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
614 Imgproc.dilate(test,test,kernelDilate);
615 imageViewer("02 Dilate", test);
616
617 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
618 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
619 imageViewer("03 Open", test);
620
621 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
622 Imgproc.erode(test,test,kernelErode02);
623 imageViewer("04 Erode (Final)", test);
624
625
626 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
627 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
628 Mat hierarchy = new Mat();
629
630 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
631 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
632 //(hierarchy) output array: Optional output vector, containing information about the image topology.
633 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
634
635 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
636
637 //Draw contours and record areas
638 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
639 int areaCounter = 0;
640 for (int i = 0; i < contours.size(); i++) {
641 double area = Imgproc.contourArea(contours.get(i));
642 if(area > THRESHOLD_AREA_SIZE ) {
643 areaCounter++;
644 Scalar color = new Scalar(0, 0, 255);
645 Imgproc.drawContours(drawing, contours, i, color, 1);
646 System.out.println("AREA: " + area);
647 }
648 }
649
650 //Classifier Calculation
651 if(areaCounter >= THRESHOLD_AREA_COUNT){
652 System.out.println("THIS IS SHEET MUSIC");
653 System.out.println(areaCounter);
654 }
655
656
657 //Show in a window
658 imageViewer("Contours", drawing);
659 }
660 //MASK UNDERSTANDING
661 if(CODE_VERSION == 6) {
662 String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
663 Mat mask = new Mat();
664 Mat dst = new Mat();
665 //Get source image and binerize
666 Mat src = Imgcodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE);
667 Imgproc.adaptiveThreshold(original, src,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
668 imageViewer("src", src);
669
670 //Find unwanted material, then invert it so mask removes not keeps.
671 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
672 Imgproc.erode(src,mask,kernelErode);
673 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
674 Imgproc.dilate(mask, mask, kernelDilate);
675 Core.bitwise_not(mask,mask);
676 imageViewer("mask", mask);
677
678 //Copy source image to new Mat, with mask in use
679 src.copyTo(dst, mask);
680 imageViewer("dst", dst);
681
682
683
684
685 }
686 //Mask implementation
687 if(CODE_VERSION == 7) {
688
689 //Display Original
690 imageViewer("original", original);
691
692 Mat src = binarized.clone();
693 Mat test = binarized.clone();
694 Mat mask = new Mat();
695 Mat dst = new Mat();
696
697 imageViewer("00 Inverse Binarized Original", src);
698
699 //denoize
700 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
701 Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
702 imageViewer("01 Denoize - mask", mask);
703
704 //close up gaps
705 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
706 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
707 imageViewer("02 gap closer - mask", mask);
708
709 //Isolate large items
710 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
711 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
712 imageViewer("03 Isolate Large - mask", mask);
713 Core.bitwise_not(mask,mask);
714
715 //Remove unwanted large items from image
716 src.copyTo(dst, mask);
717 imageViewer("04 Large Items Removed", dst);
718
719 //start staff line detection
720
721 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
722 Imgproc.erode(dst,test,kernelErode);
723 imageViewer("11 Erode plus pre", test);
724
725 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
726 Imgproc.dilate(test,test,kernelDilate);
727 imageViewer("12 Dilate", test);
728
729 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
730 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
731 imageViewer("13 Open", test);
732
733 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
734 Imgproc.erode(test,test,kernelErode02);
735 imageViewer("14 Erode (Final)", test);
736
737
738 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
739 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
740 Mat hierarchy = new Mat();
741
742 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
743 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
744 //(hierarchy) output array: Optional output vector, containing information about the image topology.
745 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
746
747 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
748
749 //Draw contours and record areas
750 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
751 int areaCounter = 0;
752
753
754 Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), FILLED);
755// for (int i = 0; i < contours.size(); i++) {
756// Scalar color = new Scalar(0, i, i);
757// double area = Imgproc.contourArea(contours.get(i));
758// Imgproc.drawContours(drawing, contours, i, color, FILLED);
759// System.out.println("AREA: " + area);
760//
761// }
762 imageViewer("Contours found", drawing);
763
764 //Classifier Calculation
765 if(areaCounter >= THRESHOLD_AREA_COUNT){
766 System.out.println("THIS IS SHEET MUSIC");
767 System.out.println(areaCounter);
768 }
769
770
771 }
772 //Mask implementations (LARGE AND SMALL) - HIGH RES NUMBER MOD
773 if(CODE_VERSION == 8) {
774
775 //Display Original
776 imageViewer("original", original);
777
778 Mat test = binarized.clone();
779
780
781 imageViewer("00 Inverse Binarized Original", test);
782
783
784 //************************************
785 //Large Object Removal
786 //************************************
787 Mat srcLOR = binarized.clone();
788 Mat maskLOR = new Mat();
789 Mat dstLOR = new Mat();
790
791 //denoize
792 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
793 Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
794 imageViewer("01 Denoize - mask", maskLOR);
795
796 //close up gaps
797 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
798 Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
799 imageViewer("02 gap closer - mask", maskLOR);
800
801 //Isolate large items
802 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
803 Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
804 imageViewer("03 Isolate Large - mask", maskLOR);
805
806 //Remove large items from image
807 Core.bitwise_not(maskLOR,maskLOR);
808 srcLOR.copyTo(dstLOR, maskLOR);
809 imageViewer("04 Large Items Removed", dstLOR);
810
811 //****************************************
812 //Small object removal (SOR)
813 //****************************************
814
815 Mat srcSOR = dstLOR.clone();
816 Mat maskSOR = new Mat();
817 Mat dstSOR = new Mat();
818
819 Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
820 Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
821 imageViewer("11 show small - mask", maskSOR);
822
823 Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
824 Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
825 imageViewer("12 highlight small - mask", maskSOR);
826
827/* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
828 Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
829 imageViewer("13 isolate small - mask", maskSOR);
830*/
831
832 //Remove small items from image
833 Core.bitwise_not(maskSOR, maskSOR);
834 srcSOR.copyTo(dstSOR, maskSOR);
835 imageViewer("14 Small Items Removed", dstSOR);
836
837
838 //****************************************
839 //start staff line detection
840 //****************************************
841
842 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
843 Imgproc.erode(dstSOR,test,kernelErode);
844 imageViewer("21 Erode plus pre", test);
845
846 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
847 Imgproc.dilate(test,test,kernelDilate);
848 imageViewer("22 Dilate", test);
849
850 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
851 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
852 imageViewer("23 Close", test);
853
854
855 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
856 Imgproc.erode(test,test,kernelErode02);
857 imageViewer("24 Erode (Final)", test);
858
859 //********************************************************************************
860 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
861 //********************************************************************************
862 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
863 ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
864 ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
865 Mat hierarchy = new Mat();
866
867 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
868 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
869 //(hierarchy) output array: Optional output vector, containing information about the image topology.
870 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
871
872 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
873
874 System.out.println(contours.size());
875 //Draw contours and record areas
876 Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
877 Mat largeContoursFound = allContoursFound.clone() ;
878 Mat postContoursFound = allContoursFound.clone();
879 int areaCounter = 0;
880
881 //Have created a preprocess to remove large objects.
882 //Need to now finalized Classifier, re try area detection.
883 //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
884 //Just use area and periemter to determine if sheet music
885 //Discuss with david before weekend perhaps?
886
887 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
888 for (int i = 0; i < contours.size(); i++) {
889 double area = Imgproc.contourArea(contours.get(i));
890 if(area > 100) {
891 System.out.println("AREA: " + area);
892 Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
893 //create list of large coutours found
894 largeContours.add(contours.get(i));
895 }
896 }
897 imageViewer("80 All Contours found", allContoursFound);
898 imageViewer("81 Large Contours Found", largeContoursFound);
899
900 //*****************************************************************
901 //Circles on processed images
902 //*****************************************************************
903// //Init arrays
904// Mat circleOutput = allContoursFound.clone();
905// MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()];
906// Rect[] boundRect = new Rect[largeContours.size()];
907// Point[] centers = new Point[largeContours.size()];
908// float[][] radius = new float[largeContours.size()][1];
909//
910// //Fill arrays
911// for (int i = 0; i < largeContours.size(); i++) {
912// contoursPoly[i] = new MatOfPoint2f();
913// Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
914// boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
915// centers[i] = new Point();
916// Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
917//
918// }
919// //Draw circle for each large contour
920// for (int i = 0; i < largeContours.size(); i++) {
921// Imgproc.rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2);
922// Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
923// }
924// imageViewer("82 Circles found", circleOutput);
925
926 //********************************************************************************
927 //Centroids -Everything must be to scale
928 //********************************************************************************
929
930 ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
931 Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
932 for (int i = 0; i < largeContours.size(); i++) {
933 mu.add(i, Imgproc.moments(largeContours.get(i), false));
934 Moments p = mu.get(i);
935 int x = (int) (p.get_m10() / p.get_m00());
936 int y = (int) (p.get_m01() / p.get_m00());
937 Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
938 }
939 imageViewer("83 Centres found", centreOutput);
940
941
942 //***********************************************
943 //PostProcessing - Morphology Classifier
944 // Use dilation to "Connect the dots"
945 // Testing showed the centroids were clustered together
946 // Then use area or perimeter as a classifier filter
947 //REFINEREFINEREIFEN
948 //REFINEREFINEREIFEN
949 //REFINEREFINEREIFEN
950 //REFINEREFINEREIFEN
951 //REFINEREFINEREIFEN
952 //REFINEREFINEREIFEN
953 // FIX UP CLASSIFIER COMPARISON.
954 // MORPHOLOGIES FUNCTIONS RETURN NULL AS THEIR RETURN VARIABLES. WHY?
955 //REFINEREFINEREIFEN
956 //REFINEREFINEREIFEN
957 //REFINEREFINEREIFEN
958 //REFINEREFINEREIFEN
959 //REFINEREFINEREIFEN
960 //***********************************************
961
962 Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
963 Imgproc.dilate(centreOutput,centreOutput,postDilate);
964 imageViewer("91 PostDilated", centreOutput);
965
966 Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
967 Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
968 imageViewer("92 PostClose", centreOutput);
969
970 Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
971 Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
972 imageViewer("93 PostDenoize", centreOutput);
973
974 //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50));
975 //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline);
976
977
978
979 //Find area
980 Mat centreOutputGrey = new Mat();
981 Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
982 Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
983
984 for (int i = 0; i < postContours.size(); i++) {
985 double area = Imgproc.contourArea(postContours.get(i));
986 if(area > THRESHOLD_AREA_SIZE) {
987 System.out.println("POST AREA: " + area);
988 Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
989 areaCounter++;
990 }
991 }
992
993
994
995 imageViewer("93 PostEND", postContoursFound);
996
997 //Classifier Calculation
998 if(areaCounter >= THRESHOLD_AREA_COUNT){
999 System.out.println("THIS IS SHEET MUSIC");
1000 System.out.println(areaCounter);
1001 }
1002
1003
1004 }
1005 //ClassifierComparison - Using code version 8
1006 if(CODE_VERSION == 81) {
1007
1008 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
1009 Pair returnVariables = new Pair();
1010 int FILLED = -1;
1011 try{
1012 //Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
1013 //System.out.println("Width: " + original1.width() + " Height: " + original1.height());
1014 //Mat original = original1.clone();
1015 //Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
1016
1017 Mat test = binarized.clone();
1018 //************************************
1019 //Large Object Removal
1020 //************************************
1021 Mat srcLOR = binarized.clone();
1022 Mat maskLOR = new Mat();
1023 Mat dstLOR = new Mat();
1024
1025 //denoize
1026 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
1027 Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
1028
1029 //close up gaps
1030 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
1031 Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
1032
1033 //Isolate large items
1034 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
1035 Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
1036
1037 //Remove large items from image
1038 Core.bitwise_not(maskLOR,maskLOR);
1039 srcLOR.copyTo(dstLOR, maskLOR);
1040
1041 //****************************************
1042 //Small object removal (SOR)
1043 //****************************************
1044
1045 Mat srcSOR = dstLOR.clone();
1046 Mat maskSOR = new Mat();
1047 Mat dstSOR = new Mat();
1048
1049 Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
1050 Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
1051
1052 Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
1053 Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
1054
1055
1056/* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
1057 Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
1058 imageViewer("13 isolate small - mask", maskSOR);
1059*/
1060
1061 //Remove small items from image
1062 Core.bitwise_not(maskSOR, maskSOR);
1063 srcSOR.copyTo(dstSOR, maskSOR);
1064
1065
1066 //****************************************
1067 //start staff line detection
1068 //****************************************
1069
1070 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
1071 Imgproc.erode(dstSOR,test,kernelErode);
1072
1073
1074 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
1075 Imgproc.dilate(test,test,kernelDilate);
1076
1077
1078 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
1079 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
1080
1081
1082
1083 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
1084 Imgproc.erode(test,test,kernelErode02);
1085
1086
1087 //********************************************************************************
1088 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
1089 //********************************************************************************
1090 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
1091 ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
1092 ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
1093 Mat hierarchy = new Mat();
1094
1095 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
1096 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
1097 //(hierarchy) output array: Optional output vector, containing information about the image topology.
1098 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
1099
1100 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
1101
1102 System.out.println(contours.size());
1103 //Draw contours and record areas
1104 Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
1105 Mat largeContoursFound = allContoursFound.clone() ;
1106 Mat postContoursFound = allContoursFound.clone();
1107 int areaCounter = 0;
1108
1109 //Have created a preprocess to remove large objects.
1110 //Need to now finalized Classifier, re try area detection.
1111 //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
1112 //Just use area and periemter to determine if sheet music
1113 //Discuss with david before weekend perhaps?
1114
1115 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
1116 for (int i = 0; i < contours.size(); i++) {
1117 double area = Imgproc.contourArea(contours.get(i));
1118 if(area > 100) {
1119 //System.out.println("AREA: " + area);
1120 Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
1121 //create list of large coutours found
1122 largeContours.add(contours.get(i));
1123 }
1124 }
1125
1126 //*****************************************************************
1127 //Circles and centres on processed images
1128 //*****************************************************************
1129
1130 //Init arrays
1131 Mat circleOutput = allContoursFound.clone();
1132 MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()];
1133 Point[] centers = new Point[largeContours.size()];
1134 float[][] radius = new float[largeContours.size()][1];
1135
1136 //Fill arrays
1137 for (int i = 0; i < largeContours.size(); i++) {
1138 contoursPoly[i] = new MatOfPoint2f();
1139 Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
1140 centers[i] = new Point();
1141 Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
1142
1143 }
1144 //Draw circle for each large contour
1145 for (int i = 0; i < largeContours.size(); i++) {
1146 Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
1147 }
1148
1149
1150 //********************************************************************************
1151 //Centroids - Everything must be to scale
1152 //********************************************************************************
1153
1154 ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
1155 Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
1156 for (int i = 0; i < largeContours.size(); i++) {
1157 mu.add(i, Imgproc.moments(largeContours.get(i), false));
1158 Moments p = mu.get(i);
1159 int x = (int) (p.get_m10() / p.get_m00());
1160 int y = (int) (p.get_m01() / p.get_m00());
1161 Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
1162 }
1163
1164 //***********************************************
1165 //PostProcessing - Morphology Classifier
1166 // Use dilation to "Connect the dots"
1167 // Testing showed the centroids were clustered together
1168 // Then use area or perimeter as a classifier filter
1169 //***********************************************
1170
1171 Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
1172 Imgproc.dilate(centreOutput,centreOutput,postDilate);
1173
1174 Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
1175 Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
1176
1177 Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
1178 Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
1179
1180 //Find area
1181 Mat centreOutputGrey = new Mat();
1182 Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
1183 Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
1184
1185 for (int i = 0; i < postContours.size(); i++) {
1186 double area = Imgproc.contourArea(postContours.get(i));
1187 if(area > THRESHOLD_AREA_SIZE) {
1188 //System.err.println("POST AREA: " + area);
1189 Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
1190 areaCounter++;
1191 }
1192 }
1193 //Classifier Calculation
1194 if(areaCounter >= THRESHOLD_AREA_COUNT){
1195 returnVariables.setBoolean(true);
1196 returnVariables.setInteger(areaCounter);
1197 }
1198 }
1199 catch(Exception e){
1200 System.err.println(e);
1201 }
1202 //return returnVariables;
1203 }
1204
1205 //Reset after david chat
1206 if(CODE_VERSION == 9){
1207 int areaCounter=0;
1208 //Display Original
1209 imageViewer("original", original);
1210 imageViewer("000 Inverse Binarized Original", binarized);
1211
1212
1213 //************************************
1214 //1. Large Object Remover
1215 //************************************
1216 Mat srcLOR = binarized.clone();
1217 Mat maskLOR = new Mat();
1218 Mat dstLOR = new Mat();
1219
1220 //Remove small objects in prep for masking (De-Noise)
1221 Mat removeSmallLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
1222 Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, removeSmallLOR);
1223 imageViewer("001 De-noise", maskLOR);
1224
1225 //Heal the large items
1226 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25)); //5,5
1227 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR);
1228 imageViewer("002 heal objects in mask", maskLOR);
1229
1230 //IsolateLarge
1231 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30)); //8,8
1232 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR);
1233 imageViewer("003 Isolate large", maskLOR);
1234
1235 Core.bitwise_not(maskLOR,maskLOR);
1236 srcLOR.copyTo(dstLOR, maskLOR);
1237 imageViewer("100 Large Items Removed", dstLOR);
1238
1239 // //CODE FROM PRESENTATION
1240// Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
1241// Imgproc.erode(dstTOR,base,kernelErode);
1242// imageViewer("201 Erode", base);
1243//
1244// Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
1245// Imgproc.dilate(base, base, kernelDialate);
1246// imageViewer("202 Dialate", base);
1247//
1248// Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
1249// Imgproc.erode(base,base,kernelErodeAgain);
1250// imageViewer(" 203 Erode Again", base);
1251//
1252// Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
1253// Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelClose);
1254// imageViewer("204 Close", base);
1255//
1256// Imgproc.threshold(base,base, 127, 255, Imgproc.THRESH_BINARY);
1257// imageViewer("205 Binarized", base);
1258//
1259// Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
1260// Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
1261// imageViewer("206 Open", base);
1262//
1263// Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
1264// Imgproc.dilate(base, base, kernelDialateAgain);
1265// imageViewer("207 Dialate", base);
1266//
1267// Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
1268// Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
1269// imageViewer("208 Close Again (Final)", base);
1270
1271 //***********************************
1272// //3.1 Remove big outlines
1273// //***********************************
1274// Mat srcTOR = base.clone();
1275// Mat maskTOR = new Mat();
1276// Mat dstTOR = new Mat();
1277//
1278// //Remove small objects in prep for masking (De-Noise)
1279// Mat removeSmallTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
1280// Imgproc.morphologyEx(srcTOR,maskTOR, Imgproc.MORPH_OPEN, removeSmallTOR);
1281// imageViewer("2101 Remove Small from mask", maskTOR);
1282//
1283// //heal the large items
1284// Mat healTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
1285// Imgproc.morphologyEx(maskTOR,maskTOR, Imgproc.MORPH_CLOSE, healTOR);
1286// imageViewer("2102 heal objects in mask", maskTOR);
1287//
1288// //Highlight black outline
1289// Mat highlightLargeTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
1290// Imgproc.erode(maskTOR,maskTOR,highlightLargeTOR);
1291// imageViewer("2103 highlight objects", maskTOR);
1292//
1293// Core.bitwise_not(maskTOR, maskTOR);
1294// srcTOR.copyTo(dstTOR, maskTOR);
1295// imageViewer("2200 Black outline Removed", dstTOR);
1296
1297
1298
1299
1300
1301
1302
1303 //4. Classify like line clusters.
1304 Mat base = binarized.clone();
1305 //***********************************
1306 //3. Isolate straight lines
1307 //***********************************
1308 //Heal lines
1309 Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(86,1));
1310 Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL);
1311 imageViewer("202 heal objects in mask", base);
1312
1313 //Make White Blobs
1314
1315 //NEED THIS??
1316 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
1317 Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
1318 imageViewer("203 Open", base);
1319 //NEED THIS?
1320
1321 Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
1322 Imgproc.dilate(base, base, kernelDilateAgain);
1323 imageViewer("204 Dilate", base);
1324
1325 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50));
1326 Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
1327 imageViewer("205 Close Again (Final)", base);
1328
1329 //***********************************
1330 //4. Find 'Clusters'
1331 // Need to find areas, using bounding boxes
1332 // Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music
1333 //***********************************
1334
1335 //*****************************************************************
1336 //4.1 Prep Rectangles on processed images
1337 //*****************************************************************
1338 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
1339 Mat hierarchy = new Mat();
1340 Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
1341 //Draw contours and record areas
1342 Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3);
1343 Mat rectOutput = allContoursFound.clone();
1344 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
1345 //Init arrays
1346 MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
1347 Rect[] boundRect = new Rect[contours.size()];
1348
1349 //Fill arrays
1350 for (int i = 0; i < contours.size(); i++) {
1351 contoursPoly[i] = new MatOfPoint2f();
1352 Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true);
1353 boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
1354 }
1355 //Draw circle for each large contour
1356 for (int i = 0; i < contours.size(); i++) {
1357 if(boundRect[i].height > 100){
1358 System.out.println("i:"+ i + " Height: " + boundRect[i].height);
1359 if(boundRect[i].width > 300){
1360 areaCounter++;
1361 System.out.println("i:"+ i + " Width: " + boundRect[i].width);
1362 Imgproc.rectangle(rectOutput, boundRect[i].tl(), boundRect[i].br(), new Scalar(255, 0, 0), 20);
1363 }
1364 }
1365 }
1366
1367
1368 imageViewer("4000 Rect found", rectOutput);
1369 System.out.println(areaCounter);
1370
1371
1372
1373 }
1374 //ClassfierComparsion - Reset after david chat
1375 if(CODE_VERSION == 91){
1376 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
1377 Pair returnVariables = new Pair();
1378 int areaCounter=0;
1379
1380
1381 //Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
1382 //Mat binarized = original.clone();
1383 //Mat mid = new Mat();
1384 //if(borderCrop > 0){
1385 //Imgproc.adaptiveThreshold(original, mid,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, 2);
1386 //double maxX = mid.size().width;
1387 //double maxY = mid.size().height;
1388 //Point cp1 = new Point(maxX/10, maxY/10);
1389 //Point cp2 = new Point(maxX - cp1.x, maxY -cp1.y);
1390 //Rect rectCrop = new Rect(cp1, cp2);
1391 //binarized = mid.submat(rectCrop);
1392 //}
1393 //else{
1394 //Imgproc.adaptiveThreshold(original, binarized,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
1395 //}
1396
1397
1398 try{
1399 //************************************
1400 //1. Large Object Remover
1401 //************************************
1402 Mat srcLOR = binarized.clone();
1403 Mat maskLOR = new Mat();
1404 Mat dstLOR = new Mat();
1405
1406 //Remove small objects in prep for masking (De-Noise)
1407 Mat removeSmallLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
1408 Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, removeSmallLOR);
1409
1410 //Heal the large items
1411 Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25));
1412 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR);
1413
1414 //IsolateLarge
1415 Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30));
1416 Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR);
1417
1418 Core.bitwise_not(maskLOR,maskLOR);
1419 srcLOR.copyTo(dstLOR, maskLOR);
1420
1421 Mat base = binarized.clone();
1422 //***********************************
1423 //3. Isolate straight lines
1424 //***********************************
1425 //Heal lines
1426 Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(82,1));
1427 Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL);
1428
1429 //Make White Blobs
1430 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
1431 Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
1432
1433 Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
1434 Imgproc.dilate(base, base, kernelDilateAgain);
1435
1436 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50));
1437 Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
1438
1439 //***********************************
1440 //4. Find 'Clusters'
1441 // Need to find areas, using bounding boxes
1442 // Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music
1443 //***********************************
1444
1445 //*****************************************************************
1446 //4.1 Prep Rectangles on processed images
1447 //*****************************************************************
1448 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
1449 Mat hierarchy = new Mat();
1450 Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
1451 //Draw contours and record areas
1452 Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3);
1453 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
1454 //Init arrays
1455 MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
1456 Rect[] boundRect = new Rect[contours.size()];
1457
1458 //Fill arrays
1459 for (int i = 0; i < contours.size(); i++) {
1460 contoursPoly[i] = new MatOfPoint2f();
1461 Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true);
1462 boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
1463 }
1464 //Draw circle for each large contour
1465 //MAKE THE COMPARING VALUES GLOBAL CONSTANTS
1466 for (int i = 0; i < contours.size(); i++) {
1467 if(boundRect[i].height > 100){
1468 if(boundRect[i].width > 300){
1469 areaCounter++;
1470 }
1471 }
1472 }
1473
1474 //Classifier Calculation
1475 if(areaCounter >= THRESHOLD_AREA_COUNT){
1476 returnVariables.setBoolean(true);
1477 returnVariables.setInteger(areaCounter);
1478 }
1479 }
1480 catch(Exception e){
1481 System.err.println(e);
1482 }
1483 //return returnVariables;
1484 }
1485
1486 //****************MORPHOLOGY****************************************************************************************
1487
1488 //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
1489
1490 //Display Results
1491 //HighGui.imshow("Source", original);
1492 //HighGui.imshow("Just Edges", justEdges); //TESTING
1493
1494
1495 //imshow("LINESFOUND", edgesDetectedRGB);
1496 //HighGui.resizeWindow("LINESFOUND", 1000,1000);
1497
1498 //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
1499 //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
1500
1501 //COUNT OF LINES CLASSIFICATION
1502 //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
1503 //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
1504 //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
1505
1506 //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
1507
1508 // Wait and Exit
1509 //HighGui.waitKey();
1510 System.exit(0);
1511 }
1512 catch(Exception e){
1513 System.err.println(e);
1514 }
1515 }
1516}
Note: See TracBrowser for help on using the repository browser.