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 javax.imageio.ImageIO;
|
---|
11 |
|
---|
12 | //REFERENCES:
|
---|
13 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
|
---|
14 | //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
|
---|
15 | //https://www.wikihow.com/Calculate-Percentage-in-Java
|
---|
16 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
17 |
|
---|
18 |
|
---|
19 |
|
---|
20 | //GOAL for 21st
|
---|
21 |
|
---|
22 |
|
---|
23 | //Classifier 01
|
---|
24 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
25 | //args can be parameters in algorthim such as threshold or theta?
|
---|
26 | //Run on 5000 images.
|
---|
27 | //Record success rates
|
---|
28 | //All done with makefile
|
---|
29 |
|
---|
30 |
|
---|
31 | //But first understand houghline transform
|
---|
32 | //Know what the algorithm being used is doing.
|
---|
33 | //MAke constants for this classifier
|
---|
34 | //Make java be able to run on CMD line
|
---|
35 |
|
---|
36 | public class terminalImageClassifier {
|
---|
37 |
|
---|
38 | //GLOBAL_CONSTANTS
|
---|
39 | //SHOULD TURN INTO ARGS
|
---|
40 |
|
---|
41 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
42 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
43 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
44 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
45 | try{
|
---|
46 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
47 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
48 | byte[] b = new byte[bufferSize];
|
---|
49 | //get all the pixels
|
---|
50 | mat.get(0, 0, b);
|
---|
51 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
52 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
53 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
54 | return image;
|
---|
55 | }
|
---|
56 | catch(Exception e){
|
---|
57 | System.err.println(e);
|
---|
58 | }
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static boolean Classifier(BufferedImage img){
|
---|
63 | try {
|
---|
64 | //Read file
|
---|
65 | //BufferedImage img = ImageIO.read(new File(processedFile));
|
---|
66 | int x = img.getWidth();
|
---|
67 | int y = img.getHeight();
|
---|
68 | int pixelCount = 0;
|
---|
69 | int redCount = 0;
|
---|
70 | float percentage = 0;
|
---|
71 |
|
---|
72 | //Go Thru every pixel
|
---|
73 | for(int i=0; i < y; i++){
|
---|
74 | for(int j=0;j < x; j++){
|
---|
75 | //Get value for current pixels RGB value
|
---|
76 | int currPixelRGB = img.getRGB(j, i);
|
---|
77 | //Check if pixel is red (hex value of red)
|
---|
78 | if(currPixelRGB == 0xFFFF0000){
|
---|
79 | redCount++;
|
---|
80 | }
|
---|
81 | pixelCount++;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | //Calculate percentage of Red in image
|
---|
85 | percentage = ((float)redCount/(float)pixelCount)*(float)100;
|
---|
86 | //If more than %10 and less than %50 then its sheet music!
|
---|
87 | if(percentage > 10 && percentage < 50){ //MAKE THESE CONSTANTS!!
|
---|
88 | return true;}
|
---|
89 | }
|
---|
90 | catch (Exception e) {
|
---|
91 | System.err.println(e);
|
---|
92 | }
|
---|
93 | return false;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static void main(String[] args) {
|
---|
97 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
98 | try {
|
---|
99 | //Variables
|
---|
100 | Mat edgesDetected = new Mat();
|
---|
101 | Mat edgesDetectedRGB = new Mat();
|
---|
102 | Mat edgesDetectedRGBProb;
|
---|
103 | Mat justEdges; //TESTING
|
---|
104 | String default_file = "Test02.png";
|
---|
105 | String filename = ((args.length > 0) ? args[0] : default_file);
|
---|
106 |
|
---|
107 | // Load an image
|
---|
108 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
109 | // Edge detection
|
---|
110 | Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
|
---|
111 | //Copy edges to the images that will display the results in BGR
|
---|
112 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
113 | edgesDetectedRGBProb = edgesDetectedRGB.clone();
|
---|
114 |
|
---|
115 | justEdges = edgesDetectedRGBProb.clone();//TESTING
|
---|
116 |
|
---|
117 | // Standard Hough Line Transform
|
---|
118 | Mat lines = new Mat(); // will hold the results of the detection
|
---|
119 | //(edgeDetectedImage, outputOfDetection(r,Ξ), resolution of r, resolution of Ξ, threshold (minimum num of intersections)
|
---|
120 | Imgproc.HoughLines(edgesDetected, lines, 1.4, Math.PI / 180, 500); // runs the actual detection
|
---|
121 |
|
---|
122 | // Draw the lines
|
---|
123 | //LOOK OVER THIS AGAIN THE 1000 might be image height?
|
---|
124 | //THRESHOLD should be changed, based on img dimensions?
|
---|
125 |
|
---|
126 | for (int x = 0; x < lines.rows(); x++) {
|
---|
127 | double rho = lines.get(x, 0)[0],
|
---|
128 | theta = lines.get(x, 0)[1];
|
---|
129 | //CONVERT to Cartisean coord
|
---|
130 | double a = Math.cos(theta), b = Math.sin(theta);
|
---|
131 | double x0 = a * rho, y0 = b * rho;
|
---|
132 |
|
---|
133 | Point pt1 = new Point(Math.round(x0 + 1000 * (-b)), Math.round(y0 + 1000 * (a)));
|
---|
134 | Point pt2 = new Point(Math.round(x0 - 1000 * (-b)), Math.round(y0 - 1000 * (a)));
|
---|
135 | Imgproc.line(edgesDetectedRGB, pt1, pt2, new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Probabilistic Line Transform
|
---|
139 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
140 | Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, 50, 10); // runs the actual detection
|
---|
141 | // Draw the lines
|
---|
142 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
143 | double[] l = linesP.get(x, 0);
|
---|
144 | Imgproc.line(edgesDetectedRGBProb, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
|
---|
145 | }
|
---|
146 |
|
---|
147 | //Convert MAT into a BufferedImage
|
---|
148 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGBProb);
|
---|
149 |
|
---|
150 | //Calculate if its sheet music or not
|
---|
151 | Boolean isSheetMusic = Classifier(toBeClassifiedImg);
|
---|
152 | System.out.println("Filename: " + filename + " Status: " + isSheetMusic);
|
---|
153 |
|
---|
154 | //Save Processed Image
|
---|
155 | String processedFile = null;
|
---|
156 | if (isSheetMusic == true) {
|
---|
157 | //NEED FIGURE OUT HOW RUN IN SCRIPT. THEN USE ARGS as filename + "HoughLineP + "png"
|
---|
158 | processedFile = "SheetMusic/Test_HoughLineP.png";
|
---|
159 | }else {
|
---|
160 | processedFile = "Test_HoughLineP.png";
|
---|
161 | }
|
---|
162 | imwrite(processedFile, edgesDetectedRGBProb);
|
---|
163 |
|
---|
164 | //Display Results
|
---|
165 | //HighGui.imshow("Source", original);
|
---|
166 | HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
167 | HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB);
|
---|
168 | //HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb);
|
---|
169 |
|
---|
170 | // Wait and Exit
|
---|
171 | HighGui.waitKey();
|
---|
172 | System.exit(0);
|
---|
173 | }
|
---|
174 | catch(Exception e){
|
---|
175 | System.err.println(e);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|