1 | /*
|
---|
2 | StartAndEndPoint l1 = parseArray[i];
|
---|
3 | StartAndEndPoint l2 = parseArray[i+ 1];
|
---|
4 | //CHECK WHICH line starts after the other
|
---|
5 | //If l1 is starting after, then comparisons are based around l1.s
|
---|
6 | //System.out.println("l1: " + l1.getP1().x);
|
---|
7 | //System.out.println("l2: " + l2.getP1().x);
|
---|
8 |
|
---|
9 | System.out.println("1.0: L1S: " + l1.getP1().x + " larger than L2S: " + l2.getP1().x);
|
---|
10 | if(l1.getP1().x > l2.getP1().x) {
|
---|
11 | System.out.println("1.1: Comparing L1S: " + l1.getP1().x + " less than L2E: " + l2.getP2().x);
|
---|
12 | if (l1.getP1().x < l2.getP2().x) {
|
---|
13 | //AND
|
---|
14 | System.out.println("1.2: Comparing L1S: " + l1.getP1().x + " larger than L2S: " + l2.getP1().x);
|
---|
15 | if (l1.getP1().x > l2.getP1().x) {
|
---|
16 | System.out.println("1: Success. NEXT");
|
---|
17 | //IT IS INTERSECTED
|
---|
18 | continue;
|
---|
19 | }
|
---|
20 | else {
|
---|
21 | //FAILED SECOND COMPARISON
|
---|
22 | System.out.println("1: Fail");
|
---|
23 | }
|
---|
24 | }
|
---|
25 | else {
|
---|
26 | System.out.println("Checking other line");
|
---|
27 | }
|
---|
28 | System.out.println("2.0: L2S: " + l2.getP1().x + " larger than L1S: " + l1.getP1().x);
|
---|
29 | }
|
---|
30 | //If l2 is starting after, then comparisons are based around l2.s
|
---|
31 | else if(l2.getP1().x > l1.getP1().x) {
|
---|
32 | System.out.println("2.1: Comparing L2S: " + l1.getP1().x + " less than L1E: " + l2.getP2().x);
|
---|
33 | if (l2.getP1().x < l1.getP2().x) {
|
---|
34 | //AND
|
---|
35 | System.out.println("2.2: Comparing L2S: " + l2.getP1().x + " larger than L1S: " + l1.getP1().x);
|
---|
36 | if (l2.getP1().x > l1.getP1().x) {
|
---|
37 | System.out.println("2: Success");
|
---|
38 | //IT IS INTERSECTED
|
---|
39 | //continue;
|
---|
40 | }
|
---|
41 | else {
|
---|
42 | //FAILED SECOND COMPARISON
|
---|
43 | System.out.println("2: Fail");
|
---|
44 | //return false;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | else {
|
---|
48 | System.out.println("Failed second comparison RETURN FALSE");
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | //return false;
|
---|
52 | }
|
---|
53 | else{
|
---|
54 | System.out.println("NEITHER RETURN FALSE");
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | */
|
---|
58 |
|
---|
59 | import org.opencv.core.*;
|
---|
60 | import org.opencv.core.Point;
|
---|
61 | import org.opencv.highgui.HighGui;
|
---|
62 | import org.opencv.imgcodecs.Imgcodecs;
|
---|
63 | import org.opencv.imgproc.Imgproc;
|
---|
64 | import org.opencv.photo.Photo;
|
---|
65 | import static org.opencv.imgcodecs.Imgcodecs.imwrite;
|
---|
66 | import java.awt.image.BufferedImage;
|
---|
67 | import java.awt.image.DataBufferByte;
|
---|
68 | import java.io.File;
|
---|
69 | import java.util.ArrayList;
|
---|
70 | import java.util.Collection;
|
---|
71 | import java.util.Collections;
|
---|
72 | import java.util.Comparator;
|
---|
73 | import javax.imageio.ImageIO;
|
---|
74 |
|
---|
75 | //REFERENCES:
|
---|
76 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
|
---|
77 | //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
|
---|
78 | //https://www.wikihow.com/Calculate-Percentage-in-Java
|
---|
79 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
80 | //https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
|
---|
81 | //https://www.programiz.com/java-programming/examples/standard-deviation
|
---|
82 | //https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/
|
---|
83 | //https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556
|
---|
84 | //https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class
|
---|
85 | //https://stackoverflow.com/questions/37946482/crop-images-area-with-opencv-java
|
---|
86 | //https://docs.opencv.org/3.4/dd/dd7/tutorial_morph_lines_detection.html
|
---|
87 |
|
---|
88 |
|
---|
89 |
|
---|
90 | //GOAL for 21st
|
---|
91 |
|
---|
92 |
|
---|
93 | //Classifier 01
|
---|
94 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
95 | //args can be parameters in algorthim such as threshold or theta?
|
---|
96 | //Run on 5000 images.
|
---|
97 | //Record success rates
|
---|
98 | //All done with makefile
|
---|
99 |
|
---|
100 |
|
---|
101 | //But first understand houghline transform
|
---|
102 | //Know what the algorithm being used is doing.
|
---|
103 | //MAke constants for this classifier
|
---|
104 | //Make java be able to run on CMD line
|
---|
105 |
|
---|
106 | public class MainMorph {
|
---|
107 | //GLOBAL_CONSTANTS
|
---|
108 |
|
---|
109 | static double THRESHOLD_C = 4;
|
---|
110 |
|
---|
111 |
|
---|
112 | //
|
---|
113 | static class StartAndEndPoint {
|
---|
114 | //PRIVATES
|
---|
115 | private Point _p1;
|
---|
116 | private Point _p2;
|
---|
117 | //CONSTRUCTOR
|
---|
118 | public StartAndEndPoint(Point p1, Point p2){
|
---|
119 | _p1 = p1;
|
---|
120 | _p2 = p2;
|
---|
121 | }
|
---|
122 | //GETTERS
|
---|
123 | public Point getP1(){
|
---|
124 | return _p1;
|
---|
125 | }
|
---|
126 | public Point getP2(){
|
---|
127 | return _p2;
|
---|
128 | }
|
---|
129 | //SETTERS
|
---|
130 | public void setP1(Point p1){
|
---|
131 | _p1 = p1;
|
---|
132 | }
|
---|
133 | public void setP2(Point p2){
|
---|
134 | _p2 = p2;
|
---|
135 | }
|
---|
136 |
|
---|
137 | //ToString
|
---|
138 | public String toString(){
|
---|
139 | return "Start: " + _p1 + " End: " + _p2;
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|
143 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
144 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
145 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
146 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
147 | try{
|
---|
148 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
149 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
150 | byte[] b = new byte[bufferSize];
|
---|
151 | //get all the pixels
|
---|
152 | mat.get(0, 0, b);
|
---|
153 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
154 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
155 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
156 | return image;
|
---|
157 | }
|
---|
158 | catch(Exception e){
|
---|
159 | System.err.println(e);
|
---|
160 | }
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | private static void showWaitDestroy(String winname, Mat img) {
|
---|
165 | HighGui.imshow(winname, img);
|
---|
166 | HighGui.moveWindow(winname, 500, 0);
|
---|
167 | HighGui.waitKey(0);
|
---|
168 | HighGui.destroyWindow(winname);
|
---|
169 | }
|
---|
170 | //MAIN
|
---|
171 | public static void main(String[] args) {
|
---|
172 |
|
---|
173 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
174 |
|
---|
175 | try {
|
---|
176 | ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();
|
---|
177 |
|
---|
178 | //Variables
|
---|
179 | Mat edgesDetected = new Mat();
|
---|
180 | Mat mid = new Mat();
|
---|
181 | Mat edgesDetectedRGB = new Mat();
|
---|
182 | Mat clustersFoundRGB = new Mat();
|
---|
183 | String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
|
---|
184 | String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
|
---|
185 |
|
---|
186 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
187 | //mdp.39015097852365-2.png 176 lines Contents page.
|
---|
188 | //mdp.39015097852555-3.png 76 lines
|
---|
189 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
190 | //coo.31924062612282-9.png 8 lines
|
---|
191 | //String default_file = directory+"NotSheetMusic/coo.31924062612282-9.png";
|
---|
192 | //String default_file = directory+"NotSheetMusic/mdp.39015097852365-2.png";
|
---|
193 | //String default_file ="TestImages/NotNot/mdp.39015080972303-3.png";
|
---|
194 | //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
|
---|
195 | String default_file = "/Scratch/cpb16/is-sheet-music-encore/image-identification-terminal/TestImages/test-coo.31924062612282-9.png";
|
---|
196 | //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
|
---|
197 |
|
---|
198 | //System.out.println(default_file);
|
---|
199 | //String default_file = "TestImages/NotSheetMusic01.png";
|
---|
200 | //String default_file = "TestImages/NotSheetMusic02.png";
|
---|
201 | //String default_file = "TestImages/SheetMusic01.png";
|
---|
202 | //String default_file = "TestImages/SheetMusic02.png";
|
---|
203 | //String default_file = "TestImages/vLine.png";
|
---|
204 | String filename = ((args.length > 0) ? args[0] : default_file);
|
---|
205 | File file = new File(filename);
|
---|
206 | if(!file.exists()){System.err.println("Image not found: "+ filename);}
|
---|
207 |
|
---|
208 | int horizontalLineCount =0;
|
---|
209 |
|
---|
210 | // Load an image
|
---|
211 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
212 | // Edge detection
|
---|
213 |
|
---|
214 | Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
215 | //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
|
---|
216 |
|
---|
217 |
|
---|
218 |
|
---|
219 | //****************MORPHOLOGY****************************************************************************************
|
---|
220 | //ADDIOTIONAL FILTERING TO STOP STREAKS
|
---|
221 | //LOOK INTO STREAKS MORPHOGOLY.
|
---|
222 | //****************MORPHOLOGY****************************************************************************************
|
---|
223 |
|
---|
224 | //Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,8));
|
---|
225 | //Imgproc.morphologyEx(original, mid, Imgproc.MORPH_OPEN, kernel);
|
---|
226 | //Imgproc.adaptiveThreshold(mid, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, 2);
|
---|
227 |
|
---|
228 | // Create the images that will use to extract the horizontal and vertical lines
|
---|
229 | Mat horizontal = original.clone();
|
---|
230 | Mat vertical = original.clone();
|
---|
231 | // Specify size on horizontal axis
|
---|
232 | int horizontal_size = horizontal.cols() / 30;
|
---|
233 | // Create structure element for extracting horizontal lines through morphology operations
|
---|
234 | Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size,1));
|
---|
235 | // Apply morphology operations
|
---|
236 | Imgproc.erode(horizontal, horizontal, horizontalStructure);
|
---|
237 | Imgproc.dilate(horizontal, horizontal, horizontalStructure);
|
---|
238 | // Show extracted horizontal lines
|
---|
239 | showWaitDestroy("horizontal" , horizontal);
|
---|
240 | // Specify size on vertical axis
|
---|
241 | int vertical_size = vertical.rows() / 30;
|
---|
242 | // Create structure element for extracting vertical lines through morphology operations
|
---|
243 | Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size( 1,vertical_size));
|
---|
244 | // Apply morphology operations
|
---|
245 | Imgproc.erode(vertical, vertical, verticalStructure);
|
---|
246 | Imgproc.dilate(vertical, vertical, verticalStructure);
|
---|
247 | // Show extracted vertical lines
|
---|
248 | showWaitDestroy("vertical", vertical);
|
---|
249 | // Inverse vertical image
|
---|
250 | Core.bitwise_not(vertical, vertical);
|
---|
251 | showWaitDestroy("vertical_bit" , vertical);
|
---|
252 | // Extract edges and smooth image according to the logic
|
---|
253 | // 1. extract edges
|
---|
254 | // 2. dilate(edges)
|
---|
255 | // 3. src.copyTo(smooth)
|
---|
256 | // 4. blur smooth img
|
---|
257 | // 5. smooth.copyTo(src, edges)
|
---|
258 | // Step 1
|
---|
259 | Mat edges = new Mat();
|
---|
260 | Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
|
---|
261 | showWaitDestroy("edges", edges);
|
---|
262 | // Step 2
|
---|
263 | Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
|
---|
264 | Imgproc.dilate(edges, edges, kernel);
|
---|
265 | showWaitDestroy("dilate", edges);
|
---|
266 | // Step 3
|
---|
267 | Mat smooth = new Mat();
|
---|
268 | vertical.copyTo(smooth);
|
---|
269 | // Step 4
|
---|
270 | Imgproc.blur(smooth, smooth, new Size(2, 2));
|
---|
271 | // Step 5
|
---|
272 | smooth.copyTo(vertical, edges);
|
---|
273 | // Show final result
|
---|
274 | showWaitDestroy("smooth - final", vertical);
|
---|
275 | System.exit(0);
|
---|
276 |
|
---|
277 |
|
---|
278 |
|
---|
279 |
|
---|
280 | //****************MORPHOLOGY****************************************************************************************
|
---|
281 |
|
---|
282 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
283 |
|
---|
284 |
|
---|
285 | //Display Results
|
---|
286 | //HighGui.imshow("Source", original);
|
---|
287 | //HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
288 |
|
---|
289 |
|
---|
290 | HighGui.imshow("LINESFOUND", edgesDetectedRGB);
|
---|
291 | HighGui.resizeWindow("LINESFOUND", 1000,1000);
|
---|
292 |
|
---|
293 | //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
|
---|
294 | //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
|
---|
295 |
|
---|
296 | //COUNT OF LINES CLASSIFICATION
|
---|
297 | //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
|
---|
298 | //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
|
---|
299 | //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
|
---|
300 |
|
---|
301 | //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
|
---|
302 |
|
---|
303 | // Wait and Exit
|
---|
304 | HighGui.waitKey();
|
---|
305 | System.exit(0);
|
---|
306 | }
|
---|
307 | catch(Exception e){
|
---|
308 | System.err.println(e);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|