Changeset 33243 for other-projects/is-sheet-music-encore/trunk/image-identification-development/src/Main.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-development/src/Main.java
r33170 r33243 4 4 import org.opencv.imgcodecs.Imgcodecs; 5 5 import org.opencv.imgproc.Imgproc; 6 import org.opencv.photo.Photo; 6 7 import static org.opencv.imgcodecs.Imgcodecs.imwrite; 7 8 import java.awt.image.BufferedImage; 8 9 import java.awt.image.DataBufferByte; 9 10 import java.io.File; 11 import java.util.ArrayList; 10 12 import javax.imageio.ImageIO; 11 13 … … 37 39 38 40 //GLOBAL_CONSTANTS 41 static int CLASSIFIER_HOUGHLINESP_MIN = 10; 42 static int CLASSIFIER_HOUGHLINESP_MAX = 65; 43 static int HOUGHLINEP_THRESHOLD = 10; 44 static int MINLINECOUNT = 40; 45 static double MAXLINEGAP = 4; 46 static double SLOPEGRADIENT = 0.02; 39 47 //SHOULD TURN INTO ARGS 40 48 … … 59 67 return null; 60 68 } 61 62 private static boolean Classifier(BufferedImage img){ 69 private static boolean ClassifierPixelCount(BufferedImage img){ 63 70 try { 64 71 //Read file … … 85 92 percentage = ((float)redCount/(float)pixelCount)*(float)100; 86 93 //If more than %10 and less than %50 then its sheet music! 87 if(percentage > 10 && percentage < 50){ //MAKE THESE CONSTANTS!!94 if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){ //MAKE THESE CONSTANTS!! 88 95 return true;} 89 96 } … … 93 100 return false; 94 101 } 102 private static boolean Classifier(int lineCount){ 103 104 if(lineCount>MINLINECOUNT){ 105 return true; 106 } 107 else{ 108 return false; 109 } 110 } 111 95 112 96 113 public static void main(String[] args) { 97 114 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 98 115 try { 116 //temp array for terminalversion 117 118 ArrayList returnArray = new ArrayList(); 119 returnArray.add(true); 120 returnArray.add(10); 121 122 99 123 //Variables 100 124 Mat edgesDetected = new Mat(); 101 125 Mat edgesDetectedRGB = new Mat(); 126 Mat edgesExtra = new Mat(); 102 127 Mat edgesDetectedRGBProb; 103 128 Mat edgeDoesntMakeSense; 104 129 Mat justEdges; //TESTING 130 131 String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/"; 132 //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 133 //mdp.39015097852365-2.png 176 lines Contents page. 134 //mdp.39015097852555-3.png 76 lines 135 String default_file = directory+"SheetMusic/mdp.39015080972303-3.png"; 136 //String default_file ="TestImages/NotNot/mdp.39015080972303-3.png"; 137 138 139 //System.out.println(default_file); 105 140 //String default_file = "TestImages/NotSheetMusic01.png"; 106 String default_file = "TestImages/NotSheetMusic02.png";141 //String default_file = "TestImages/NotSheetMusic02.png"; 107 142 //String default_file = "TestImages/SheetMusic01.png"; 108 143 //String default_file = "TestImages/SheetMusic02.png"; 109 144 //String default_file = "TestImages/vLine.png"; 110 145 String filename = ((args.length > 0) ? args[0] : default_file); 146 File file = new File(filename); 147 if(!file.exists()){System.err.println("Image not found: "+ filename);} 148 149 int horizontalLineCount =0; 111 150 112 151 // Load an image … … 114 153 // Edge detection 115 154 //01 CANNY 116 Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); 155 //Imgproc.Canny(original, edgesDetected, 50, 200, 3, false); 156 //Imgproc.Canny(original, edgesDetected,0, 100, 3, false ); 117 157 //Imgproc.Canny(original, edgesDetected,80, 120); 118 158 //02 BINARYINV 119 //Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2); 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); 120 163 //03 BINARY 121 164 //Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,15, 2); … … 127 170 128 171 172 173 129 174 //Convert to RGB for future use 130 175 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR); … … 136 181 //(edgeDetectedImage, outputOfDetection(r,Ξ), resolution of rho, resolution of theta, threshold (minimum num of intersections) 137 182 138 double minLineLength = edgesDetectedRGB.size().width/4; 139 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, minLineLength, 10); // runs the actual detection 140 141 183 double minLineLength = edgesDetectedRGB.size().width/8; 184 185 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()); 142 188 143 189 //Imgproc.HoughLinesP(edgesDetected,linesP,1,Math.PI/2, 50, 80, 5); … … 170 216 171 217 172 173 174 175 176 if(m<=0.1) { 218 if(m<=SLOPEGRADIENT) { 177 219 //System.out.println("m: " + m); 178 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); 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); 221 horizontalLineCount++; 179 222 } 180 223 … … 189 232 190 233 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 191 System.out.println("Result: " + Classifier(toBeClassifiedImg)); 192 234 System.out.println("Result: " + Classifier(horizontalLineCount)); 235 236 237 System.out.println(); 193 238 //Display Results 194 //HighGui.imshow("Source", original);239 HighGui.imshow("Source", original); 195 240 //HighGui.imshow("Just Edges", justEdges); //TESTING 196 241 HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB);
Note:
See TracChangeset
for help on using the changeset viewer.