Changeset 33324 for other-projects/is-sheet-music-encore/trunk/image-identification-development/src/Main.java
- Timestamp:
- 2019-07-11T17:49:42+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
r33310 r33324 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 1 59 import org.opencv.core.*; 2 60 import org.opencv.core.Point; … … 12 70 import java.util.Collection; 13 71 import java.util.Collections; 72 import java.util.Comparator; 14 73 import javax.imageio.ImageIO; 15 74 … … 22 81 //https://www.programiz.com/java-programming/examples/standard-deviation 23 82 //https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/ 83 //https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556 84 //https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class 24 85 25 86 … … 43 104 44 105 public class Main { 45 46 106 //DEPENDENT FUNCTIONS AND CLASSES 47 107 static class StartAndEndPoint { … … 101 161 //DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED 102 162 } 103 public static double StandardDeviation(double parseArray[]) 104 { 163 public static double StandardDeviation(double parseArray[]) { 105 164 106 165 double mean; … … 121 180 122 181 } 182 public static Boolean ClusterCheck(StartAndEndPoint parseArray[]){ 183 System.out.println("LENGTH: " + parseArray.length); 184 //MAKE THREE COMPARISONS 185 //After clusters have been found. 186 //Check if their x positions intersect 187 //Logic being 188 //(L1.S < L2.E && L1.S > L2.S) 189 //or 190 //(L2.S < L1.E && L2.S > L1.S) 191 //Variance is using Start of line point. 192 //USING VARIANTS 193 double sum =0; 194 double temp =0; 195 double mean, variance; 196 int size = parseArray.length; 197 //Calculate sum of array 198 for(int i =0; i < parseArray.length; i++){ 199 sum += parseArray[i].getP1().y; 200 } 201 //Calculate mean of array 202 mean = sum/parseArray.length; 203 //Calculate variants 204 for(int i =0; i < size; i++){ 205 temp += Math.pow((parseArray[i].getP1().y-mean),2); 206 } 207 variance = Math.abs(temp/(size -1)); 208 System.out.println("VARIANCE: " + variance); 209 if(variance <= CLUSTER_DISTANCE_MAX && variance > CLUSTER_DISTANCE_MIN){ 210 for(int i = 0; i < 3; i++){ 211 double l1_S = parseArray[i].getP1().x; 212 double l1_E = parseArray[i].getP2().x; 213 double l2_S = 214 215 } 216 } 217 return false; 218 } 123 219 124 220 //GLOBAL_CONSTANTS 221 static double CLUSTER_DISTANCE_MAX = 15; 222 static double CLUSTER_DISTANCE_MIN = 2; 125 223 static int CLASSIFIER_HOUGHLINESP_MIN = 10; 126 224 static int CLASSIFIER_HOUGHLINESP_MAX = 65; … … 128 226 static int STANDARD_DEVIATION_THRESHOLD = 6; 129 227 static int MINLINECOUNT = 40; 130 static double MAXLINEGAP= 1; //4228 static int MAXLINEGAP = 1; //4 131 229 static double SLOPEGRADIENT = 0.02; 132 230 //SHOULD TURN INTO ARGS … … 265 363 266 364 /* 365 ADDITION: 366 After clusters have been found. 367 Check if x positions intersect at all 368 StartXPos of p1 369 267 370 This will check for a cluster of lines that are close together. 268 371 1. Go through the list of Y positions(start point) in parsed array. … … 307 410 if(linePointsArray.get(j).getP1().y != linePointsArray.get(i).getP1().y){ 308 411 closeLineYPos.add(linePointsArray.get(j).getP1().y); 412 309 413 } 310 414 } … … 325 429 //Sort array and remove duplicates 326 430 Collections.sort(closeLineYPos); 431 432 433 327 434 closeLineYPos = removeDuplicates(closeLineYPos); 435 436 //DISPLAYING AS EXCEPTED! WOO! 437 438 for (double y : closeLineYPos){ 439 System.out.println("CloseLineYPos: " + y); 440 } 328 441 if(closeLineYPos.size() >= 4) { 329 442 //FOR every item in array of CloseLines … … 334 447 } 335 448 else{ 336 //Add 4 values of Close Line Array to a tempArray 449 450 //Add 4 values of CloseLine Array to a tempArray 337 451 double[] tempArray = new double[4]; 338 452 tempArray[0] = closeLineYPos.get(i); … … 344 458 //If it SD is less than 5 then it is considered to be a cluster of lines. 345 459 460 346 461 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));462 //System.out.println("tempArray PT: "+tempArray[0] + " , " + tempArray[1] + " , " + tempArray[2] + " , " + tempArray[3]); 463 //System.out.println("tempArray SD: " + StandardDeviation(tempArray)); 349 464 //Store array 350 465 clusterArray.add(tempArray); … … 352 467 //Go down +4 positions in closeLineYPos array 353 468 if((i + 4 < closeLineYPos.size())){ 354 System.out.println("IF, i = " + i + " -> "+ (i+4) + ", CloseLineYpos size= " + closeLineYPos.size());469 //System.out.println("IF, i = " + i + " -> "+ (i+4) + ", CloseLineYpos size= " + closeLineYPos.size()); 355 470 i = i+4; 356 471 } 357 472 else{ 358 473 //break 359 System.out.println("ELSE, i = " + i+ " closeLineYpos size= " + closeLineYPos.size());474 //System.out.println("ELSE, i = " + i+ " closeLineYpos size= " + closeLineYPos.size()); 360 475 Thread.sleep(2000); 361 476 break; … … 413 528 } 414 529 530 private static ArrayList ClassifierLineClusterPt(ArrayList<StartAndEndPoint> linePointsArray, Mat clustersFoundRGB){ 531 /* 532 ADDITION: 533 This will check for a cluster of lines that are close together. 534 1. Go through the list of Y positions(start point) in parsed array. 535 If, there is a small distance between them, 536 then, add to closeLineArray. 537 538 Have all Y positions that are close to each other now. 539 Need to find the lines that are clustered together. 540 541 Now check if there are four of these are close to each other. 542 2. Go through list of closeLine. 543 Get first four lines, traversing down a step each iteration {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5} 544 If, those 4 lines are close together, 545 Then, add them to a new array that holds Line Cluster Values. 546 Go to line 4 positions down since, as do not want duplicates. 547 548 3. 549 */ 550 ArrayList returnArray = new ArrayList(); 551 ArrayList<StartAndEndPoint> closeLinePts = new ArrayList(); 552 ArrayList<StartAndEndPoint[]> clusterPtArray = new ArrayList(); 553 int clusterCount = 0; 554 try { 555 if(linePointsArray.size()> 1) { 556 /* 557 //Display input array TESTING PURPOSES 558 for (int i = 0; i < linePointsArray.size(); i++) { 559 System.out.println(linePointsArray.get(i).toString()); 560 } 561 */ 562 //1. Check if y points are close together 563 //go thru list and compare values against each other 564 for (int i = 0; i < linePointsArray.size(); i++){ 565 //System.out.println("i: "+ linePointsArray.get(i).getP1().y); 566 for (int j = 0; j < linePointsArray.size(); j++) { 567 //System.out.println("j: "+ linePointsArray.get(j).getP1().y); 568 //Check if difference is less than 4 and the values are not duplicates. 569 if(Math.abs(linePointsArray.get(j).getP1().y - linePointsArray.get(i).getP1().y) < 5){ 570 if(linePointsArray.get(j).getP1().y != linePointsArray.get(i).getP1().y){ 571 closeLinePts.add(linePointsArray.get(i)); 572 } 573 } 574 } 575 } 576 /*for (double num : closeLineYPos){ 577 System.out.println(num); 578 } */ 579 580 //2. Now check if there are four of these are close to each other. 581 //Go through all of the items in this list and check if four of them are close together 582 //Check first four items, traverse down a step {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5} 583 //If 4 items are close together, 584 //Then add them to a new array that holds Line Cluster Values. 585 //Go down 4 positions down since, as do not want duplicates. 586 587 //Now have an array of at least four lines that are close together. 588 //Sort array and remove duplicates 589 Collections.sort(closeLinePts, new Comparator<StartAndEndPoint>() { 590 @Override 591 public int compare(StartAndEndPoint p1, StartAndEndPoint p2) { 592 return (int)(p1.getP1().y - p2.getP1().y); 593 } 594 }); 595 closeLinePts = removeDuplicates(closeLinePts); 596 /*DISPLAYING AS EXCEPTED! WOO! 597 for (StartAndEndPoint pt : closeLinePts) { 598 System.out.println("CloseLinePTs: " + pt.getP1().y); 599 } 600 */ 601 602 if(closeLinePts.size() >= 4) { 603 //FOR every item in array of CloseLines 604 for(int i= 0; i< closeLinePts.size(); i++){ 605 //If last comparator is at end of array. 606 if(i + 4 >= closeLinePts.size()){ 607 break; 608 } 609 else{ 610 //Add 4 values of CloseLinePt Array to a tempArray 611 StartAndEndPoint[] tempPtArray = new StartAndEndPoint[4]; 612 tempPtArray[0] = closeLinePts.get(i); 613 tempPtArray[1] = closeLinePts.get(i + 1); 614 tempPtArray[2] = closeLinePts.get(i + 2); 615 tempPtArray[3] = closeLinePts.get(i + 3); 616 617 //Check standard deviation between these 4 values. 618 //If it SD is less than 5 then it is considered to be a cluster of lines. 619 if(ClusterCheck(tempPtArray)){ 620 //System.out.println("tempArray PT: "+tempArray[0] + " , " + tempArray[1] + " , " + tempArray[2] + " , " + tempArray[3]); 621 //System.out.println("tempArray SD: " + StandardDeviation(tempArray)); 622 //Store array 623 clusterPtArray.add(tempPtArray); 624 //If I + 4 is less than the size of the array then increment by 4 625 //Go down +4 positions in closeLineYPos array 626 if((i + 4 < closeLinePts.size())){ 627 //System.out.println("IF, i = " + i + " -> "+ (i+4) + ", CloseLineYpos size= " + closeLineYPos.size()); 628 i = i+4; 629 } 630 else{ 631 //break 632 //System.out.println("ELSE, i = " + i+ " closeLineYpos size= " + closeLineYPos.size()); 633 Thread.sleep(2000); 634 break; 635 } 636 } 637 } 638 } 639 } 640 /* 641 System.out.println("Cluster Coordinates: "); 642 for(double[] items : clusterArray){ 643 for(int i = 0; i <items.length; i++){ 644 System.out.println("ITEMS: "+ items[i]); 645 } 646 } 647 */ 648 //Setup Drawing clusters found. 649 //For every pt given the input array 650 for(StartAndEndPoint pt : linePointsArray){ 651 //Go through every the Arrays in the clusterArray(clustered lines) 652 for(int i =0; i < clusterPtArray.size(); i++){ 653 //Go through every item in the array 654 for(StartAndEndPoint item : clusterPtArray.get(i)) { 655 //Check if the curr item is equal to current pt 656 if (item.getP1().y == pt.getP1().y){ 657 //calculate a different colour for each line 658 //Draw a line 659 Imgproc.line(clustersFoundRGB, pt.getP1(), pt.getP2(), new Scalar(0, 255, 0), 1, Imgproc.LINE_4, 0); 660 } 661 } 662 } 663 664 } 665 666 clusterCount = clusterPtArray.size(); 667 //SETUP RETURN ARRAY 668 if(clusterCount >= 1){ 669 returnArray.add(true); 670 returnArray.add(closeLinePts.size()); 671 returnArray.add(clusterCount); 672 returnArray.add(clustersFoundRGB); 673 } 674 else{ 675 returnArray.add(false); 676 returnArray.add(closeLinePts.size()); 677 returnArray.add(clusterCount); 678 } 679 } 680 } 681 catch (Exception e) { 682 System.err.println(e); 683 } 684 return returnArray; 685 } 686 415 687 416 688 … … 435 707 436 708 //System.out.println(default_file); 437 //String default_file = "TestImages/NotSheetMusic01.png";709 String default_file = "TestImages/NotSheetMusic01.png"; 438 710 //String default_file = "TestImages/NotSheetMusic02.png"; 439 711 //String default_file = "TestImages/SheetMusic01.png"; 440 String default_file = "TestImages/SheetMusic02.png";712 //String default_file = "TestImages/SheetMusic02.png"; 441 713 //String default_file = "TestImages/vLine.png"; 442 714 String filename = ((args.length > 0) ? args[0] : default_file); … … 461 733 462 734 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength,MAXLINEGAP); // runs the actual detection 463 System.out.println("Before Gradient Filtering num lines: " + linesP.rows());735 //System.out.println("Before Gradient Filtering num lines: " + linesP.rows()); 464 736 465 737 // Draw the lines … … 489 761 //HighGui.imshow("Source", original); 490 762 //HighGui.imshow("Just Edges", justEdges); //TESTING 491 HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB);492 if(ClassifierLineCluster (pointArrayList, clustersFoundRGB).get(3) != null) {763 //HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB); 764 if(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB).get(3) != null) { 493 765 HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB); 494 766 } … … 499 771 //System.out.println("LINE COUNT RESULT: " + ClassifierLineCount(horizontalLineCount) + '\t' +"LineCount: " + horizontalLineCount); //COUNT OF LINES CLASSIFICATION 500 772 //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));773 //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)); 774 System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB)); 503 775 504 776 // Wait and Exit
Note:
See TracChangeset
for help on using the changeset viewer.