Changeset 33310 for other-projects/is-sheet-music-encore/trunk/image-identification-development/src/Main.java
- Timestamp:
- 2019-07-08T16:36:39+12:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/is-sheet-music-encore/trunk/image-identification-development/src/Main.java
r33304 r33310 103 103 public static double StandardDeviation(double parseArray[]) 104 104 { 105 105 106 double mean; 106 double sum =0 ;107 double standardDeviation = 0 ;107 double sum =0.0; 108 double standardDeviation = 0.0; 108 109 //calculate sum of array 109 for(int i =0; i >parseArray.length; i++){110 for(int i =0; i < parseArray.length; i++){ 110 111 sum += parseArray[i]; 111 112 } … … 113 114 mean = sum/parseArray.length; 114 115 //calculate SD of array 115 for(int j =0; j >parseArray.length; j++){116 for(int j =0; j < parseArray.length; j++){ 116 117 standardDeviation += Math.pow(parseArray[j]-mean, 2); 117 118 } 118 119 return Math.sqrt(standardDeviation/parseArray.length); 120 121 119 122 } 120 123 … … 123 126 static int CLASSIFIER_HOUGHLINESP_MAX = 65; 124 127 static int HOUGHLINEP_THRESHOLD = 10; 128 static int STANDARD_DEVIATION_THRESHOLD = 6; 125 129 static int MINLINECOUNT = 40; 126 130 static double MAXLINEGAP = 1; //4 … … 210 214 ArrayList<Integer> redPixelYpos = new ArrayList<Integer>(); 211 215 212 213 214 216 //Go Thru every pixel 215 217 for(int i=0; i < y; i++){ … … 260 262 return returnArray; 261 263 } 262 263 private static ArrayList ClassifierLineCluster(ArrayList<StartAndEndPoint> linePointsArray){ 264 private static ArrayList ClassifierLineCluster(ArrayList<StartAndEndPoint> linePointsArray, Mat clustersFoundRGB){ 265 266 /* 267 This will check for a cluster of lines that are close together. 268 1. Go through the list of Y positions(start point) in parsed array. 269 If, there is a small distance between them, 270 then, add to closeLineArray. 271 272 Have all Y positions that are close to each other now. 273 Need to find the lines that are clustered together. 274 275 Now check if there are four of these are close to each other. 276 2. Go through list of closeLine. 277 Get first four lines, traversing down a step each iteration {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5} 278 If, those 4 lines are close together, 279 Then, add them to a new array that holds Line Cluster Values. 280 Go to line 4 positions down since, as do not want duplicates. 281 282 3. 283 */ 284 264 285 ArrayList returnArray = new ArrayList(); 265 286 ArrayList<Double> closeLineYPos = new ArrayList(); … … 269 290 if(linePointsArray.size()> 1) { 270 291 292 /* 271 293 //Display input array TESTING PURPOSES 272 294 for (int i = 0; i < linePointsArray.size(); i++) { 273 295 System.out.println(linePointsArray.get(i).toString()); 274 296 } 275 276 277 // Check if y points are close together297 */ 298 299 //1. Check if y points are close together 278 300 //go thru list and compare values against each other 279 301 for (int i = 0; i < linePointsArray.size(); i++){ … … 289 311 } 290 312 } 291 292 System.out.println(" "); 293 294 //Have all y coordinates that close to each other. 295 //Now check if there are four of these are close to each other. 313 /*for (double num : closeLineYPos){ 314 System.out.println(num); 315 } */ 316 317 //2. Now check if there are four of these are close to each other. 318 //Go through all of the items in this list and check if four of them are close together 319 //Check first four items, traverse down a step {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5} 320 //If 4 items are close together, 321 //Then add them to a new array that holds Line Cluster Values. 322 //Go down 4 positions down since, as do not want duplicates. 323 324 //Now have an array of at least four lines that are close together. 325 //Sort array and remove duplicates 326 Collections.sort(closeLineYPos); 327 closeLineYPos = removeDuplicates(closeLineYPos); 296 328 if(closeLineYPos.size() >= 4) { 297 //Sort array and remove duplicates 298 Collections.sort(closeLineYPos); 299 closeLineYPos = removeDuplicates(closeLineYPos); 300 301 302 /*for (double num : closeLineYPos){ 303 System.out.println(num); 304 } */ 305 306 307 //Check first four items, traverse down a step {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5} 329 //FOR every item in array of CloseLines 308 330 for(int i= 0; i< closeLineYPos.size(); i++){ 309 //If last comparator is within the array bounds.310 if(i + 3 == closeLineYPos.size()){331 //If last comparator is at end of array. 332 if(i + 4 >= closeLineYPos.size()){ 311 333 break; 312 334 } 313 335 else{ 336 //Add 4 values of Close Line Array to a tempArray 314 337 double[] tempArray = new double[4]; 315 tempArray[0] = closeLineYPos.get(i + 0);338 tempArray[0] = closeLineYPos.get(i); 316 339 tempArray[1] = closeLineYPos.get(i + 1); 317 340 tempArray[2] = closeLineYPos.get(i + 2); 318 341 tempArray[3] = closeLineYPos.get(i + 3); 319 System.out.println(tempArray[0] + " , " + tempArray[1] + " , " + tempArray[2] + " , " + tempArray[3]); 320 //Check standard deviation 321 if(StandardDeviation(tempArray) < 5){ 342 343 //Check standard deviation between these 4 values. 344 //If it SD is less than 5 then it is considered to be a cluster of lines. 345 346 if(StandardDeviation(tempArray) < STANDARD_DEVIATION_THRESHOLD){ 347 System.out.println("tempArray PT: "+tempArray[0] + " , " + tempArray[1] + " , " + tempArray[2] + " , " + tempArray[3]); 348 System.out.println("tempArray SD: " + StandardDeviation(tempArray)); 322 349 //Store array 323 350 clusterArray.add(tempArray); 324 //Check if more than one item in array 325 if(clusterArray.size() > 1){ 326 //check for duplicate yPos in stored arrays (tempArray) 327 351 //If I + 4 is less than the size of the array then increment by 4 352 //Go down +4 positions in closeLineYPos array 353 if((i + 4 < closeLineYPos.size())){ 354 System.out.println("IF, i = " + i + " -> "+ (i+4) + ", CloseLineYpos size= " + closeLineYPos.size()); 355 i = i+4; 328 356 } 329 330 357 else{ 358 //break 359 System.out.println("ELSE, i = " + i+ " closeLineYpos size= " + closeLineYPos.size()); 360 Thread.sleep(2000); 361 break; 362 } 331 363 } 332 364 } 333 365 } 334 335 //for (double num : closeLineYPos){ 336 // System.out.println(num); 337 //} 338 } 339 340 //PROBLEM. Definition of cluster. Need to check if cluster. 341 //check if four lines are close to each other.(four for loops) 342 // then store these four items in an array and add one to the counter. 343 // (will need to check if found 5th item. - DONT NEED TO? Value gained from finding the 5th line? The staffline height?) 344 // 345 346 347 366 } 367 /* 368 System.out.println("Cluster Coordinates: "); 369 for(double[] items : clusterArray){ 370 for(int i = 0; i <items.length; i++){ 371 System.out.println("ITEMS: "+ items[i]); 372 } 373 } 374 */ 375 //Setup Drawing clusters found. 376 //For every pt given the input array 377 for(StartAndEndPoint pt : linePointsArray){ 378 //Go through every the Arrays in the clusterArray(clustered lines) 379 for(int i =0; i < clusterArray.size(); i++){ 380 //Go through every item in the array 381 for(double item : clusterArray.get(i)) { 382 //Check if the curr item is equal to current pt 383 if (item == pt.getP1().y){ 384 //calculate a different colour for each line 385 386 //Draw a line 387 Imgproc.line(clustersFoundRGB, pt.getP1(), pt.getP2(), new Scalar(0, 255, 0), 1, Imgproc.LINE_4, 0); 388 } 389 } 390 } 391 392 } 393 394 clusterCount = clusterArray.size(); 348 395 //SETUP RETURN ARRAY 349 if(cl oseLineYPos.size() >= 4){396 if(clusterCount >= 1){ 350 397 returnArray.add(true); 351 398 returnArray.add(closeLineYPos.size()); 352 399 returnArray.add(clusterCount); 400 returnArray.add(clustersFoundRGB); 353 401 } 354 402 else{ … … 377 425 Mat edgesDetected = new Mat(); 378 426 Mat edgesDetectedRGB = new Mat(); 427 Mat clustersFoundRGB = new Mat(); 379 428 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/"; 380 429 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 381 430 //mdp.39015097852365-2.png 176 lines Contents page. 382 431 //mdp.39015097852555-3.png 76 lines 383 String default_file = directory+"SheetMusic/coo.31924062612282-9.png";432 //String default_file = directory+"SheetMusic/coo.31924062612282-9.png"; 384 433 //String default_file ="TestImages/NotNot/mdp.39015080972303-3.png"; 385 434 … … 389 438 //String default_file = "TestImages/NotSheetMusic02.png"; 390 439 //String default_file = "TestImages/SheetMusic01.png"; 391 //String default_file = "TestImages/SheetMusic02.png";440 String default_file = "TestImages/SheetMusic02.png"; 392 441 //String default_file = "TestImages/vLine.png"; 393 442 String filename = ((args.length > 0) ? args[0] : default_file); … … 404 453 //Convert to RGB for future use 405 454 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR); 455 clustersFoundRGB = edgesDetectedRGB.clone(); 406 456 407 457 Mat linesP = new Mat(); // will hold the results of the detection … … 434 484 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 435 485 436 System.out.println("LINE COUNT RESULT: " + ClassifierLineCount(horizontalLineCount) + '\t' +"LineCount: " + horizontalLineCount); //COUNT OF LINES CLASSIFICATION 437 System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2)); 438 //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineCluster(pointArrayList).get(0) + '\t' + "LinesFound: " + ClassifierLineCluster(pointArrayList).get(1) + '\t' + "ClustersFound: " + ClassifierLineCluster(pointArrayList).get(2)); 439 System.out.println(ClassifierLineCluster(pointArrayList)); 486 440 487 441 488 //Display Results … … 443 490 //HighGui.imshow("Just Edges", justEdges); //TESTING 444 491 HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB); 492 if(ClassifierLineCluster(pointArrayList, clustersFoundRGB).get(3) != null) { 493 HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB); 494 } 445 495 //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb); 446 //HighGui.imshow("Detected Lines (in red) - edgeDoesntMakeSense", edgeDoesntMakeSense); 496 497 498 499 //System.out.println("LINE COUNT RESULT: " + ClassifierLineCount(horizontalLineCount) + '\t' +"LineCount: " + horizontalLineCount); //COUNT OF LINES CLASSIFICATION 500 //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2)); 501 System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineCluster(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + ClassifierLineCluster(pointArrayList,clustersFoundRGB).get(1) + '\t' + "ClustersFound: " + ClassifierLineCluster(pointArrayList,clustersFoundRGB).get(2)); 502 //System.out.println(ClassifierLineCluster(pointArrayList, clustersFoundRGB)); 447 503 448 504 // Wait and Exit
Note:
See TracChangeset
for help on using the changeset viewer.