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

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

Started implementing Davids suggested morphology sequence, codeversion9

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