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

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

Have created a preprocess to remove large objects.
Need to now finalized Classifier, re try area detection.
Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
Just use area and periemter to determine if sheet music
Discuss with david before weekend perhaps?

File size: 38.7 KB
RevLine 
[33415]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;
[33437]61
[33415]62import org.opencv.highgui.HighGui;
63import org.opencv.imgcodecs.Imgcodecs;
64import org.opencv.imgproc.Imgproc;
[33437]65
[33444]66import static org.opencv.core.Core.FILLED;
[33437]67import static org.opencv.core.CvType.CV_8UC3;
68import static org.opencv.highgui.HighGui.imshow;
[33415]69import static org.opencv.imgcodecs.Imgcodecs.imwrite;
[33444]70
[33415]71import java.io.File;
72import java.util.ArrayList;
73
74//REFERENCES:
75//https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
76//https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
77//https://www.wikihow.com/Calculate-Percentage-in-Java
78//https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
79//https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
80//https://www.programiz.com/java-programming/examples/standard-deviation
81//https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/
82//https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556
83//https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class
84//https://stackoverflow.com/questions/37946482/crop-images-area-with-opencv-java
85//https://docs.opencv.org/3.4/dd/dd7/tutorial_morph_lines_detection.html
[33437]86//https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html
87//https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/moments/moments.html
88//https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
89//http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html
90//https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
[33444]91//https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values
[33415]92
93
94//GOAL for 21st
95
96
97//Classifier 01
98//Have args so can call "java image-identification-classifier01 XX XX"
99//args can be parameters in algorthim such as threshold or theta?
100//Run on 5000 images.
101//Record success rates
102//All done with makefile
103
104
105//But first understand houghline transform
106//Know what the algorithm being used is doing.
107//MAke constants for this classifier
108//Make java be able to run on CMD line
109
110public class MainMorph {
[33444]111 //CODE VERSIONS
112 static int CODE_VERSION = 8;
113 static int IMAGE_VERSION = 3;
[33415]114 //GLOBAL_CONSTANTS
115
[33437]116 static double THRESHOLD_C = 4;
117 static double THRESHOLD_AREA_SIZE = 1000;
118 static double THRESHOLD_AREA_COUNT = 2;
[33415]119
120 //
121
[33444]122 private static void imageViewer(String winName, Mat img) {
123 try {
124 //Internal display - Overview - Will Distort High Res images
125 if(IMAGE_VERSION == 1) {
126 HighGui.namedWindow(winName, HighGui.WINDOW_NORMAL);
127 HighGui.resizeWindow(winName, 1000, 1000);
128 imshow(winName, img);
[33415]129
[33444]130 HighGui.moveWindow(winName, 500, 0);
131 HighGui.waitKey(0);
[33415]132
[33444]133 HighGui.destroyWindow(winName);
134 }
135 //Internal display - Segmented - Will _NOT_ Distort High Res images
136 if(IMAGE_VERSION == 2) {
137 HighGui.namedWindow(winName, HighGui.WINDOW_AUTOSIZE);
138 HighGui.resizeWindow(winName, 1000, 1000);
139 imshow(winName, img);
140 HighGui.moveWindow(winName, 500, 0);
141 HighGui.waitKey(0);
[33437]142
[33444]143 HighGui.destroyWindow(winName);
144 }
145 //External display - Save Images for manual viewing
146 if(IMAGE_VERSION == 3) {
147 //save file (testing purposes)
148 imwrite(winName+".jpg", img);
149 }
[33437]150 }
151 catch (Exception e){
152 e.printStackTrace();
153 }
[33415]154 }
155 //MAIN
156 public static void main(String[] args) {
157 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
158 try {
159 //Variables
[33444]160 System.out.println("Running code version: " + CODE_VERSION);
[33415]161 Mat edgesDetected = new Mat();
162 Mat mid = new Mat();
163 Mat edgesDetectedRGB = new Mat();
164 Mat clustersFoundRGB = new Mat();
[33439]165
[33437]166 String testDirectory = "/Scratch/cpb16/is-sheet-music-encore/image-identification-dev-02/image-identification-development/";
[33415]167 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
168 String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
169
170 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
171 //mdp.39015097852365-2.png 176 lines Contents page.
172 //mdp.39015097852555-3.png 76 lines
173 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
174 //coo.31924062612282-9.png 8 lines
175 //String default_file = directory+"NotSheetMusic/coo.31924062612282-9.png";
176 //String default_file = directory+"NotSheetMusic/mdp.39015097852365-2.png";
[33437]177 //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY?
[33415]178 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
179 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
[33444]180 //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
181 //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png";
182 //String default_file =hiresDirectory+"MU/NotSheetMusic/mdp.39015096363935-1.png";
183 String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
[33415]184
185 //System.out.println(default_file);
[33437]186 //String default_file = "/Scratch/cpb16/is-sheet-music-encore/image-identification-terminal/TestImages/test-coo.31924062612282-9.png";
187 //String default_file = testDirectory+"TestImages/MorphTester.png";
188 //String default_file = testDirectory+"TestImages/NotSheetMusic01.png";
189 //String default_file = testDirectory+"TestImages/NotSheetMusic02.png";
190 //String default_file = testDirectory+"TestImages/SheetMusic01.png";
191 //String default_file = testDirectory+"TestImages/SheetMusic02.png";
192 //String default_file = testDirectory+"TestImages/vLine.png";
[33415]193 String filename = ((args.length > 0) ? args[0] : default_file);
194 File file = new File(filename);
195 if(!file.exists()){System.err.println("Image not found: "+ filename);}
196
197 int horizontalLineCount =0;
198
199 // Load an image
[33437]200 Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
[33444]201 System.out.println("Width: " + original1.width() + " Height: " + original1.height());
[33437]202 Mat original = original1.clone();
[33415]203
[33437]204 Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
[33415]205 //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
[33437]206 //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY);
[33415]207
208
209 //****************MORPHOLOGY****************************************************************************************
210 //ADDIOTIONAL FILTERING TO STOP STREAKS
211 //LOOK INTO STREAKS MORPHOGOLY.
212 //****************MORPHOLOGY****************************************************************************************
213
214 // Create the images that will use to extract the horizontal and vertical lines
215
[33418]216 //dynamic morphology??
[33444]217 if(CODE_VERSION == 1) {
[33418]218 int hori = original.width();
219 int vert = original.height();
220 //Find ratio between 100 and width and 100 and height
221 int divX = hori/10;
222 int divY = vert/10;
223 int sizeX = (hori/divX) * 10;
224 int sizeY = (vert/divY) * 10;
[33415]225
[33418]226 Mat test = original.clone();
[33444]227 imageViewer("Original", test);
[33415]228
[33418]229 System.out.println("hori: " + hori + '\t' + "vert: " + vert);
230 System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY);
[33415]231
[33418]232 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
233 Imgproc.erode(test,test,kernelErode);
[33444]234 imageViewer("01 Erode", test);
[33418]235
236 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
237 Imgproc.dilate(test, test, kernelDialate);
[33444]238 imageViewer("02 Dialate", test);
[33418]239
240 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
241 Imgproc.erode(test,test,kernelErodeAgain);
[33444]242 imageViewer(" 03 Erode Again", test);
[33418]243
244 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
245 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
[33444]246 imageViewer("04 Close", test);
[33418]247
248 Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
[33444]249 imageViewer("05 Binarized", test);
[33418]250
251 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
252 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
[33444]253 imageViewer(" 06 Open", test);
[33418]254
255 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
256 Imgproc.dilate(test, test, kernelDialateAgain);
[33444]257 imageViewer("07 Dialate", test);
[33418]258
259
260 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
261 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
[33444]262 imageViewer(" 08 Close Again (Final)", test);
[33418]263 }
264 //Successful hardcode for morhpology
[33444]265 if (CODE_VERSION == 2) {
[33437]266
267 //MAKE SURE BLACK & WHITE
[33418]268 Mat test = original.clone();
[33444]269 imageViewer("00 Binarized Original", test);
[33418]270
271 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
272 Imgproc.erode(test,test,kernelErode);
[33444]273 imageViewer("01 Erode", test);
[33418]274
[33437]275 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
[33418]276 Imgproc.dilate(test, test, kernelDialate);
[33444]277 imageViewer("02 Dialate", test);
[33418]278
279 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
280 Imgproc.erode(test,test,kernelErodeAgain);
[33444]281 imageViewer(" 03 Erode Again", test);
[33418]282
[33437]283 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
[33418]284 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
[33444]285 imageViewer("04 Close", test);
[33418]286
[33437]287// Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
[33444]288// imageViewer("05 Binarized", test);
[33418]289
290 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
291 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
[33444]292 imageViewer(" 06 Open", test);
[33418]293
294// Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
295// Imgproc.dilate(test, test, kernelDialateAgain);
[33444]296// imageViewer("07 Dialate", test);
[33418]297
298 //FIGURE OUT FLOOD FILL!!
299 Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2));
300
301
302 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
303 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
[33444]304 imageViewer(" 08 Close Again (Final)", test);
[33418]305
306 }
307 //Tutorial/Demo Code
[33444]308 if (CODE_VERSION == 3) {
[33418]309 Mat horizontal = original.clone();
310 Mat vertical = original.clone();
311 // Specify size on horizontal axis
312 int horizontal_size = horizontal.cols() / 50;
313 // Create structure element for extracting horizontal lines through morphology operations
314 Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2));
315 // Apply morphology operations
316 Imgproc.erode(horizontal, horizontal, horizontalStructure);
317 Imgproc.dilate(horizontal, horizontal, horizontalStructure);
318 // Show extracted horizontal lines
[33444]319 imageViewer("horizontal", horizontal);
[33418]320 // Specify size on vertical axis
321 int vertical_size = vertical.rows() / 30;
322 // Create structure element for extracting vertical lines through morphology operations
323 Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size));
324 // Apply morphology operations
325 Imgproc.erode(vertical, vertical, verticalStructure);
326 Imgproc.dilate(vertical, vertical, verticalStructure);
327 // Show extracted vertical lines
[33444]328 imageViewer("vertical", vertical);
[33418]329 // Inverse vertical image
330 Core.bitwise_not(vertical, vertical);
[33444]331 imageViewer("vertical_bit", vertical);
[33418]332 // Extract edges and smooth image according to the logic
333 // 1. extract edges
334 // 2. dilate(edges)
335 // 3. src.copyTo(smooth)
336 // 4. blur smooth img
337 // 5. smooth.copyTo(src, edges)
338 // Step 1
339 Mat edges = new Mat();
340 Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
[33444]341 imageViewer("edges", edges);
[33418]342 // Step 2
343 Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
344 Imgproc.dilate(edges, edges, kernel);
[33444]345 imageViewer("dilate", edges);
[33418]346 // Step 3
347 Mat smooth = new Mat();
348 vertical.copyTo(smooth);
349 // Step 4
350 Imgproc.blur(smooth, smooth, new Size(2, 2));
351 // Step 5
352 smooth.copyTo(vertical, edges);
353 // Show final result
[33444]354 imageViewer("smooth - final", vertical);
[33418]355 System.exit(0);
356 }
[33437]357 //Better morphology attempt - static
[33444]358 if(CODE_VERSION ==4) {
[33418]359
[33437]360 //Display Original
[33444]361 imageViewer("original", original1);
[33418]362
[33437]363 Mat test = original.clone();
[33444]364 Mat pre = original.clone();
365 Mat dst = new Mat();
[33439]366
[33444]367 imageViewer("00 Inverse Binarized Original", test);
[33418]368
[33439]369 //remove large items of no interest pre proc
370 //denoize
371 //heal
372 //fnd large images, write to a seperate mat.
373 //draw these onto orignal image(binerized) in red
374 //turn all red pixels in image to black
375
376
377 //denoize
378 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
[33444]379 Imgproc.morphologyEx(pre,pre, Imgproc.MORPH_OPEN, denoize);
380 imageViewer("Denoize - PRE", pre);
[33439]381
382 //close up gaps
[33444]383 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
384 Imgproc.morphologyEx(pre,pre,Imgproc.MORPH_CLOSE, gapCloser);
385 imageViewer("gap closer - PRE", pre);
[33439]386
[33444]387 Mat kernelHighlightLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));
388 Imgproc.erode(pre,pre, kernelHighlightLarge);
389 imageViewer("Highlight Large - PRE", pre);
[33439]390
[33444]391 //change white pixels to red
392 ArrayList<MatOfPoint> contoursPre = new ArrayList<MatOfPoint>();
393 Mat hierarchyPre = new Mat();
394
395 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
396 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
397 //(hierarchy) output array: Optional output vector, containing information about the image topology.
398 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
399
400 Imgproc.findContours(pre, contoursPre, hierarchyPre, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
401
402 //Draw contours and record areas
403
404 Mat drawingPre = Mat.zeros(test.size(), CvType.CV_8UC3);
405 Mat mask = new Mat(drawingPre.size(), CV_8UC3,new Scalar(0));
406 Scalar colorPre = new Scalar(255, 255, 255);
407 Imgproc.drawContours(mask, contoursPre, -1, new Scalar(255, 255, 255), FILLED);
408 Imgproc.fillPoly(drawingPre, contoursPre,colorPre);
409 imageViewer("DRAWINGPRE", drawingPre);
410
411
412 //FIIIIIIIIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXX
413 //Remove from main Mat
414 Core.bitwise_not(mask,mask);
415 imageViewer("MASK", mask);
416 imageViewer("TEST", test);
417
418 drawingPre.copyTo(test, mask);
419 imageViewer("COMBINE", test);
420
[33439]421 //start staff line detection
422
[33437]423 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
424 Imgproc.erode(test,test,kernelErode);
[33444]425 imageViewer("01 Erode plus pre", test);
[33418]426
[33437]427 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
428 Imgproc.dilate(test,test,kernelDilate);
[33444]429 imageViewer("02 Dilate", test);
[33437]430
431 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
432 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
[33444]433 imageViewer("03 Open", test);
[33437]434
435 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
436 Imgproc.erode(test,test,kernelErode02);
[33444]437 imageViewer("04 Erode (Final)", test);
[33437]438
439
440 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
441 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
442 Mat hierarchy = new Mat();
443
444 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
445 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
446 //(hierarchy) output array: Optional output vector, containing information about the image topology.
447 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
448
449 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
450
451 //Draw contours and record areas
452 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
453 Mat drawing2 = Mat.zeros(test.size(), CvType.CV_8UC3);
454 int areaCounter = 0;
455 for (int i = 0; i < contours.size(); i++) {
456 double area = Imgproc.contourArea(contours.get(i));
457 if(area > THRESHOLD_AREA_SIZE ) {
458 areaCounter++;
459 Scalar color = new Scalar(0, 0, 255);
460 Imgproc.drawContours(drawing, contours, i, color, 1);
461 System.out.println("AREA: " + area);
462 }
463 }
464
465 //Classifier Calculation
466 if(areaCounter >= THRESHOLD_AREA_COUNT){
467 System.out.println("THIS IS SHEET MUSIC");
468 System.out.println(areaCounter);
469 }
470
471
472 //Show in a window
[33444]473 imageViewer("Contours", drawing);
[33437]474 }
475 //Better morphology attempt - dynamic
[33444]476 if(CODE_VERSION ==5) {
[33437]477 int hori = original.width();
478 int vert = original.height();
479 //Find ratio between 100 and width and 100 and height
[33439]480 int sizeX100 = 10 * (hori/68);
481 int sizeY100 = 10 * (vert/46);
[33437]482 int sizeX10 = (hori/68);
483 int sizeY10 = (vert/46);
[33439]484 int sizeX1 = (hori/68)/10;
[33437]485 int sizeY1 = (vert/46)/10;
486
487 //SizeX should always be a 68th * 10. Based off the defualt tester image "coo.*"
488 //SizeT should always be a 46th * 10
489
490 System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1);
491 //Display Original
[33444]492 imageViewer("original", original1);
[33437]493
494 Mat test = original.clone();
[33444]495 imageViewer("00 Inverse Binarized Original", test);
[33437]496
[33439]497 //Remove very large and wide black spaces (8th of the page)
498 //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8));
499 //Imgproc.erode(test,test, kernelRemoveLarge);
[33444]500 //imageViewer("Remove Large", test);
[33439]501
502 //Eliminate things that are not long and thin
[33437]503 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
504 Imgproc.erode(test,test,kernelErode);
[33444]505 imageViewer("01 Erode", test);
[33437]506
507 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
508 Imgproc.dilate(test,test,kernelDilate);
[33444]509 imageViewer("02 Dilate", test);
[33437]510
[33439]511 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
512 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
[33444]513 imageViewer("03 Open", test);
[33437]514
515 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
516 Imgproc.erode(test,test,kernelErode02);
[33444]517 imageViewer("04 Erode (Final)", test);
[33437]518
519
520 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
521 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
522 Mat hierarchy = new Mat();
523
524 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
525 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
526 //(hierarchy) output array: Optional output vector, containing information about the image topology.
527 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
528
529 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
530
531 //Draw contours and record areas
532 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
533 int areaCounter = 0;
534 for (int i = 0; i < contours.size(); i++) {
535 double area = Imgproc.contourArea(contours.get(i));
536 if(area > THRESHOLD_AREA_SIZE ) {
537 areaCounter++;
538 Scalar color = new Scalar(0, 0, 255);
539 Imgproc.drawContours(drawing, contours, i, color, 1);
540 System.out.println("AREA: " + area);
541 }
542 }
543
544 //Classifier Calculation
545 if(areaCounter >= THRESHOLD_AREA_COUNT){
546 System.out.println("THIS IS SHEET MUSIC");
547 System.out.println(areaCounter);
548 }
549
550
551 //Show in a window
[33444]552 imageViewer("Contours", drawing);
[33437]553 }
[33444]554 //MASK UNDERSTANDING
555 if(CODE_VERSION ==6) {
556 String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
557 Mat mask = new Mat();
558 Mat dst = new Mat();
559 //Get source image and binerize
560 Mat src = Imgcodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE);
561 Imgproc.adaptiveThreshold(original1, src,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
562 imageViewer("src", src);
[33437]563
[33444]564 //Find unwanted material, then invert it so mask removes not keeps.
565 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
566 Imgproc.erode(src,mask,kernelErode);
567 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
568 Imgproc.dilate(mask, mask, kernelDilate);
569 Core.bitwise_not(mask,mask);
570 imageViewer("mask", mask);
571
572 //Copy source image to new Mat, with mask in use
573 src.copyTo(dst, mask);
574 imageViewer("dst", dst);
575
576
577
578
579 }
580 //Mask implementation
581 if(CODE_VERSION ==7) {
582
583 //Display Original
584 imageViewer("original", original1);
585
586 Mat src = original.clone();
587 Mat test = original.clone();
588 Mat mask = new Mat();
589 Mat dst = new Mat();
590
591 imageViewer("00 Inverse Binarized Original", src);
592
593 //denoize
594 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
595 Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
596 imageViewer("01 Denoize - mask", mask);
597
598 //close up gaps
599 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
600 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
601 imageViewer("02 gap closer - mask", mask);
602
603 //Isolate large items
604 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
605 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
606 imageViewer("03 Isolate Large - mask", mask);
607 Core.bitwise_not(mask,mask);
608
609 //Remove unwanted large items from image
610 src.copyTo(dst, mask);
611 imageViewer("04 Large Items Removed", dst);
612
613 //start staff line detection
614
615 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
616 Imgproc.erode(dst,test,kernelErode);
617 imageViewer("11 Erode plus pre", test);
618
619 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
620 Imgproc.dilate(test,test,kernelDilate);
621 imageViewer("12 Dilate", test);
622
623 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
624 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
625 imageViewer("13 Open", test);
626
627 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
628 Imgproc.erode(test,test,kernelErode02);
629 imageViewer("14 Erode (Final)", test);
630
631
632 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
633 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
634 Mat hierarchy = new Mat();
635
636 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
637 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
638 //(hierarchy) output array: Optional output vector, containing information about the image topology.
639 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
640
641 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
642
643 //Draw contours and record areas
644 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
645 int areaCounter = 0;
646
647
648 Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), FILLED);
649// for (int i = 0; i < contours.size(); i++) {
650// Scalar color = new Scalar(0, i, i);
651// double area = Imgproc.contourArea(contours.get(i));
652// Imgproc.drawContours(drawing, contours, i, color, FILLED);
653// System.out.println("AREA: " + area);
654//
655// }
656 imageViewer("Contours found", drawing);
657
658 //Classifier Calculation
659 if(areaCounter >= THRESHOLD_AREA_COUNT){
660 System.out.println("THIS IS SHEET MUSIC");
661 System.out.println(areaCounter);
662 }
663
664
665 }
666 //Mask implementation - HIGH RES NUMBER MOD
667 if(CODE_VERSION ==8) {
668
669 //Display Original
670 imageViewer("original", original1);
671
672 Mat src = original.clone();
673 Mat test = original.clone();
674 Mat mask = new Mat();
675 Mat dst = new Mat();
676
677 imageViewer("00 Inverse Binarized Original", src);
678
679 //Close then Open
680
681// Mat firstKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
682// Imgproc.morphologyEx(src, mask, Imgproc.MORPH_CLOSE, firstKernel);
683// imageViewer("01 Closed - mask", mask);
684//
685// Mat secondKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
686// Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, secondKernel);
687// imageViewer("02 Open - mask", mask);
688
689
690 //denoize
691 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
692 Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
693 imageViewer("01 Denoize - mask", mask);
694
695 //close up gaps
696 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
697 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
698 imageViewer("02 gap closer - mask", mask);
699
700 //Isolate large items
701 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
702 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
703 imageViewer("03 Isolate Large - mask", mask);
704 Core.bitwise_not(mask,mask);
705
706 //Remove unwanted large items from image
707 src.copyTo(dst, mask);
708 imageViewer("04 Large Items Removed", dst);
709
710 //start staff line detection
711
712 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
713 Imgproc.erode(dst,test,kernelErode);
714 imageViewer("11 Erode plus pre", test);
715
716 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
717 Imgproc.dilate(test,test,kernelDilate);
718 imageViewer("12 Dilate", test);
719
720 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
721 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
722 imageViewer("13 Open", test);
723
724 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
725 Imgproc.erode(test,test,kernelErode02);
726 imageViewer("14 Erode (Final)", test);
727
728
729 //DETECT OUTLINE AND FIND AREA OF THESE LINES.
730 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
731 Mat hierarchy = new Mat();
732
733 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
734 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
735 //(hierarchy) output array: Optional output vector, containing information about the image topology.
736 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
737
738 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
739
740 //Draw contours and record areas
741 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
742 int areaCounter = 0;
743
744 //Have created a preprocess to remove large objects.
745 //Need to now finalized Classifier, re try area detection.
746 //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
747 //Just use area and periemter to determine if sheet music
748 //Discuss with david before weekend perhaps?
749
750 Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
751// for (int i = 0; i < contours.size(); i++) {
752// Scalar color = new Scalar(0, i, i);
753// double area = Imgproc.contourArea(contours.get(i));
754// Imgproc.drawContours(drawing, contours, i, color, FILLED);
755// System.out.println("AREA: " + area);
756//
757// }
758 imageViewer("Contours found", drawing);
759
760 //Classifier Calculation
761 if(areaCounter >= THRESHOLD_AREA_COUNT){
762 System.out.println("THIS IS SHEET MUSIC");
763 System.out.println(areaCounter);
764 }
765
766
767 }
768
[33439]769 //USE stuc element, to rule out large wide and long pieces of black and white.
[33437]770
771
[33415]772 //****************MORPHOLOGY****************************************************************************************
773
[33437]774 //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
[33415]775
776 //Display Results
777 //HighGui.imshow("Source", original);
778 //HighGui.imshow("Just Edges", justEdges); //TESTING
779
780
[33437]781 imshow("LINESFOUND", edgesDetectedRGB);
[33415]782 HighGui.resizeWindow("LINESFOUND", 1000,1000);
783
784 //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
785 //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
786
787 //COUNT OF LINES CLASSIFICATION
788 //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
789 //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
790 //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
791
792 //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
793
794 // Wait and Exit
795 HighGui.waitKey();
796 System.exit(0);
797 }
798 catch(Exception e){
799 System.err.println(e);
800 }
801 }
802}
Note: See TracBrowser for help on using the repository browser.