Changeset 33170 for other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java
- Timestamp:
- 2019-06-20T20:42:56+12:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java
r33141 r33170 33 33 public static void main(String[] args) { 34 34 try { 35 if (args.length != 2) {36 System.out.println("Usage: imageClassifier <inputFilename> <classifierType> ");35 if (args.length != 3) { 36 System.out.println("Usage: imageClassifier <inputFilename> <classifierType> <outputFilename>"); 37 37 } 38 38 else { 39 39 Boolean result = null; 40 40 String imageFilename = args[0]; 41 String classifierType = args[1]; 41 String classifierType = args[1]; 42 String outputFilename = args[2]; 42 43 //Execute classifierType defined from arguement 43 44 switch(classifierType){ 44 45 case "houghlinesP": 45 result = setup_HoughLineP(imageFilename); //true or false 46 result = setup_HoughLinesP(imageFilename); //true or false 47 break; 48 case "houghlinesP-refined": 49 result = setup_HoughLinesP_refined(imageFilename); 46 50 break; 47 51 default: 48 System.out.println("unknown ");52 System.out.println("unknown algorithm"); 49 53 break; 50 54 } 51 55 //Write output to disc 52 File log = new File( "log.txt");56 File log = new File(outputFilename); 53 57 FileWriter fileWriter = new FileWriter(log, true); 54 58 BufferedWriter bw = new BufferedWriter(fileWriter); … … 66 70 //True = 1 + Filename + Status 67 71 //False= 0 + Filename + Status 68 private static Boolean setup_HoughLineP(String filename){ 72 73 //****************** 74 //CLASSIFIER FUNCTIONS 75 //****************** 76 private static Boolean setup_HoughLinesP(String filename){ 69 77 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 70 78 Boolean isSheetMusic = null; … … 85 93 // Draw the lines 86 94 for (int x = 0; x < linesP.rows(); x++) { 87 double[] l = linesP.get(x, 0);88 Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);95 double[] l = linesP.get(x, 0); 96 Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0); 89 97 } 90 98 … … 92 100 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 93 101 //Calculate if its sheet music or not 94 isSheetMusic = classifier_HoughLineP(toBeClassifiedImg); 95 96 //Save Processed Image 97 String processedFile = filename; 98 if (isSheetMusic == true) { 99 processedFile = "proc_T_"+filename; 100 }else { 101 processedFile = "proc_F_"+filename; 102 } 103 imwrite(processedFile, edgesDetectedRGB); 102 isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg); 104 103 } 105 104 catch(Exception e){ … … 107 106 } 108 107 return isSheetMusic; 109 /*110 if (isSheetMusic == true){111 return (1 + "\t" + "Filename: " + filename + " Status: " + isSheetMusic +"\t" );112 }113 else{114 return (0 + "\t" + "Filename: " + filename + " Status: " + isSheetMusic +"\t" );115 } */116 108 } 117 109 110 private static Boolean setup_HoughLinesP_refined(String filename){ 111 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 112 Boolean isSheetMusic = null; 113 try{ 114 //Variables 115 Mat edgesDetected = new Mat(); 116 Mat edgesDetectedRGB = new Mat(); 117 Mat edgesDetectedRGBProb; 118 // Load an image 119 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 120 // Edge detection 121 Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); 122 //Copy edges to the images that will display the results in BGR 123 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR); 124 // Probabilistic Line Transform 125 Mat linesP = new Mat(); // will hold the results of the detection 126 double minLineLength = edgesDetectedRGB.size().width/4; 127 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, minLineLength, 10);// runs the actual detection 128 // Draw the lines 129 130 for (int x = 0; x < linesP.rows(); x++) { 131 double[] l = linesP.get(x, 0); 132 //New angles 133 Point p1 = new Point(l[0], l[1]); 134 Point p2 = new Point(l[2], l[3]); 135 double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x); 136 //System.out.println(l[0]); 137 //System.out.println(l[1]); 138 //System.out.println(l[2]); 139 //System.out.println(l[3]); 140 if(m<0.1) { 141 //System.out.println("m: " + m); 142 Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0); 143 } 144 } 145 146 //Convert MAT into a BufferedImage 147 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 148 //Calculate if its sheet music or not 149 isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg); 150 151 } 152 catch(Exception e){ 153 System.err.println(e); 154 } 155 return isSheetMusic; 156 } 157 118 158 //****************** 119 159 //INTERNAL FUNCTIONS 120 160 //****************** 121 private static boolean classifier_HoughLine P(BufferedImage img){161 private static boolean classifier_HoughLinesP(BufferedImage img){ 122 162 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 123 163 try { … … 143 183 percentage = ((float)redCount/(float)pixelCount)*(float)100; 144 184 //If more than %10 and less than %50 then its sheet music! 145 if(percentage > 10 && percentage < 50){185 if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){ 146 186 return true;} 147 187 }
Note:
See TracChangeset
for help on using the changeset viewer.