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

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

made progress with morphology. Need to have a better area dimension threshold setup

File size: 26.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;
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 ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();
196
197 //Variables
198 int codeVersion = 5;
199 Mat edgesDetected = new Mat();
200 Mat mid = new Mat();
201 Mat edgesDetectedRGB = new Mat();
202 Mat clustersFoundRGB = new Mat();
203 String testDirectory = "/Scratch/cpb16/is-sheet-music-encore/image-identification-dev-02/image-identification-development/";
204 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
205 String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
206
207 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
208 //mdp.39015097852365-2.png 176 lines Contents page.
209 //mdp.39015097852555-3.png 76 lines
210 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
211 //coo.31924062612282-9.png 8 lines
212 //String default_file = directory+"NotSheetMusic/coo.31924062612282-9.png";
213 //String default_file = directory+"NotSheetMusic/mdp.39015097852365-2.png";
214 //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY?
215 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
216 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
217 //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
218
219 //System.out.println(default_file);
220 //String default_file = "/Scratch/cpb16/is-sheet-music-encore/image-identification-terminal/TestImages/test-coo.31924062612282-9.png";
221 //String default_file = testDirectory+"TestImages/MorphTester.png";
222 //String default_file = testDirectory+"TestImages/NotSheetMusic01.png";
223 //String default_file = testDirectory+"TestImages/NotSheetMusic02.png";
224 //String default_file = testDirectory+"TestImages/SheetMusic01.png";
225 //String default_file = testDirectory+"TestImages/SheetMusic02.png";
226 //String default_file = testDirectory+"TestImages/vLine.png";
227 String filename = ((args.length > 0) ? args[0] : default_file);
228 File file = new File(filename);
229 if(!file.exists()){System.err.println("Image not found: "+ filename);}
230
231 int horizontalLineCount =0;
232
233 // Load an image
234 Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
235 Mat original = original1.clone();
236
237 Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
238 //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
239 //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY);
240
241
242 //****************MORPHOLOGY****************************************************************************************
243 //ADDIOTIONAL FILTERING TO STOP STREAKS
244 //LOOK INTO STREAKS MORPHOGOLY.
245 //****************MORPHOLOGY****************************************************************************************
246
247 // Create the images that will use to extract the horizontal and vertical lines
248
249 //dynamic morphology??
250 if(codeVersion == 1) {
251 int hori = original.width();
252 int vert = original.height();
253 //Find ratio between 100 and width and 100 and height
254 int divX = hori/10;
255 int divY = vert/10;
256 int sizeX = (hori/divX) * 10;
257 int sizeY = (vert/divY) * 10;
258
259 Mat test = original.clone();
260 showWaitDestroy("Original", test);
261
262 System.out.println("hori: " + hori + '\t' + "vert: " + vert);
263 System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY);
264
265 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
266 Imgproc.erode(test,test,kernelErode);
267 showWaitDestroy("01 Erode", test);
268
269 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
270 Imgproc.dilate(test, test, kernelDialate);
271 showWaitDestroy("02 Dialate", test);
272
273 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
274 Imgproc.erode(test,test,kernelErodeAgain);
275 showWaitDestroy(" 03 Erode Again", test);
276
277 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
278 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
279 showWaitDestroy("04 Close", test);
280
281 Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
282 showWaitDestroy("05 Binarized", test);
283
284 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
285 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
286 showWaitDestroy(" 06 Open", test);
287
288 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
289 Imgproc.dilate(test, test, kernelDialateAgain);
290 showWaitDestroy("07 Dialate", test);
291
292
293 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
294 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
295 showWaitDestroy(" 08 Close Again (Final)", test);
296 }
297 //Successful hardcode for morhpology
298 if (codeVersion == 2) {
299
300 //MAKE SURE BLACK & WHITE
301 Mat test = original.clone();
302 showWaitDestroy("00 Binarized Original", test);
303
304 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
305 Imgproc.erode(test,test,kernelErode);
306 showWaitDestroy("01 Erode", test);
307
308 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
309 Imgproc.dilate(test, test, kernelDialate);
310 showWaitDestroy("02 Dialate", test);
311
312 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
313 Imgproc.erode(test,test,kernelErodeAgain);
314 showWaitDestroy(" 03 Erode Again", test);
315
316 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
317 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
318 showWaitDestroy("04 Close", test);
319
320// Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
321// showWaitDestroy("05 Binarized", test);
322
323 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
324 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
325 showWaitDestroy(" 06 Open", test);
326
327// Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
328// Imgproc.dilate(test, test, kernelDialateAgain);
329// showWaitDestroy("07 Dialate", test);
330
331 //FIGURE OUT FLOOD FILL!!
332 Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2));
333
334
335 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
336 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
337 showWaitDestroy(" 08 Close Again (Final)", test);
338
339 }
340 //Tutorial/Demo Code
341 if (codeVersion == 3) {
342 Mat horizontal = original.clone();
343 Mat vertical = original.clone();
344 // Specify size on horizontal axis
345 int horizontal_size = horizontal.cols() / 50;
346 // Create structure element for extracting horizontal lines through morphology operations
347 Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2));
348 // Apply morphology operations
349 Imgproc.erode(horizontal, horizontal, horizontalStructure);
350 Imgproc.dilate(horizontal, horizontal, horizontalStructure);
351 // Show extracted horizontal lines
352 showWaitDestroy("horizontal", horizontal);
353 // Specify size on vertical axis
354 int vertical_size = vertical.rows() / 30;
355 // Create structure element for extracting vertical lines through morphology operations
356 Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size));
357 // Apply morphology operations
358 Imgproc.erode(vertical, vertical, verticalStructure);
359 Imgproc.dilate(vertical, vertical, verticalStructure);
360 // Show extracted vertical lines
361 showWaitDestroy("vertical", vertical);
362 // Inverse vertical image
363 Core.bitwise_not(vertical, vertical);
364 showWaitDestroy("vertical_bit", vertical);
365 // Extract edges and smooth image according to the logic
366 // 1. extract edges
367 // 2. dilate(edges)
368 // 3. src.copyTo(smooth)
369 // 4. blur smooth img
370 // 5. smooth.copyTo(src, edges)
371 // Step 1
372 Mat edges = new Mat();
373 Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
374 showWaitDestroy("edges", edges);
375 // Step 2
376 Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
377 Imgproc.dilate(edges, edges, kernel);
378 showWaitDestroy("dilate", edges);
379 // Step 3
380 Mat smooth = new Mat();
381 vertical.copyTo(smooth);
382 // Step 4
383 Imgproc.blur(smooth, smooth, new Size(2, 2));
384 // Step 5
385 smooth.copyTo(vertical, edges);
386 // Show final result
387 showWaitDestroy("smooth - final", vertical);
388 System.exit(0);
389 }
390 //Better morphology attempt - static
391 if(codeVersion ==4) {
392
393 //Display Original
394 showWaitDestroy("original", original1);
395
396 Mat test = original.clone();
397 showWaitDestroy("00 Inverse Binarized Original", test);
398
399 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
400 Imgproc.erode(test,test,kernelErode);
401 showWaitDestroy("01 Erode", test);
402
403 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
404 Imgproc.dilate(test,test,kernelDilate);
405 showWaitDestroy("02 Dilate", test);
406
407 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
408 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
409 showWaitDestroy("03 Open", test);
410
411 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
412 Imgproc.erode(test,test,kernelErode02);
413 showWaitDestroy("04 Erode (Final)", test);
414
415
416 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
417 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
418 Mat hierarchy = new Mat();
419
420 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
421 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
422 //(hierarchy) output array: Optional output vector, containing information about the image topology.
423 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
424
425 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
426
427 //Draw contours and record areas
428 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
429 Mat drawing2 = Mat.zeros(test.size(), CvType.CV_8UC3);
430 int areaCounter = 0;
431 for (int i = 0; i < contours.size(); i++) {
432 double area = Imgproc.contourArea(contours.get(i));
433 if(area > THRESHOLD_AREA_SIZE ) {
434 areaCounter++;
435 Scalar color = new Scalar(0, 0, 255);
436 Imgproc.drawContours(drawing, contours, i, color, 1);
437 System.out.println("AREA: " + area);
438 }
439 }
440
441 //Classifier Calculation
442 if(areaCounter >= THRESHOLD_AREA_COUNT){
443 System.out.println("THIS IS SHEET MUSIC");
444 System.out.println(areaCounter);
445 }
446
447
448 //Show in a window
449 showWaitDestroy("Contours", drawing);
450 }
451 //Better morphology attempt - dynamic
452 if(codeVersion ==5) {
453 int hori = original.width();
454 int vert = original.height();
455 //Find ratio between 100 and width and 100 and height
456 int sizeX100 = (hori/68) * 10;
457 int sizeY100 = (vert/46) * 10;
458 int sizeX10 = (hori/68);
459 int sizeY10 = (vert/46);
460 int sizeX1 = (hori/46)/10;
461 int sizeY1 = (vert/46)/10;
462
463 //SizeX should always be a 68th * 10. Based off the defualt tester image "coo.*"
464 //SizeT should always be a 46th * 10
465
466 System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1);
467 //Display Original
468 showWaitDestroy("original", original1);
469
470 Mat test = original.clone();
471 showWaitDestroy("00 Inverse Binarized Original", test);
472
473 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
474 Imgproc.erode(test,test,kernelErode);
475 showWaitDestroy("01 Erode", test);
476
477 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
478 Imgproc.dilate(test,test,kernelDilate);
479 showWaitDestroy("02 Dilate", test);
480
481 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
482 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
483 showWaitDestroy("03 Open", test);
484
485 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
486 Imgproc.erode(test,test,kernelErode02);
487 showWaitDestroy("04 Erode (Final)", test);
488
489
490 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
491 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
492 Mat hierarchy = new Mat();
493
494 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
495 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
496 //(hierarchy) output array: Optional output vector, containing information about the image topology.
497 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
498
499 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
500
501 //Draw contours and record areas
502 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
503 int areaCounter = 0;
504 for (int i = 0; i < contours.size(); i++) {
505 double area = Imgproc.contourArea(contours.get(i));
506 if(area > THRESHOLD_AREA_SIZE ) {
507 areaCounter++;
508 Scalar color = new Scalar(0, 0, 255);
509 Imgproc.drawContours(drawing, contours, i, color, 1);
510 System.out.println("AREA: " + area);
511 }
512 }
513
514 //Classifier Calculation
515 if(areaCounter >= THRESHOLD_AREA_COUNT){
516 System.out.println("THIS IS SHEET MUSIC");
517 System.out.println(areaCounter);
518 }
519
520
521 //Show in a window
522 showWaitDestroy("Contours", drawing);
523 }
524
525
526
527 //****************MORPHOLOGY****************************************************************************************
528
529 //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
530
531 //Display Results
532 //HighGui.imshow("Source", original);
533 //HighGui.imshow("Just Edges", justEdges); //TESTING
534
535
536 imshow("LINESFOUND", edgesDetectedRGB);
537 HighGui.resizeWindow("LINESFOUND", 1000,1000);
538
539 //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
540 //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
541
542 //COUNT OF LINES CLASSIFICATION
543 //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
544 //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
545 //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
546
547 //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
548
549 // Wait and Exit
550 HighGui.waitKey();
551 System.exit(0);
552 }
553 catch(Exception e){
554 System.err.println(e);
555 }
556 }
557}
Note: See TracBrowser for help on using the repository browser.