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 | import 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
|
---|
29 | public 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; //50
|
---|
36 | static double MAXLINEGAP = 4;
|
---|
37 | static double SLOPEGRADIENT = 0.02; //0.01
|
---|
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.medianBlur(edgesExtra, edgesDetected, 3);
|
---|
139 |
|
---|
140 |
|
---|
141 | //Copy edges to the images that will display the results in BGR
|
---|
142 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
143 | // Probabilistic Line Transform
|
---|
144 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
145 | 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
|
---|
148 | // Draw the lines
|
---|
149 |
|
---|
150 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
151 | double[] l = linesP.get(x, 0);
|
---|
152 | //New angles
|
---|
153 | Point p1 = new Point(l[0], l[1]);
|
---|
154 | Point p2 = new Point(l[2], l[3]);
|
---|
155 | double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
|
---|
156 | //System.out.println(l[0]);
|
---|
157 | //System.out.println(l[1]);
|
---|
158 | //System.out.println(l[2]);
|
---|
159 | //System.out.println(l[3]);
|
---|
160 | if(m<SLOPEGRADIENT) {
|
---|
161 | //System.out.println("m: " + m);
|
---|
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++;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | //Convert MAT into a BufferedImage
|
---|
168 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
169 | //Calculate if its sheet music or not
|
---|
170 | isSheetMusic = classifier_HoughLinesP_refined(horizontalLineCount);
|
---|
171 | returnArray.add(isSheetMusic);
|
---|
172 | returnArray.add(horizontalLineCount);
|
---|
173 |
|
---|
174 | }
|
---|
175 | catch(Exception e){
|
---|
176 | System.err.println(e);
|
---|
177 | }
|
---|
178 | return returnArray;
|
---|
179 | }
|
---|
180 |
|
---|
181 | //******************
|
---|
182 | //INTERNAL FUNCTIONS
|
---|
183 | //******************
|
---|
184 | private static boolean classifier_HoughLinesP(BufferedImage img){
|
---|
185 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
186 | try {
|
---|
187 | //Read file
|
---|
188 | int x = img.getWidth();
|
---|
189 | int y = img.getHeight();
|
---|
190 | int pixelCount = 0;
|
---|
191 | int redCount = 0;
|
---|
192 | float percentage = 0;
|
---|
193 | //Go Thru every pixel
|
---|
194 | for(int i=0; i < y; i++){
|
---|
195 | for(int j=0;j < x; j++){
|
---|
196 | //Get value for current pixels RGB value
|
---|
197 | int currPixelRGB = img.getRGB(j, i);
|
---|
198 | //Check if pixel is red (hex value of red)
|
---|
199 | if(currPixelRGB == 0xFFFF0000){
|
---|
200 | redCount++;
|
---|
201 | }
|
---|
202 | pixelCount++;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | //Calculate percentage of Red in image
|
---|
206 | percentage = ((float)redCount/(float)pixelCount)*(float)100;
|
---|
207 | //If more than %10 and less than %50 then its sheet music!
|
---|
208 | if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){
|
---|
209 | return true;}
|
---|
210 | }
|
---|
211 | catch (Exception e) {
|
---|
212 | System.err.println(e);
|
---|
213 | }
|
---|
214 | return false;
|
---|
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 |
|
---|
226 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
227 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
228 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
229 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
230 | try{
|
---|
231 |
|
---|
232 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
233 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
234 | byte[] b = new byte[bufferSize];
|
---|
235 | //get all the pixels
|
---|
236 | mat.get(0, 0, b);
|
---|
237 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
238 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
239 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
240 | return image;
|
---|
241 | }
|
---|
242 | catch(Exception e){
|
---|
243 | System.err.println(e);
|
---|
244 | }
|
---|
245 | return null;
|
---|
246 | }
|
---|
247 | }
|
---|