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

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

Have created properties file and accessibility from javaClassifierComparision. Have created download middle page only variant of image-downloader system. Have created EndToEndSystem bash script that will be used as makefile, with makefile being used to execute preset useful commands to the EndToEndSystem. It needs to be fleshed out as only has one script, this script can run a classifier and place the results into a subdirectory defined by the user

File size: 27.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;
66import org.opencv.osgi.OpenCVNativeLoader;
67import org.opencv.photo.Photo;
68
69import static org.opencv.core.CvType.CV_8UC3;
70import static org.opencv.highgui.HighGui.imshow;
71import static org.opencv.imgcodecs.Imgcodecs.imwrite;
72import java.awt.image.BufferedImage;
73import java.awt.image.DataBufferByte;
74import java.io.File;
75import java.util.ArrayList;
76import java.util.Collection;
77import java.util.Collections;
78import java.util.Comparator;
79import javax.imageio.ImageIO;
80
81//REFERENCES:
82//https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
83//https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
84//https://www.wikihow.com/Calculate-Percentage-in-Java
85//https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
86//https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
87//https://www.programiz.com/java-programming/examples/standard-deviation
88//https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/
89//https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556
90//https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class
91//https://stackoverflow.com/questions/37946482/crop-images-area-with-opencv-java
92//https://docs.opencv.org/3.4/dd/dd7/tutorial_morph_lines_detection.html
93//https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html
94//https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/moments/moments.html
95//https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
96//http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html
97//https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
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 //GLOBAL_CONSTANTS
118
119 static double THRESHOLD_C = 4;
120 static double THRESHOLD_AREA_SIZE = 1000;
121 static double THRESHOLD_AREA_COUNT = 2;
122
123 //
124 static class StartAndEndPoint {
125 //PRIVATES
126 private Point _p1;
127 private Point _p2;
128 //CONSTRUCTOR
129 public StartAndEndPoint(Point p1, Point p2){
130 _p1 = p1;
131 _p2 = p2;
132 }
133 //GETTERS
134 public Point getP1(){
135 return _p1;
136 }
137 public Point getP2(){
138 return _p2;
139 }
140 //SETTERS
141 public void setP1(Point p1){
142 _p1 = p1;
143 }
144 public void setP2(Point p2){
145 _p2 = p2;
146 }
147
148 //ToString
149 public String toString(){
150 return "Start: " + _p1 + " End: " + _p2;
151 }
152
153 }
154 private static BufferedImage toBufferedImage(Mat mat){
155 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
156 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
157 //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
158 try{
159 int type = BufferedImage.TYPE_3BYTE_BGR;
160 int bufferSize = mat.channels() * mat.cols() * mat.rows();
161 byte[] b = new byte[bufferSize];
162 //get all the pixels
163 mat.get(0, 0, b);
164 BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
165 final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
166 System.arraycopy(b, 0, targetPixels, 0, b.length);
167 return image;
168 }
169 catch(Exception e){
170 System.err.println(e);
171 }
172 return null;
173 }
174
175 private static void showWaitDestroy(String winname, Mat img) {
176 try {
177 HighGui.namedWindow(winname, HighGui.WINDOW_AUTOSIZE);
178 imshow(winname, img);
179 HighGui.resizeWindow(winname, 1000, 1000);
180 HighGui.moveWindow(winname, 500, 0);
181 HighGui.waitKey(0);
182
183 HighGui.destroyWindow(winname);
184 }
185 catch (Exception e){
186 e.printStackTrace();
187 }
188 }
189 //MAIN
190 public static void main(String[] args) {
191
192 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
193
194 try {
195
196 ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();
197
198 //Variables
199 int codeVersion = 4;
200 System.out.println("Running code version: " + codeVersion);
201 Mat edgesDetected = new Mat();
202 Mat mid = new Mat();
203 Mat edgesDetectedRGB = new Mat();
204 Mat clustersFoundRGB = new Mat();
205
206 String testDirectory = "/Scratch/cpb16/is-sheet-music-encore/image-identification-dev-02/image-identification-development/";
207 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
208 String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
209
210 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
211 //mdp.39015097852365-2.png 176 lines Contents page.
212 //mdp.39015097852555-3.png 76 lines
213 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
214 //coo.31924062612282-9.png 8 lines
215 //String default_file = directory+"NotSheetMusic/coo.31924062612282-9.png";
216 //String default_file = directory+"NotSheetMusic/mdp.39015097852365-2.png";
217 //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY?
218 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
219 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
220 String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
221
222 //System.out.println(default_file);
223 //String default_file = "/Scratch/cpb16/is-sheet-music-encore/image-identification-terminal/TestImages/test-coo.31924062612282-9.png";
224 //String default_file = testDirectory+"TestImages/MorphTester.png";
225 //String default_file = testDirectory+"TestImages/NotSheetMusic01.png";
226 //String default_file = testDirectory+"TestImages/NotSheetMusic02.png";
227 //String default_file = testDirectory+"TestImages/SheetMusic01.png";
228 //String default_file = testDirectory+"TestImages/SheetMusic02.png";
229 //String default_file = testDirectory+"TestImages/vLine.png";
230 String filename = ((args.length > 0) ? args[0] : default_file);
231 File file = new File(filename);
232 if(!file.exists()){System.err.println("Image not found: "+ filename);}
233
234 int horizontalLineCount =0;
235
236 // Load an image
237 Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
238 Mat original = original1.clone();
239
240 Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
241 //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
242 //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY);
243
244
245 //****************MORPHOLOGY****************************************************************************************
246 //ADDIOTIONAL FILTERING TO STOP STREAKS
247 //LOOK INTO STREAKS MORPHOGOLY.
248 //****************MORPHOLOGY****************************************************************************************
249
250 // Create the images that will use to extract the horizontal and vertical lines
251
252 //dynamic morphology??
253 if(codeVersion == 1) {
254 int hori = original.width();
255 int vert = original.height();
256 //Find ratio between 100 and width and 100 and height
257 int divX = hori/10;
258 int divY = vert/10;
259 int sizeX = (hori/divX) * 10;
260 int sizeY = (vert/divY) * 10;
261
262 Mat test = original.clone();
263 showWaitDestroy("Original", test);
264
265 System.out.println("hori: " + hori + '\t' + "vert: " + vert);
266 System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY);
267
268 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
269 Imgproc.erode(test,test,kernelErode);
270 showWaitDestroy("01 Erode", test);
271
272 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
273 Imgproc.dilate(test, test, kernelDialate);
274 showWaitDestroy("02 Dialate", test);
275
276 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
277 Imgproc.erode(test,test,kernelErodeAgain);
278 showWaitDestroy(" 03 Erode Again", test);
279
280 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
281 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
282 showWaitDestroy("04 Close", test);
283
284 Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
285 showWaitDestroy("05 Binarized", test);
286
287 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
288 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
289 showWaitDestroy(" 06 Open", test);
290
291 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
292 Imgproc.dilate(test, test, kernelDialateAgain);
293 showWaitDestroy("07 Dialate", test);
294
295
296 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
297 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
298 showWaitDestroy(" 08 Close Again (Final)", test);
299 }
300 //Successful hardcode for morhpology
301 if (codeVersion == 2) {
302
303 //MAKE SURE BLACK & WHITE
304 Mat test = original.clone();
305 showWaitDestroy("00 Binarized Original", test);
306
307 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
308 Imgproc.erode(test,test,kernelErode);
309 showWaitDestroy("01 Erode", test);
310
311 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
312 Imgproc.dilate(test, test, kernelDialate);
313 showWaitDestroy("02 Dialate", test);
314
315 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
316 Imgproc.erode(test,test,kernelErodeAgain);
317 showWaitDestroy(" 03 Erode Again", test);
318
319 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
320 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
321 showWaitDestroy("04 Close", test);
322
323// Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
324// showWaitDestroy("05 Binarized", test);
325
326 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
327 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
328 showWaitDestroy(" 06 Open", test);
329
330// Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
331// Imgproc.dilate(test, test, kernelDialateAgain);
332// showWaitDestroy("07 Dialate", test);
333
334 //FIGURE OUT FLOOD FILL!!
335 Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2));
336
337
338 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
339 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
340 showWaitDestroy(" 08 Close Again (Final)", test);
341
342 }
343 //Tutorial/Demo Code
344 if (codeVersion == 3) {
345 Mat horizontal = original.clone();
346 Mat vertical = original.clone();
347 // Specify size on horizontal axis
348 int horizontal_size = horizontal.cols() / 50;
349 // Create structure element for extracting horizontal lines through morphology operations
350 Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2));
351 // Apply morphology operations
352 Imgproc.erode(horizontal, horizontal, horizontalStructure);
353 Imgproc.dilate(horizontal, horizontal, horizontalStructure);
354 // Show extracted horizontal lines
355 showWaitDestroy("horizontal", horizontal);
356 // Specify size on vertical axis
357 int vertical_size = vertical.rows() / 30;
358 // Create structure element for extracting vertical lines through morphology operations
359 Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size));
360 // Apply morphology operations
361 Imgproc.erode(vertical, vertical, verticalStructure);
362 Imgproc.dilate(vertical, vertical, verticalStructure);
363 // Show extracted vertical lines
364 showWaitDestroy("vertical", vertical);
365 // Inverse vertical image
366 Core.bitwise_not(vertical, vertical);
367 showWaitDestroy("vertical_bit", vertical);
368 // Extract edges and smooth image according to the logic
369 // 1. extract edges
370 // 2. dilate(edges)
371 // 3. src.copyTo(smooth)
372 // 4. blur smooth img
373 // 5. smooth.copyTo(src, edges)
374 // Step 1
375 Mat edges = new Mat();
376 Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
377 showWaitDestroy("edges", edges);
378 // Step 2
379 Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
380 Imgproc.dilate(edges, edges, kernel);
381 showWaitDestroy("dilate", edges);
382 // Step 3
383 Mat smooth = new Mat();
384 vertical.copyTo(smooth);
385 // Step 4
386 Imgproc.blur(smooth, smooth, new Size(2, 2));
387 // Step 5
388 smooth.copyTo(vertical, edges);
389 // Show final result
390 showWaitDestroy("smooth - final", vertical);
391 System.exit(0);
392 }
393 //Better morphology attempt - static
394 if(codeVersion ==4) {
395
396 //Display Original
397 showWaitDestroy("original", original1);
398
399 Mat test = original.clone();
400
401 showWaitDestroy("00 Inverse Binarized Original", test);
402
403 //remove large items of no interest pre proc
404 //denoize
405 //heal
406 //fnd large images, write to a seperate mat.
407 //draw these onto orignal image(binerized) in red
408 //turn all red pixels in image to black
409
410
411 //denoize
412 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
413 Imgproc.morphologyEx(test,test, Imgproc.MORPH_OPEN, denoize);
414 showWaitDestroy("Denoize", test);
415
416 //close up gaps
417 Mat gapcloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
418 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, gapcloser);
419 showWaitDestroy("gap closer", test);
420
421 Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 50));
422 Imgproc.erode(test,test, kernelRemoveLarge);
423 showWaitDestroy("Remove Large", test);
424
425 //start staff line detection
426
427 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
428 Imgproc.erode(test,test,kernelErode);
429 showWaitDestroy("01 Erode", test);
430
431 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
432 Imgproc.dilate(test,test,kernelDilate);
433 showWaitDestroy("02 Dilate", test);
434
435 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
436 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
437 showWaitDestroy("03 Open", test);
438
439 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
440 Imgproc.erode(test,test,kernelErode02);
441 showWaitDestroy("04 Erode (Final)", test);
442
443
444 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
445 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
446 Mat hierarchy = new Mat();
447
448 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
449 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
450 //(hierarchy) output array: Optional output vector, containing information about the image topology.
451 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
452
453 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
454
455 //Draw contours and record areas
456 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
457 Mat drawing2 = Mat.zeros(test.size(), CvType.CV_8UC3);
458 int areaCounter = 0;
459 for (int i = 0; i < contours.size(); i++) {
460 double area = Imgproc.contourArea(contours.get(i));
461 if(area > THRESHOLD_AREA_SIZE ) {
462 areaCounter++;
463 Scalar color = new Scalar(0, 0, 255);
464 Imgproc.drawContours(drawing, contours, i, color, 1);
465 System.out.println("AREA: " + area);
466 }
467 }
468
469 //Classifier Calculation
470 if(areaCounter >= THRESHOLD_AREA_COUNT){
471 System.out.println("THIS IS SHEET MUSIC");
472 System.out.println(areaCounter);
473 }
474
475
476 //Show in a window
477 showWaitDestroy("Contours", drawing);
478 }
479 //Better morphology attempt - dynamic
480 if(codeVersion ==5) {
481 int hori = original.width();
482 int vert = original.height();
483 //Find ratio between 100 and width and 100 and height
484 int sizeX100 = 10 * (hori/68);
485 int sizeY100 = 10 * (vert/46);
486 int sizeX10 = (hori/68);
487 int sizeY10 = (vert/46);
488 int sizeX1 = (hori/68)/10;
489 int sizeY1 = (vert/46)/10;
490
491 //SizeX should always be a 68th * 10. Based off the defualt tester image "coo.*"
492 //SizeT should always be a 46th * 10
493
494 System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1);
495 //Display Original
496 showWaitDestroy("original", original1);
497
498 Mat test = original.clone();
499 showWaitDestroy("00 Inverse Binarized Original", test);
500
501 //Remove very large and wide black spaces (8th of the page)
502 //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8));
503 //Imgproc.erode(test,test, kernelRemoveLarge);
504 //showWaitDestroy("Remove Large", test);
505
506 //Eliminate things that are not long and thin
507 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
508 Imgproc.erode(test,test,kernelErode);
509 showWaitDestroy("01 Erode", test);
510
511 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
512 Imgproc.dilate(test,test,kernelDilate);
513 showWaitDestroy("02 Dilate", test);
514
515 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
516 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
517 showWaitDestroy("03 Open", test);
518
519 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
520 Imgproc.erode(test,test,kernelErode02);
521 showWaitDestroy("04 Erode (Final)", test);
522
523
524 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
525 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
526 Mat hierarchy = new Mat();
527
528 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
529 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
530 //(hierarchy) output array: Optional output vector, containing information about the image topology.
531 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
532
533 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
534
535 //Draw contours and record areas
536 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
537 int areaCounter = 0;
538 for (int i = 0; i < contours.size(); i++) {
539 double area = Imgproc.contourArea(contours.get(i));
540 if(area > THRESHOLD_AREA_SIZE ) {
541 areaCounter++;
542 Scalar color = new Scalar(0, 0, 255);
543 Imgproc.drawContours(drawing, contours, i, color, 1);
544 System.out.println("AREA: " + area);
545 }
546 }
547
548 //Classifier Calculation
549 if(areaCounter >= THRESHOLD_AREA_COUNT){
550 System.out.println("THIS IS SHEET MUSIC");
551 System.out.println(areaCounter);
552 }
553
554
555 //Show in a window
556 showWaitDestroy("Contours", drawing);
557 }
558 //ADD PRE PROC
559
560 //USE stuc element, to rule out large wide and long pieces of black and white.
561
562
563 //****************MORPHOLOGY****************************************************************************************
564
565 //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
566
567 //Display Results
568 //HighGui.imshow("Source", original);
569 //HighGui.imshow("Just Edges", justEdges); //TESTING
570
571
572 imshow("LINESFOUND", edgesDetectedRGB);
573 HighGui.resizeWindow("LINESFOUND", 1000,1000);
574
575 //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
576 //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
577
578 //COUNT OF LINES CLASSIFICATION
579 //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
580 //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
581 //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
582
583 //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
584
585 // Wait and Exit
586 HighGui.waitKey();
587 System.exit(0);
588 }
589 catch(Exception e){
590 System.err.println(e);
591 }
592 }
593}
Note: See TracBrowser for help on using the repository browser.