Changeset 33243 for other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java
- Timestamp:
- 2019-06-27T15:19:39+12:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java
r33221 r33243 12 12 import javax.imageio.ImageIO; 13 13 import java.util.logging.Logger; 14 import java.util.ArrayList; 14 15 15 16 //REFERENCES: … … 28 29 public class javaImageClassifier{ 29 30 //Constants 30 static int CLASSIFIER_HOUGHLINESP_MIN = 5; 31 static int CLASSIFIER_HOUGHLINESP_MAX = 40; 31 32 static int CLASSIFIER_HOUGHLINESP_MIN = 10; 33 static int CLASSIFIER_HOUGHLINESP_MAX = 65; 34 static int HOUGHLINEP_THRESHOLD = 10; 35 static int MINLINECOUNT = 40; //50 36 static double MAXLINEGAP = 4; 37 static double SLOPEGRADIENT = 0.02; //0.01 32 38 33 39 public static void main(String[] args) { … … 37 43 } 38 44 else { 45 ArrayList result_refined = null; 39 46 Boolean result = null; 40 47 String imageFilename = args[0]; 41 48 String classifierType = args[1]; 42 String outputFilename = args[2]; 49 String outputFilename = args[2]; 50 //Prep Writing output to disc 51 File log = new File(outputFilename); 52 FileWriter fileWriter = new FileWriter(log, true); 53 BufferedWriter bw = new BufferedWriter(fileWriter); 43 54 //Execute classifierType defined from arguement 55 56 //Split output by tab for processing in next java program 57 //imageFilename = 1, result = 3, classifierType = 4 44 58 switch(classifierType){ 45 59 case "houghlinesP": 46 60 result = setup_HoughLinesP(imageFilename); //true or false 61 bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result + '\t' + classifierType + '\n'); 47 62 break; 48 63 case "houghlinesP-refined": 49 result = setup_HoughLinesP_refined(imageFilename); 64 result_refined = setup_HoughLinesP_refined(imageFilename); 65 bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result_refined.get(0) + '\t' + "Number of lines:" + '\t' + result_refined.get(1) + '\t' + classifierType + '\n'); 50 66 break; 51 67 default: … … 53 69 break; 54 70 } 55 //Write output to disc 56 File log = new File(outputFilename); 57 FileWriter fileWriter = new FileWriter(log, true); 58 BufferedWriter bw = new BufferedWriter(fileWriter); 59 //Split output by tab for processing in next java program 60 //imageFilename = 1, result = 3, classifierType = 4 61 bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result + '\t' + classifierType + '\n'); 71 62 72 bw.close(); 63 73 } … … 81 91 Mat edgesDetected = new Mat(); 82 92 Mat edgesDetectedRGB = new Mat(); 93 Mat edgesExtra = new Mat(); 83 94 Mat edgesDetectedRGBProb; 84 95 // Load an image … … 108 119 } 109 120 110 private static Booleansetup_HoughLinesP_refined(String filename){121 private static ArrayList setup_HoughLinesP_refined(String filename){ 111 122 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 112 123 Boolean isSheetMusic = null; 124 ArrayList returnArray = new ArrayList(); 113 125 try{ 126 114 127 //Variables 128 int horizontalLineCount =0; 115 129 Mat edgesDetected = new Mat(); 116 130 Mat edgesDetectedRGB = new Mat(); 131 Mat edgesExtra = new Mat(); 117 132 Mat edgesDetectedRGBProb; 118 133 // Load an image 119 134 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 120 135 // Edge detection 121 Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); 136 //Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); 137 Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2); 138 //Imgproc.medianBlur(edgesExtra, edgesDetected, 3); 139 140 122 141 //Copy edges to the images that will display the results in BGR 123 142 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR); 124 143 // Probabilistic Line Transform 125 144 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 detection128 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 10, minLineLength, 5);// remote testing145 double minLineLength = edgesDetectedRGB.size().width/8; 146 //Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 10, minLineLength, MAXLINEGAP);// runs the actual detection 147 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength, MAXLINEGAP); //TESTING 129 148 // Draw the lines 130 149 … … 139 158 //System.out.println(l[2]); 140 159 //System.out.println(l[3]); 141 if(m< 0.1) {160 if(m<SLOPEGRADIENT) { 142 161 //System.out.println("m: " + m); 143 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); 162 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); 163 horizontalLineCount++; 144 164 } 145 165 } … … 148 168 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 149 169 //Calculate if its sheet music or not 150 isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg); 151 170 isSheetMusic = classifier_HoughLinesP_refined(horizontalLineCount); 171 returnArray.add(isSheetMusic); 172 returnArray.add(horizontalLineCount); 152 173 153 174 } … … 155 176 System.err.println(e); 156 177 } 157 return isSheetMusic;178 return returnArray; 158 179 } 159 180 … … 193 214 return false; 194 215 } 216 217 private static boolean classifier_HoughLinesP_refined(int lineCount){ 218 if(lineCount>MINLINECOUNT){ 219 return true; 220 } 221 else{ 222 return false; 223 } 224 } 225 195 226 private static BufferedImage toBufferedImage(Mat mat){ 196 227 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Note:
See TracChangeset
for help on using the changeset viewer.