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 | //OUTPUT OF THIS JAVA PROGRAM FOUND IN log.txt
|
---|
25 | //Each image processed will have an output of
|
---|
26 | //True =classifierType + 1 + Filename + Status
|
---|
27 | //False =classifierType + 0 + Filename + Status
|
---|
28 | public class javaImageClassifier{
|
---|
29 | //Constants
|
---|
30 | static int CLASSIFIER_HOUGHLINESP_MIN = 5;
|
---|
31 | static int CLASSIFIER_HOUGHLINESP_MAX = 40;
|
---|
32 |
|
---|
33 | public static void main(String[] args) {
|
---|
34 | try {
|
---|
35 | if (args.length != 3) {
|
---|
36 | System.out.println("Usage: imageClassifier <inputFilename> <classifierType> <outputFilename>");
|
---|
37 | }
|
---|
38 | else {
|
---|
39 | Boolean result = null;
|
---|
40 | String imageFilename = args[0];
|
---|
41 | String classifierType = args[1];
|
---|
42 | String outputFilename = args[2];
|
---|
43 | //Execute classifierType defined from arguement
|
---|
44 | switch(classifierType){
|
---|
45 | case "houghlinesP":
|
---|
46 | result = setup_HoughLinesP(imageFilename); //true or false
|
---|
47 | break;
|
---|
48 | case "houghlinesP-refined":
|
---|
49 | result = setup_HoughLinesP_refined(imageFilename);
|
---|
50 | break;
|
---|
51 | default:
|
---|
52 | System.out.println("unknown algorithm");
|
---|
53 | break;
|
---|
54 | }
|
---|
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');
|
---|
62 | bw.close();
|
---|
63 | }
|
---|
64 | }
|
---|
65 | catch(Exception e){
|
---|
66 | System.err.println(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | //Returns
|
---|
70 | //True = 1 + Filename + Status
|
---|
71 | //False= 0 + Filename + Status
|
---|
72 |
|
---|
73 | //******************
|
---|
74 | //CLASSIFIER FUNCTIONS
|
---|
75 | //******************
|
---|
76 | private static Boolean setup_HoughLinesP(String filename){
|
---|
77 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
78 | Boolean isSheetMusic = null;
|
---|
79 | try{
|
---|
80 | //Variables
|
---|
81 | Mat edgesDetected = new Mat();
|
---|
82 | Mat edgesDetectedRGB = new Mat();
|
---|
83 | Mat edgesDetectedRGBProb;
|
---|
84 | // Load an image
|
---|
85 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
86 | // Edge detection
|
---|
87 | Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
|
---|
88 | //Copy edges to the images that will display the results in BGR
|
---|
89 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
90 | // Probabilistic Line Transform
|
---|
91 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
92 | Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, 50, 10); // runs the actual detection
|
---|
93 | // Draw the lines
|
---|
94 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
95 | double[] l = linesP.get(x, 0);
|
---|
96 | 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);
|
---|
97 | }
|
---|
98 |
|
---|
99 | //Convert MAT into a BufferedImage
|
---|
100 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
101 | //Calculate if its sheet music or not
|
---|
102 | isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg);
|
---|
103 | }
|
---|
104 | catch(Exception e){
|
---|
105 | System.err.println(e);
|
---|
106 | }
|
---|
107 | return isSheetMusic;
|
---|
108 | }
|
---|
109 |
|
---|
110 | private static Boolean setup_HoughLinesP_refined(String filename){
|
---|
111 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
112 | Boolean isSheetMusic = null;
|
---|
113 | try{
|
---|
114 | //Variables
|
---|
115 | Mat edgesDetected = new Mat();
|
---|
116 | Mat edgesDetectedRGB = new Mat();
|
---|
117 | Mat edgesDetectedRGBProb;
|
---|
118 | // Load an image
|
---|
119 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
120 | // Edge detection
|
---|
121 | Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
|
---|
122 | //Copy edges to the images that will display the results in BGR
|
---|
123 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
124 | // Probabilistic Line Transform
|
---|
125 | 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 detection
|
---|
128 | Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 10, minLineLength, 5);// remote testing
|
---|
129 | // Draw the lines
|
---|
130 |
|
---|
131 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
132 | double[] l = linesP.get(x, 0);
|
---|
133 | //New angles
|
---|
134 | Point p1 = new Point(l[0], l[1]);
|
---|
135 | Point p2 = new Point(l[2], l[3]);
|
---|
136 | double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
|
---|
137 | //System.out.println(l[0]);
|
---|
138 | //System.out.println(l[1]);
|
---|
139 | //System.out.println(l[2]);
|
---|
140 | //System.out.println(l[3]);
|
---|
141 | if(m<0.1) {
|
---|
142 | //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);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | //Convert MAT into a BufferedImage
|
---|
148 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
149 | //Calculate if its sheet music or not
|
---|
150 | isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg);
|
---|
151 |
|
---|
152 |
|
---|
153 | }
|
---|
154 | catch(Exception e){
|
---|
155 | System.err.println(e);
|
---|
156 | }
|
---|
157 | return isSheetMusic;
|
---|
158 | }
|
---|
159 |
|
---|
160 | //******************
|
---|
161 | //INTERNAL FUNCTIONS
|
---|
162 | //******************
|
---|
163 | private static boolean classifier_HoughLinesP(BufferedImage img){
|
---|
164 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
165 | try {
|
---|
166 | //Read file
|
---|
167 | int x = img.getWidth();
|
---|
168 | int y = img.getHeight();
|
---|
169 | int pixelCount = 0;
|
---|
170 | int redCount = 0;
|
---|
171 | float percentage = 0;
|
---|
172 | //Go Thru every pixel
|
---|
173 | for(int i=0; i < y; i++){
|
---|
174 | for(int j=0;j < x; j++){
|
---|
175 | //Get value for current pixels RGB value
|
---|
176 | int currPixelRGB = img.getRGB(j, i);
|
---|
177 | //Check if pixel is red (hex value of red)
|
---|
178 | if(currPixelRGB == 0xFFFF0000){
|
---|
179 | redCount++;
|
---|
180 | }
|
---|
181 | pixelCount++;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | //Calculate percentage of Red in image
|
---|
185 | percentage = ((float)redCount/(float)pixelCount)*(float)100;
|
---|
186 | //If more than %10 and less than %50 then its sheet music!
|
---|
187 | if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){
|
---|
188 | return true;}
|
---|
189 | }
|
---|
190 | catch (Exception e) {
|
---|
191 | System.err.println(e);
|
---|
192 | }
|
---|
193 | return false;
|
---|
194 | }
|
---|
195 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
196 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
197 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
198 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
199 | try{
|
---|
200 |
|
---|
201 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
202 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
203 | byte[] b = new byte[bufferSize];
|
---|
204 | //get all the pixels
|
---|
205 | mat.get(0, 0, b);
|
---|
206 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
207 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
208 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
209 | return image;
|
---|
210 | }
|
---|
211 | catch(Exception e){
|
---|
212 | System.err.println(e);
|
---|
213 | }
|
---|
214 | return null;
|
---|
215 | }
|
---|
216 | }
|
---|