1 | import org.opencv.core.*;
|
---|
2 | import org.opencv.core.Point;
|
---|
3 | import org.opencv.highgui.HighGui;
|
---|
4 | import org.opencv.imgcodecs.Imgcodecs;
|
---|
5 | import org.opencv.imgproc.Imgproc;
|
---|
6 | import static org.opencv.imgcodecs.Imgcodecs.imwrite;
|
---|
7 | import java.awt.image.BufferedImage;
|
---|
8 | import java.awt.image.DataBufferByte;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.BufferedWriter;
|
---|
11 | import java.io.FileWriter;
|
---|
12 | import javax.imageio.ImageIO;
|
---|
13 | import java.util.logging.Logger;
|
---|
14 |
|
---|
15 | //REFERENCES:
|
---|
16 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
|
---|
17 | //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
|
---|
18 | //https://www.wikihow.com/Calculate-Percentage-in-Java
|
---|
19 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
20 | //https://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger
|
---|
21 | //https://stackoverflow.com/questions/9961292/write-to-text-file-without-overwriting-in-java
|
---|
22 |
|
---|
23 |
|
---|
24 | //GOAL for 21st
|
---|
25 |
|
---|
26 |
|
---|
27 | //Classifier 01
|
---|
28 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
29 | //args can be parameters in algorthim such as threshold or theta?
|
---|
30 | //Run on 5000 images.
|
---|
31 | //Record success rates
|
---|
32 | //All done with makefile
|
---|
33 |
|
---|
34 |
|
---|
35 | //But first understand houghline transform
|
---|
36 | //Know what the algorithm being used is doing.
|
---|
37 | //MAke constants for this classifier
|
---|
38 | //Make java be able to run on CMD line
|
---|
39 |
|
---|
40 | public class javaImageClassifier{
|
---|
41 |
|
---|
42 | public static void main(String[] args) {
|
---|
43 |
|
---|
44 | try {
|
---|
45 | if (args.length != 2) {
|
---|
46 | System.out.println("Usage: imageClassifier <inputFilename> <classifierType>");
|
---|
47 | }
|
---|
48 | else {
|
---|
49 | String imageFilename = args[0];
|
---|
50 | int classifierType = Integer.parseInt(args[1]);
|
---|
51 |
|
---|
52 | String result = null;
|
---|
53 | //Execute classifierType defined from arguement
|
---|
54 | switch(classifierType){
|
---|
55 | case 1:
|
---|
56 | result = classifier01(imageFilename);
|
---|
57 | break;
|
---|
58 | case 2:
|
---|
59 | System.out.println("unknown");
|
---|
60 | break;
|
---|
61 | }
|
---|
62 |
|
---|
63 | //Write output to disc
|
---|
64 | File log = new File("log.txt");
|
---|
65 | FileWriter fileWriter = new FileWriter(log, true);
|
---|
66 | BufferedWriter bw = new BufferedWriter(fileWriter);
|
---|
67 | bw.write(result + classifierType + '\n');
|
---|
68 | bw.close();
|
---|
69 |
|
---|
70 |
|
---|
71 | }
|
---|
72 | }
|
---|
73 | catch(Exception e){
|
---|
74 | System.err.println(e);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | private static String classifier01(String filename){
|
---|
78 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
79 | Boolean isSheetMusic = null;
|
---|
80 | try{
|
---|
81 | //Variables
|
---|
82 | Mat edgesDetected = new Mat();
|
---|
83 | Mat edgesDetectedRGB = new Mat();
|
---|
84 | Mat edgesDetectedRGBProb;
|
---|
85 | // Load an image
|
---|
86 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
87 | // Edge detection
|
---|
88 | Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
|
---|
89 | //Copy edges to the images that will display the results in BGR
|
---|
90 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
91 | // Probabilistic Line Transform
|
---|
92 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
93 | Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, 50, 10); // runs the actual detection
|
---|
94 | // Draw the lines
|
---|
95 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
96 | double[] l = linesP.get(x, 0);
|
---|
97 | 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);
|
---|
98 | }
|
---|
99 | //Convert MAT into a BufferedImage
|
---|
100 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
101 | //Calculate if its sheet music or not
|
---|
102 | isSheetMusic = Classifier(toBeClassifiedImg);
|
---|
103 |
|
---|
104 | //Save Processed Image
|
---|
105 | String processedFile = filename;
|
---|
106 | if (isSheetMusic == true) {
|
---|
107 | processedFile = "proc_T_"+filename;
|
---|
108 | }else {
|
---|
109 | processedFile = "proc_F_"+filename;
|
---|
110 | }
|
---|
111 | imwrite(processedFile, edgesDetectedRGB);
|
---|
112 |
|
---|
113 | //Display Results
|
---|
114 | //HighGui.imshow("Source", original);
|
---|
115 | //HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
116 | //HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB);
|
---|
117 | //HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb);
|
---|
118 |
|
---|
119 | // Wait and Exit
|
---|
120 | //HighGui.waitKey();
|
---|
121 | //System.exit(0);
|
---|
122 |
|
---|
123 | }
|
---|
124 | catch(Exception e){
|
---|
125 | System.err.println(e);
|
---|
126 | }
|
---|
127 | if (isSheetMusic == true){
|
---|
128 | return (1 + "\t" + "Filename: " + filename + " Status: " + isSheetMusic +"\t" );}
|
---|
129 |
|
---|
130 | else{
|
---|
131 | return (0 + "\t" + "Filename: " + filename + " Status: " + isSheetMusic +"\t" );}
|
---|
132 | }
|
---|
133 |
|
---|
134 | private static boolean Classifier(BufferedImage img){
|
---|
135 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
136 | try {
|
---|
137 | //Read file
|
---|
138 | //BufferedImage img = ImageIO.read(new File(processedFile));
|
---|
139 | int x = img.getWidth();
|
---|
140 | int y = img.getHeight();
|
---|
141 | int pixelCount = 0;
|
---|
142 | int redCount = 0;
|
---|
143 | float percentage = 0;
|
---|
144 |
|
---|
145 | //Go Thru every pixel
|
---|
146 | for(int i=0; i < y; i++){
|
---|
147 | for(int j=0;j < x; j++){
|
---|
148 | //Get value for current pixels RGB value
|
---|
149 | int currPixelRGB = img.getRGB(j, i);
|
---|
150 | //Check if pixel is red (hex value of red)
|
---|
151 | if(currPixelRGB == 0xFFFF0000){
|
---|
152 | redCount++;
|
---|
153 | }
|
---|
154 | pixelCount++;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | //Calculate percentage of Red in image
|
---|
158 | percentage = ((float)redCount/(float)pixelCount)*(float)100;
|
---|
159 |
|
---|
160 |
|
---|
161 | //If more than %10 and less than %50 then its sheet music!
|
---|
162 | if(percentage > 10 && percentage < 50){ //MAKE THESE CONSTANTS!!
|
---|
163 | return true;}
|
---|
164 | }
|
---|
165 | catch (Exception e) {
|
---|
166 | System.err.println(e);
|
---|
167 | }
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
172 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
173 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
174 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
175 | try{
|
---|
176 |
|
---|
177 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
178 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
179 | byte[] b = new byte[bufferSize];
|
---|
180 | //get all the pixels
|
---|
181 | mat.get(0, 0, b);
|
---|
182 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
183 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
184 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
185 | return image;
|
---|
186 | }
|
---|
187 | catch(Exception e){
|
---|
188 | System.err.println(e);
|
---|
189 | }
|
---|
190 | return null;
|
---|
191 | }
|
---|
192 |
|
---|
193 |
|
---|
194 | }
|
---|