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 org.opencv.photo.Photo;
|
---|
7 | import static org.opencv.imgcodecs.Imgcodecs.imwrite;
|
---|
8 | import java.awt.image.BufferedImage;
|
---|
9 | import java.awt.image.DataBufferByte;
|
---|
10 | import java.io.File;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import javax.imageio.ImageIO;
|
---|
13 |
|
---|
14 | //REFERENCES:
|
---|
15 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
|
---|
16 | //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
|
---|
17 | //https://www.wikihow.com/Calculate-Percentage-in-Java
|
---|
18 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
19 |
|
---|
20 |
|
---|
21 |
|
---|
22 | //GOAL for 21st
|
---|
23 |
|
---|
24 |
|
---|
25 | //Classifier 01
|
---|
26 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
27 | //args can be parameters in algorthim such as threshold or theta?
|
---|
28 | //Run on 5000 images.
|
---|
29 | //Record success rates
|
---|
30 | //All done with makefile
|
---|
31 |
|
---|
32 |
|
---|
33 | //But first understand houghline transform
|
---|
34 | //Know what the algorithm being used is doing.
|
---|
35 | //MAke constants for this classifier
|
---|
36 | //Make java be able to run on CMD line
|
---|
37 |
|
---|
38 | public class Main {
|
---|
39 |
|
---|
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;
|
---|
47 | //SHOULD TURN INTO ARGS
|
---|
48 |
|
---|
49 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
50 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
51 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
52 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
53 | try{
|
---|
54 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
55 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
56 | byte[] b = new byte[bufferSize];
|
---|
57 | //get all the pixels
|
---|
58 | mat.get(0, 0, b);
|
---|
59 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
60 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
61 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
62 | return image;
|
---|
63 | }
|
---|
64 | catch(Exception e){
|
---|
65 | System.err.println(e);
|
---|
66 | }
|
---|
67 | return null;
|
---|
68 | }
|
---|
69 | private static boolean ClassifierPixelCount(BufferedImage img){
|
---|
70 | try {
|
---|
71 | //Read file
|
---|
72 | //BufferedImage img = ImageIO.read(new File(processedFile));
|
---|
73 | int x = img.getWidth();
|
---|
74 | int y = img.getHeight();
|
---|
75 | int pixelCount = 0;
|
---|
76 | int redCount = 0;
|
---|
77 | float percentage = 0;
|
---|
78 |
|
---|
79 | //Go Thru every pixel
|
---|
80 | for(int i=0; i < y; i++){
|
---|
81 | for(int j=0;j < x; j++){
|
---|
82 | //Get value for current pixels RGB value
|
---|
83 | int currPixelRGB = img.getRGB(j, i);
|
---|
84 | //Check if pixel is red (hex value of red)
|
---|
85 | if(currPixelRGB == 0xFFFF0000){
|
---|
86 | redCount++;
|
---|
87 | }
|
---|
88 | pixelCount++;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | //Calculate percentage of Red in image
|
---|
92 | percentage = ((float)redCount/(float)pixelCount)*(float)100;
|
---|
93 | //If more than %10 and less than %50 then its sheet music!
|
---|
94 | if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){ //MAKE THESE CONSTANTS!!
|
---|
95 | return true;}
|
---|
96 | }
|
---|
97 | catch (Exception e) {
|
---|
98 | System.err.println(e);
|
---|
99 | }
|
---|
100 | return false;
|
---|
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 |
|
---|
112 |
|
---|
113 | public static void main(String[] args) {
|
---|
114 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
115 | try {
|
---|
116 | //temp array for terminalversion
|
---|
117 |
|
---|
118 | ArrayList returnArray = new ArrayList();
|
---|
119 | returnArray.add(true);
|
---|
120 | returnArray.add(10);
|
---|
121 |
|
---|
122 |
|
---|
123 | //Variables
|
---|
124 | Mat edgesDetected = new Mat();
|
---|
125 | Mat edgesDetectedRGB = new Mat();
|
---|
126 | Mat edgesExtra = new Mat();
|
---|
127 | Mat edgesDetectedRGBProb;
|
---|
128 | Mat edgeDoesntMakeSense;
|
---|
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);
|
---|
140 | //String default_file = "TestImages/NotSheetMusic01.png";
|
---|
141 | //String default_file = "TestImages/NotSheetMusic02.png";
|
---|
142 | //String default_file = "TestImages/SheetMusic01.png";
|
---|
143 | //String default_file = "TestImages/SheetMusic02.png";
|
---|
144 | //String default_file = "TestImages/vLine.png";
|
---|
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;
|
---|
150 |
|
---|
151 | // Load an image
|
---|
152 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
153 | // Edge detection
|
---|
154 | //01 CANNY
|
---|
155 | //Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
|
---|
156 | //Imgproc.Canny(original, edgesDetected,0, 100, 3, false );
|
---|
157 | //Imgproc.Canny(original, edgesDetected,80, 120);
|
---|
158 | //02 BINARYINV
|
---|
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);
|
---|
163 | //03 BINARY
|
---|
164 | //Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,15, 2);
|
---|
165 | //04 NO PROC
|
---|
166 | //edgesDetected = original.clone();
|
---|
167 | //05 OTSU THRESHOLD
|
---|
168 | //Imgproc.threshold(original, edgesDetected,0,255,Imgproc.THRESH_BINARY_INV+Imgproc.THRESH_OTSU);
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 | //Convert to RGB for future use
|
---|
175 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
176 | justEdges = edgesDetectedRGB.clone();//TESTING
|
---|
177 | edgesDetectedRGBProb = edgesDetectedRGB.clone();
|
---|
178 | edgeDoesntMakeSense = edgesDetectedRGB.clone();
|
---|
179 |
|
---|
180 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
181 | //(edgeDetectedImage, outputOfDetection(r,Ξ), resolution of rho, resolution of theta, threshold (minimum num of intersections)
|
---|
182 |
|
---|
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());
|
---|
188 |
|
---|
189 | //Imgproc.HoughLinesP(edgesDetected,linesP,1,Math.PI/2, 50, 80, 5);
|
---|
190 | // Draw the lines
|
---|
191 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
192 | double[] l = linesP.get(x, 0);
|
---|
193 |
|
---|
194 | //Find angle that line is at
|
---|
195 | //double rho = linesP.get(x, 0)[0];
|
---|
196 | //double theta = linesP.get(x, 0)[1];
|
---|
197 | //double cosTheta = Math.cos(theta);
|
---|
198 | //double sinTheta = Math.sin(theta);
|
---|
199 | //double x0 = cosTheta * rho;
|
---|
200 | //double y0 = sinTheta * rho;
|
---|
201 | //double xpt1 = x0 + 1000 * (-sinTheta);
|
---|
202 | //double ypt1 = y0 + 1000 * (cosTheta);
|
---|
203 | //double xpt2 = x0 - 1000 * (-sinTheta);
|
---|
204 | //double ypt2 = y0 - 1000 * (cosTheta);
|
---|
205 | //double angle = Math.atan2((float)ypt2 - (float)ypt1, (float)xpt2 - (float)xpt1)*(Math.PI);
|
---|
206 | //double testAngle = (ypt2 - ypt1)/(xpt2 - xpt1);
|
---|
207 |
|
---|
208 | //New angles
|
---|
209 | Point p1 = new Point(l[0], l[1]);
|
---|
210 | Point p2 = new Point(l[2], l[3]);
|
---|
211 | double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
|
---|
212 | //System.out.println(l[0]);
|
---|
213 | //System.out.println(l[1]);
|
---|
214 | //System.out.println(l[2]);
|
---|
215 | //System.out.println(l[3]);
|
---|
216 |
|
---|
217 |
|
---|
218 | if(m<=SLOPEGRADIENT) {
|
---|
219 | //System.out.println("m: " + m);
|
---|
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++;
|
---|
222 | }
|
---|
223 |
|
---|
224 | }
|
---|
225 | //Point is a co ordinate (x, y)
|
---|
226 | //Prove by finding number of points from one end to other:
|
---|
227 | //Get width of image.
|
---|
228 | System.out.println("every matrix widths: "+edgesDetectedRGB.size().width);
|
---|
229 | File filenameTest = new File("TestImages/NotSheetMusic02.png");
|
---|
230 | BufferedImage i = ImageIO.read(filenameTest);
|
---|
231 | System.out.println("input image width: "+ i.getWidth());
|
---|
232 |
|
---|
233 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
234 | System.out.println("Result: " + Classifier(horizontalLineCount));
|
---|
235 |
|
---|
236 |
|
---|
237 | System.out.println();
|
---|
238 | //Display Results
|
---|
239 | HighGui.imshow("Source", original);
|
---|
240 | //HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
241 | HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB);
|
---|
242 | //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
|
---|
243 | //HighGui.imshow("Detected Lines (in red) - edgeDoesntMakeSense", edgeDoesntMakeSense);
|
---|
244 |
|
---|
245 | // Wait and Exit
|
---|
246 | HighGui.waitKey();
|
---|
247 | System.exit(0);
|
---|
248 | }
|
---|
249 | catch(Exception e){
|
---|
250 | System.err.println(e);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|