Changeset 33304 for other-projects/is-sheet-music-encore/trunk/image-identification-development/src/Main.java
- Timestamp:
- 2019-07-06T15:45:02+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
r33243 r33304 10 10 import java.io.File; 11 11 import java.util.ArrayList; 12 import java.util.Collection; 13 import java.util.Collections; 12 14 import javax.imageio.ImageIO; 13 15 … … 17 19 //https://www.wikihow.com/Calculate-Percentage-in-Java 18 20 //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object 21 //https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/ 22 //https://www.programiz.com/java-programming/examples/standard-deviation 23 //https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/ 24 19 25 20 26 … … 38 44 public class Main { 39 45 46 //DEPENDENT FUNCTIONS AND CLASSES 47 static class StartAndEndPoint { 48 //PRIVATES 49 private Point _p1; 50 private Point _p2; 51 //CONSTRUCTOR 52 public StartAndEndPoint(Point p1, Point p2){ 53 _p1 = p1; 54 _p2 = p2; 55 } 56 //GETTERS 57 public Point getP1(){ 58 return _p1; 59 } 60 public Point getP2(){ 61 return _p2; 62 } 63 //SETTERS 64 public void setP1(Point p1){ 65 _p1 = p1; 66 } 67 public void setP2(Point p2){ 68 _p2 = p2; 69 } 70 71 //ToString 72 public String toString(){ 73 return "Start: " + _p1 + " End: " + _p2; 74 } 75 /* 76 //CompareToOverride 77 //Compares start point y co ordinates of input PointArray 78 //With this. start point y co ordinate 79 @Override 80 public double compareTo(StartAndEndPoint comparePointArray){ 81 Point comparePoint = (comparePointArray.getP1()); 82 return (this.getP1().y) - (comparePoint.y); 83 } 84 */ 85 } 86 public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) { 87 //DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED 88 // Function to remove duplicates from an ArrayList 89 // Create a new ArrayList 90 ArrayList<T> newList = new ArrayList(); 91 // Traverse through the first list 92 for (T element : list) { 93 // If this element is not present in newList 94 // then add it 95 if (!newList.contains(element)) { 96 newList.add(element); 97 } 98 } 99 // return the new list 100 return newList; 101 //DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED 102 } 103 public static double StandardDeviation(double parseArray[]) 104 { 105 double mean; 106 double sum =0; 107 double standardDeviation = 0; 108 //calculate sum of array 109 for(int i =0; i > parseArray.length; i++){ 110 sum += parseArray[i]; 111 } 112 //calculate mean of array 113 mean = sum/parseArray.length; 114 //calculate SD of array 115 for(int j =0; j > parseArray.length; j++){ 116 standardDeviation += Math.pow(parseArray[j]-mean, 2); 117 } 118 return Math.sqrt(standardDeviation/parseArray.length); 119 } 120 40 121 //GLOBAL_CONSTANTS 41 122 static int CLASSIFIER_HOUGHLINESP_MIN = 10; … … 43 124 static int HOUGHLINEP_THRESHOLD = 10; 44 125 static int MINLINECOUNT = 40; 45 static double MAXLINEGAP = 4;126 static double MAXLINEGAP = 1; //4 46 127 static double SLOPEGRADIENT = 0.02; 47 128 //SHOULD TURN INTO ARGS 48 129 130 //CLASSIFYING FUNCTIONS 49 131 private static BufferedImage toBufferedImage(Mat mat){ 50 132 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! … … 100 182 return false; 101 183 } 102 private static boolean Classifier (int lineCount){184 private static boolean ClassifierLineCount(int lineCount){ 103 185 104 186 if(lineCount>MINLINECOUNT){ … … 109 191 } 110 192 } 193 private static ArrayList ClassifierLineClusterOLD(BufferedImage img){ 194 ArrayList returnArray = new ArrayList(); 195 try { 196 197 //IF THIS WORKS THEN IMPLEMENT A VERSION THAT USES POINTS from the draw line code. 198 //ALSO CHECK OUT K NEAREST NEIGHBOR? 199 //0xFFFF0000 = RED 200 201 //go thru every pixel until find red pixel 202 //get y pos of red pixel 203 //continue with loop until find another red pixel 204 //get y pos of red pixel 205 //compare y pos (if close together then continue loop) else break 206 207 int x = img.getWidth(); 208 int y = img.getHeight(); 209 int closeLineCount = 0; 210 ArrayList<Integer> redPixelYpos = new ArrayList<Integer>(); 211 212 213 214 //Go Thru every pixel 215 for(int i=0; i < y; i++){ 216 for(int j=0;j < x; j++){ 217 //Get value for current pixels RGB value 218 int currPixelRGB = img.getRGB(j, i); 219 //Check if pixel is red (hex value of red) 220 if(currPixelRGB == 0xFFFF0000) { 221 222 //Store y pos of red pixel if there is no duplicate 223 if(!redPixelYpos.contains(i)){ 224 redPixelYpos.add(i); 225 //System.out.println(i ); 226 } 227 } 228 } 229 } 230 //Check if any of the lines found are close together and that there has been more than one line found 231 if(redPixelYpos.size()>1){ 232 //go through list and compare every value 233 for(int i =0; i< redPixelYpos.size(); i++){ 234 //System.out.println("i: " +redPixelYpos.get(i)); 235 for(int j=0; j< redPixelYpos.size(); j++){ 236 //System.out.println("j: "+redPixelYpos.get(j)); 237 //Check if difference is less than 4 and the values are not duplicates. 238 if(Math.abs(redPixelYpos.get(i) - redPixelYpos.get(j)) < 4 && !redPixelYpos.get(j).equals(redPixelYpos.get(i))){ 239 closeLineCount++; 240 } 241 } 242 } 243 } 244 int clusterCount = closeLineCount/4; 245 246 if(closeLineCount >= 4){ 247 returnArray.add(true); 248 returnArray.add(closeLineCount); 249 returnArray.add(clusterCount); 250 } 251 else{ 252 returnArray.add(false); 253 returnArray.add(closeLineCount); 254 returnArray.add(clusterCount); 255 } 256 } 257 catch (Exception e) { 258 System.err.println(e); 259 } 260 return returnArray; 261 } 262 263 private static ArrayList ClassifierLineCluster(ArrayList<StartAndEndPoint> linePointsArray){ 264 ArrayList returnArray = new ArrayList(); 265 ArrayList<Double> closeLineYPos = new ArrayList(); 266 ArrayList<double[]> clusterArray = new ArrayList(); 267 int clusterCount = 0; 268 try { 269 if(linePointsArray.size()> 1) { 270 271 //Display input array TESTING PURPOSES 272 for (int i = 0; i < linePointsArray.size(); i++) { 273 System.out.println(linePointsArray.get(i).toString()); 274 } 275 276 277 //Check if y points are close together 278 //go thru list and compare values against each other 279 for (int i = 0; i < linePointsArray.size(); i++){ 280 //System.out.println("i: "+ linePointsArray.get(i).getP1().y); 281 for (int j = 0; j < linePointsArray.size(); j++) { 282 //System.out.println("j: "+ linePointsArray.get(j).getP1().y); 283 //Check if difference is less than 4 and the values are not duplicates. 284 if(Math.abs(linePointsArray.get(j).getP1().y - linePointsArray.get(i).getP1().y) < 5){ 285 if(linePointsArray.get(j).getP1().y != linePointsArray.get(i).getP1().y){ 286 closeLineYPos.add(linePointsArray.get(j).getP1().y); 287 } 288 } 289 } 290 } 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. 296 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} 308 for(int i= 0; i< closeLineYPos.size(); i++){ 309 //If last comparator is within the array bounds. 310 if(i + 3 == closeLineYPos.size()){ 311 break; 312 } 313 else{ 314 double[] tempArray = new double[4]; 315 tempArray[0] = closeLineYPos.get(i + 0); 316 tempArray[1] = closeLineYPos.get(i + 1); 317 tempArray[2] = closeLineYPos.get(i + 2); 318 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){ 322 //Store array 323 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 328 } 329 330 331 } 332 } 333 } 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 348 //SETUP RETURN ARRAY 349 if(closeLineYPos.size() >= 4){ 350 returnArray.add(true); 351 returnArray.add(closeLineYPos.size()); 352 returnArray.add(clusterCount); 353 } 354 else{ 355 returnArray.add(false); 356 returnArray.add(closeLineYPos.size()); 357 returnArray.add(clusterCount); 358 } 359 } 360 } 361 catch (Exception e) { 362 System.err.println(e); 363 } 364 return returnArray; 365 } 366 111 367 112 368 113 369 public static void main(String[] args) { 370 114 371 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 372 115 373 try { 116 //temp array for terminalversion 117 118 ArrayList returnArray = new ArrayList(); 119 returnArray.add(true); 120 returnArray.add(10); 121 374 ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>(); 122 375 123 376 //Variables 124 377 Mat edgesDetected = new Mat(); 125 378 Mat edgesDetectedRGB = new Mat(); 126 Mat edgesExtra = new Mat();127 Mat edgesDetectedRGBProb;128 Mat edgeDoesntMakeSense;129 Mat justEdges; //TESTING130 131 379 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/"; 132 380 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 133 381 //mdp.39015097852365-2.png 176 lines Contents page. 134 382 //mdp.39015097852555-3.png 76 lines 135 String default_file = directory+"SheetMusic/ mdp.39015080972303-3.png";383 String default_file = directory+"SheetMusic/coo.31924062612282-9.png"; 136 384 //String default_file ="TestImages/NotNot/mdp.39015080972303-3.png"; 137 385 … … 152 400 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 153 401 // Edge detection 154 //01 CANNY 155 //Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); 156 //Imgproc.Canny(original, edgesDetected,0, 100, 3, false ); 157 //Imgproc.Canny(original, edgesDetected,80, 120); 158 //02 BINARYINV 159 Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2); 160 161 //Imgproc.adaptiveThreshold(original, edgesExtra,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2); 162 //Imgproc.medianBlur(edgesExtra, edgesDetected, 3); 163 //03 BINARY 164 //Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,15, 2); 165 //04 NO PROC 166 //edgesDetected = original.clone(); 167 //05 OTSU THRESHOLD 168 //Imgproc.threshold(original, edgesDetected,0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU); 169 170 171 172 402 Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, 4); 173 403 174 404 //Convert to RGB for future use 175 405 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR); 176 justEdges = edgesDetectedRGB.clone();//TESTING177 edgesDetectedRGBProb = edgesDetectedRGB.clone();178 edgeDoesntMakeSense = edgesDetectedRGB.clone();179 406 180 407 Mat linesP = new Mat(); // will hold the results of the detection … … 184 411 185 412 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength,MAXLINEGAP); // runs the actual detection 186 //Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, HOUGHLINEP_THRESHOLD, minLineLength,MAXLINEGAP); // runs the actual detection 187 System.out.println("Before Graident Filtering num lines: " + linesP.rows()); 188 189 //Imgproc.HoughLinesP(edgesDetected,linesP,1,Math.PI/2, 50, 80, 5); 413 System.out.println("Before Gradient Filtering num lines: " + linesP.rows()); 414 190 415 // Draw the lines 191 416 for (int x = 0; x < linesP.rows(); x++) { 192 417 double[] l = linesP.get(x, 0); 193 194 //Find angle that line is at195 //double rho = linesP.get(x, 0)[0];196 //double theta = linesP.get(x, 0)[1];197 //double cosTheta = Math.cos(theta);198 //double sinTheta = Math.sin(theta);199 //double x0 = cosTheta * rho;200 //double y0 = sinTheta * rho;201 //double xpt1 = x0 + 1000 * (-sinTheta);202 //double ypt1 = y0 + 1000 * (cosTheta);203 //double xpt2 = x0 - 1000 * (-sinTheta);204 //double ypt2 = y0 - 1000 * (cosTheta);205 //double angle = Math.atan2((float)ypt2 - (float)ypt1, (float)xpt2 - (float)xpt1)*(Math.PI);206 //double testAngle = (ypt2 - ypt1)/(xpt2 - xpt1);207 208 //New angles209 418 Point p1 = new Point(l[0], l[1]); 210 419 Point p2 = new Point(l[2], l[3]); 211 420 double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x); 212 //System.out.println(l[0]);213 //System.out.println(l[1]);214 //System.out.println(l[2]);215 //System.out.println(l[3]);216 217 421 218 422 if(m<=SLOPEGRADIENT) { 219 //System.out.println("m: " + m); 220 Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 1, Imgproc.LINE_AA, 0); 423 Imgproc.line(edgesDetectedRGB, p1, p2, new Scalar(0, 0, 255), 1, Imgproc.LINE_4, 0); 221 424 horizontalLineCount++; 425 pointArrayList.add(new StartAndEndPoint(p1, p2)); 222 426 } 223 427 … … 226 430 //Prove by finding number of points from one end to other: 227 431 //Get width of image. 228 System.out.println("every matrix widths: "+edgesDetectedRGB.size().width);229 432 File filenameTest = new File("TestImages/NotSheetMusic02.png"); 230 433 BufferedImage i = ImageIO.read(filenameTest); 231 System.out.println("input image width: "+ i.getWidth());232 233 434 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 234 System.out.println("Result: " + Classifier(horizontalLineCount)); 235 236 237 System.out.println(); 435 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)); 440 238 441 //Display Results 239 HighGui.imshow("Source", original);442 //HighGui.imshow("Source", original); 240 443 //HighGui.imshow("Just Edges", justEdges); //TESTING 241 444 HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB);
Note:
See TracChangeset
for help on using the changeset viewer.