source: other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java@ 33304

Last change on this file since 33304 was 33304, checked in by cpb16, 5 years ago

Backup for computer crash, only lost 5 lines of code in development section. They have been rewritten.

File size: 9.3 KB
Line 
1import org.opencv.core.*;
2import org.opencv.core.Point;
3import org.opencv.highgui.HighGui;
4import org.opencv.imgcodecs.Imgcodecs;
5import org.opencv.imgproc.Imgproc;
6import static org.opencv.imgcodecs.Imgcodecs.imwrite;
7import java.awt.image.BufferedImage;
8import java.awt.image.DataBufferByte;
9import java.io.File;
10import java.io.BufferedWriter;
11import java.io.FileWriter;
12import javax.imageio.ImageIO;
13import java.util.logging.Logger;
14import java.util.ArrayList;
15
16//REFERENCES:
17//https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
18//https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
19//https://www.wikihow.com/Calculate-Percentage-in-Java
20//https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
21//https://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger
22//https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java
23
24
25//OUTPUT OF THIS JAVA PROGRAM FOUND IN log.txt
26//Each image processed will have an output of
27//True =classifierType + 1 + Filename + Status
28//False =classifierType + 0 + Filename + Status
29public class javaImageClassifier{
30 //Constants
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;
36 static double MAXLINEGAP = 4;
37 static double SLOPEGRADIENT = 0.02;
38
39 public static void main(String[] args) {
40 try {
41 if (args.length != 3) {
42 System.out.println("Usage: imageClassifier <inputFilename> <classifierType> <outputFilename>");
43 }
44 else {
45 ArrayList result_refined = null;
46 Boolean result = null;
47 String imageFilename = args[0];
48 String classifierType = args[1];
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);
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
58 switch(classifierType){
59 case "houghlinesP":
60 result = setup_HoughLinesP(imageFilename); //true or false
61 bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result + '\t' + classifierType + '\n');
62 break;
63 case "houghlinesP-refined":
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');
66 break;
67 default:
68 System.out.println("unknown algorithm");
69 break;
70 }
71
72 bw.close();
73 }
74 }
75 catch(Exception e){
76 System.err.println(e);
77 }
78 }
79 //Returns
80 //True = 1 + Filename + Status
81 //False= 0 + Filename + Status
82
83 //******************
84 //CLASSIFIER FUNCTIONS
85 //******************
86 private static Boolean setup_HoughLinesP(String filename){
87 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
88 Boolean isSheetMusic = null;
89 try{
90 //Variables
91 Mat edgesDetected = new Mat();
92 Mat edgesDetectedRGB = new Mat();
93 Mat edgesExtra = new Mat();
94 Mat edgesDetectedRGBProb;
95 // Load an image
96 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
97 // Edge detection
98 Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
99 //Copy edges to the images that will display the results in BGR
100 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
101 // Probabilistic Line Transform
102 Mat linesP = new Mat(); // will hold the results of the detection
103 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, 50, 10); // runs the actual detection
104 // Draw the lines
105 for (int x = 0; x < linesP.rows(); x++) {
106 double[] l = linesP.get(x, 0);
107 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);
108 }
109
110 //Convert MAT into a BufferedImage
111 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
112 //Calculate if its sheet music or not
113 isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg);
114 }
115 catch(Exception e){
116 System.err.println(e);
117 }
118 return isSheetMusic;
119 }
120
121 private static ArrayList setup_HoughLinesP_refined(String filename){
122 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
123 Boolean isSheetMusic = null;
124 ArrayList returnArray = new ArrayList();
125 try{
126
127 //Variables
128 int horizontalLineCount =0;
129 Mat edgesDetected = new Mat();
130 Mat edgesDetectedRGB = new Mat();
131 Mat edgesExtra = new Mat();
132 Mat edgesDetectedRGBProb;
133 // Load an image
134 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
135 // Edge detection
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.blur(edgesExtra, edgesDetected, new Size(3,1));
139 //Imgproc.medianBlur(edgesExtra, edgesDetected, 3);
140
141
142 //Copy edges to the images that will display the results in BGR
143 Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
144 // Probabilistic Line Transform
145 Mat linesP = new Mat(); // will hold the results of the detection
146 double minLineLength = edgesDetectedRGB.size().width/8;
147 //Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 10, minLineLength, MAXLINEGAP);// runs the actual detection
148 Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength, MAXLINEGAP); //TESTING
149 // Draw the lines
150
151 for (int x = 0; x < linesP.rows(); x++) {
152 double[] l = linesP.get(x, 0);
153 //New angles
154 Point p1 = new Point(l[0], l[1]);
155 Point p2 = new Point(l[2], l[3]);
156 double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
157 //System.out.println(l[0]);
158 //System.out.println(l[1]);
159 //System.out.println(l[2]);
160 //System.out.println(l[3]);
161 if(m<SLOPEGRADIENT) {
162 //System.out.println("m: " + m);
163 //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); SINCE NOT SAVING IMAGES OR COUNTING PIXELS
164 horizontalLineCount++;
165 }
166 }
167
168 //Convert MAT into a BufferedImage
169 //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); SINCE NOT SAVING IMAGES OR COUNTING PIXELS
170 //Calculate if its sheet music or not
171 isSheetMusic = classifier_HoughLinesP_refined(horizontalLineCount);
172 returnArray.add(isSheetMusic);
173 returnArray.add(horizontalLineCount);
174
175 }
176 catch(Exception e){
177 System.err.println(e);
178 }
179 return returnArray;
180 }
181
182 //******************
183 //INTERNAL FUNCTIONS
184 //******************
185 private static boolean classifier_HoughLinesP(BufferedImage img){
186 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
187 try {
188 //Read file
189 int x = img.getWidth();
190 int y = img.getHeight();
191 int pixelCount = 0;
192 int redCount = 0;
193 float percentage = 0;
194 //Go Thru every pixel
195 for(int i=0; i < y; i++){
196 for(int j=0;j < x; j++){
197 //Get value for current pixels RGB value
198 int currPixelRGB = img.getRGB(j, i);
199 //Check if pixel is red (hex value of red)
200 if(currPixelRGB == 0xFFFF0000){
201 redCount++;
202 }
203 pixelCount++;
204 }
205 }
206 //Calculate percentage of Red in image
207 percentage = ((float)redCount/(float)pixelCount)*(float)100;
208 //If more than %10 and less than %50 then its sheet music!
209 if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){
210 return true;}
211 }
212 catch (Exception e) {
213 System.err.println(e);
214 }
215 return false;
216 }
217
218 private static boolean classifier_HoughLinesP_refined(int lineCount){
219 if(lineCount>MINLINECOUNT){
220 return true;
221 }
222 else{
223 return false;
224 }
225 }
226
227 private static BufferedImage toBufferedImage(Mat mat){
228 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
229 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
230 //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
231 try{
232
233 int type = BufferedImage.TYPE_3BYTE_BGR;
234 int bufferSize = mat.channels() * mat.cols() * mat.rows();
235 byte[] b = new byte[bufferSize];
236 //get all the pixels
237 mat.get(0, 0, b);
238 BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
239 final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
240 System.arraycopy(b, 0, targetPixels, 0, b.length);
241 return image;
242 }
243 catch(Exception e){
244 System.err.println(e);
245 }
246 return null;
247 }
248}
Note: See TracBrowser for help on using the repository browser.