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 |
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | //GOAL for 21st
|
---|
90 |
|
---|
91 |
|
---|
92 | //Classifier 01
|
---|
93 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
94 | //args can be parameters in algorthim such as threshold or theta?
|
---|
95 | //Run on 5000 images.
|
---|
96 | //Record success rates
|
---|
97 | //All done with makefile
|
---|
98 |
|
---|
99 |
|
---|
100 | //But first understand houghline transform
|
---|
101 | //Know what the algorithm being used is doing.
|
---|
102 | //MAke constants for this classifier
|
---|
103 | //Make java be able to run on CMD line
|
---|
104 |
|
---|
105 | public class Main {
|
---|
106 | //DEPENDENT FUNCTIONS AND CLASSES
|
---|
107 | static class StartAndEndPoint {
|
---|
108 | //PRIVATES
|
---|
109 | private Point _p1;
|
---|
110 | private Point _p2;
|
---|
111 | //CONSTRUCTOR
|
---|
112 | public StartAndEndPoint(Point p1, Point p2){
|
---|
113 | _p1 = p1;
|
---|
114 | _p2 = p2;
|
---|
115 | }
|
---|
116 | //GETTERS
|
---|
117 | public Point getP1(){
|
---|
118 | return _p1;
|
---|
119 | }
|
---|
120 | public Point getP2(){
|
---|
121 | return _p2;
|
---|
122 | }
|
---|
123 | //SETTERS
|
---|
124 | public void setP1(Point p1){
|
---|
125 | _p1 = p1;
|
---|
126 | }
|
---|
127 | public void setP2(Point p2){
|
---|
128 | _p2 = p2;
|
---|
129 | }
|
---|
130 |
|
---|
131 | //ToString
|
---|
132 | public String toString(){
|
---|
133 | return "Start: " + _p1 + " End: " + _p2;
|
---|
134 | }
|
---|
135 | /*
|
---|
136 | //CompareToOverride
|
---|
137 | //Compares start point y co ordinates of input PointArray
|
---|
138 | //With this. start point y co ordinate
|
---|
139 | @Override
|
---|
140 | public double compareTo(StartAndEndPoint comparePointArray){
|
---|
141 | Point comparePoint = (comparePointArray.getP1());
|
---|
142 | return (this.getP1().y) - (comparePoint.y);
|
---|
143 | }
|
---|
144 | */
|
---|
145 | }
|
---|
146 | public static <T> ArrayList<T> removeDuplicates(ArrayList<T> list) {
|
---|
147 | //DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED
|
---|
148 | // Function to remove duplicates from an ArrayList
|
---|
149 | // Create a new ArrayList
|
---|
150 | ArrayList<T> newList = new ArrayList();
|
---|
151 | // Traverse through the first list
|
---|
152 | for (T element : list) {
|
---|
153 | // If this element is not present in newList
|
---|
154 | // then add it
|
---|
155 | if (!newList.contains(element)) {
|
---|
156 | newList.add(element);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | // return the new list
|
---|
160 | return newList;
|
---|
161 | //DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED//DIRECTLY COPIED
|
---|
162 | }
|
---|
163 | public static double StandardDeviation(double parseArray[]) {
|
---|
164 |
|
---|
165 | double mean;
|
---|
166 | double sum =0.0;
|
---|
167 | double standardDeviation = 0.0;
|
---|
168 | //calculate sum of array
|
---|
169 | for(int i =0; i < parseArray.length; i++){
|
---|
170 | sum += parseArray[i];
|
---|
171 | }
|
---|
172 | //calculate mean of array
|
---|
173 | mean = sum/parseArray.length;
|
---|
174 | //calculate SD of array
|
---|
175 | for(int j =0; j < parseArray.length; j++){
|
---|
176 | standardDeviation += Math.pow(parseArray[j]-mean, 2);
|
---|
177 | }
|
---|
178 | return Math.sqrt(standardDeviation/parseArray.length);
|
---|
179 |
|
---|
180 |
|
---|
181 | }
|
---|
182 | public static Boolean ClusterCheck(StartAndEndPoint parseArray[]){
|
---|
183 | System.out.println("LENGTH: " + parseArray.length);
|
---|
184 | //MAKE THREE COMPARISONS
|
---|
185 | //After clusters have been found.
|
---|
186 | //Check if their x positions intersect
|
---|
187 | //Logic being
|
---|
188 | //(L1.S < L2.E && L1.S > L2.S)
|
---|
189 | //or
|
---|
190 | //(L2.S < L1.E && L2.S > L1.S)
|
---|
191 | //Variance is using Start of line point.
|
---|
192 | //USING VARIANTS
|
---|
193 | double sum =0;
|
---|
194 | double temp =0;
|
---|
195 | double mean, variance;
|
---|
196 | int size = parseArray.length;
|
---|
197 | //Calculate sum of array
|
---|
198 | for(int i =0; i < parseArray.length; i++){
|
---|
199 | sum += parseArray[i].getP1().y;
|
---|
200 | }
|
---|
201 | //Calculate mean of array
|
---|
202 | mean = sum/parseArray.length;
|
---|
203 | //Calculate variants
|
---|
204 | for(int i =0; i < size; i++){
|
---|
205 | temp += Math.pow((parseArray[i].getP1().y-mean),2);
|
---|
206 | }
|
---|
207 | variance = Math.abs(temp/(size -1));
|
---|
208 | System.out.println("VARIANCE: " + variance);
|
---|
209 | if(variance <= CLUSTER_DISTANCE_MAX && variance > CLUSTER_DISTANCE_MIN){
|
---|
210 | for(int i = 0; i < 3; i++){
|
---|
211 | double l1_S = parseArray[i].getP1().x;
|
---|
212 | double l1_E = parseArray[i].getP2().x;
|
---|
213 | double l2_S =
|
---|
214 |
|
---|
215 | }
|
---|
216 | }
|
---|
217 | return false;
|
---|
218 | }
|
---|
219 |
|
---|
220 | //GLOBAL_CONSTANTS
|
---|
221 | static double CLUSTER_DISTANCE_MAX = 15;
|
---|
222 | static double CLUSTER_DISTANCE_MIN = 2;
|
---|
223 | static int CLASSIFIER_HOUGHLINESP_MIN = 10;
|
---|
224 | static int CLASSIFIER_HOUGHLINESP_MAX = 65;
|
---|
225 | static int HOUGHLINEP_THRESHOLD = 10;
|
---|
226 | static int STANDARD_DEVIATION_THRESHOLD = 6;
|
---|
227 | static int MINLINECOUNT = 40;
|
---|
228 | static int MAXLINEGAP = 1; //4
|
---|
229 | static double SLOPEGRADIENT = 0.02;
|
---|
230 | //SHOULD TURN INTO ARGS
|
---|
231 |
|
---|
232 | //CLASSIFYING FUNCTIONS
|
---|
233 | private static BufferedImage toBufferedImage(Mat mat){
|
---|
234 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
235 | //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
236 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
237 | try{
|
---|
238 | int type = BufferedImage.TYPE_3BYTE_BGR;
|
---|
239 | int bufferSize = mat.channels() * mat.cols() * mat.rows();
|
---|
240 | byte[] b = new byte[bufferSize];
|
---|
241 | //get all the pixels
|
---|
242 | mat.get(0, 0, b);
|
---|
243 | BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
|
---|
244 | final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
|
---|
245 | System.arraycopy(b, 0, targetPixels, 0, b.length);
|
---|
246 | return image;
|
---|
247 | }
|
---|
248 | catch(Exception e){
|
---|
249 | System.err.println(e);
|
---|
250 | }
|
---|
251 | return null;
|
---|
252 | }
|
---|
253 | private static boolean ClassifierPixelCount(BufferedImage img){
|
---|
254 | try {
|
---|
255 | //Read file
|
---|
256 | //BufferedImage img = ImageIO.read(new File(processedFile));
|
---|
257 | int x = img.getWidth();
|
---|
258 | int y = img.getHeight();
|
---|
259 | int pixelCount = 0;
|
---|
260 | int redCount = 0;
|
---|
261 | float percentage = 0;
|
---|
262 |
|
---|
263 | //Go Thru every pixel
|
---|
264 | for(int i=0; i < y; i++){
|
---|
265 | for(int j=0;j < x; j++){
|
---|
266 | //Get value for current pixels RGB value
|
---|
267 | int currPixelRGB = img.getRGB(j, i);
|
---|
268 | //Check if pixel is red (hex value of red)
|
---|
269 | if(currPixelRGB == 0xFFFF0000){
|
---|
270 | redCount++;
|
---|
271 | }
|
---|
272 | pixelCount++;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | //Calculate percentage of Red in image
|
---|
276 | percentage = ((float)redCount/(float)pixelCount)*(float)100;
|
---|
277 | //If more than %10 and less than %50 then its sheet music!
|
---|
278 | if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){ //MAKE THESE CONSTANTS!!
|
---|
279 | return true;}
|
---|
280 | }
|
---|
281 | catch (Exception e) {
|
---|
282 | System.err.println(e);
|
---|
283 | }
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 | private static boolean ClassifierLineCount(int lineCount){
|
---|
287 |
|
---|
288 | if(lineCount>MINLINECOUNT){
|
---|
289 | return true;
|
---|
290 | }
|
---|
291 | else{
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | private static ArrayList ClassifierLineClusterOLD(BufferedImage img){
|
---|
296 | ArrayList returnArray = new ArrayList();
|
---|
297 | try {
|
---|
298 |
|
---|
299 | //IF THIS WORKS THEN IMPLEMENT A VERSION THAT USES POINTS from the draw line code.
|
---|
300 | //ALSO CHECK OUT K NEAREST NEIGHBOR?
|
---|
301 | //0xFFFF0000 = RED
|
---|
302 |
|
---|
303 | //go thru every pixel until find red pixel
|
---|
304 | //get y pos of red pixel
|
---|
305 | //continue with loop until find another red pixel
|
---|
306 | //get y pos of red pixel
|
---|
307 | //compare y pos (if close together then continue loop) else break
|
---|
308 |
|
---|
309 | int x = img.getWidth();
|
---|
310 | int y = img.getHeight();
|
---|
311 | int closeLineCount = 0;
|
---|
312 | ArrayList<Integer> redPixelYpos = new ArrayList<Integer>();
|
---|
313 |
|
---|
314 | //Go Thru every pixel
|
---|
315 | for(int i=0; i < y; i++){
|
---|
316 | for(int j=0;j < x; j++){
|
---|
317 | //Get value for current pixels RGB value
|
---|
318 | int currPixelRGB = img.getRGB(j, i);
|
---|
319 | //Check if pixel is red (hex value of red)
|
---|
320 | if(currPixelRGB == 0xFFFF0000) {
|
---|
321 |
|
---|
322 | //Store y pos of red pixel if there is no duplicate
|
---|
323 | if(!redPixelYpos.contains(i)){
|
---|
324 | redPixelYpos.add(i);
|
---|
325 | //System.out.println(i );
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | //Check if any of the lines found are close together and that there has been more than one line found
|
---|
331 | if(redPixelYpos.size()>1){
|
---|
332 | //go through list and compare every value
|
---|
333 | for(int i =0; i< redPixelYpos.size(); i++){
|
---|
334 | //System.out.println("i: " +redPixelYpos.get(i));
|
---|
335 | for(int j=0; j< redPixelYpos.size(); j++){
|
---|
336 | //System.out.println("j: "+redPixelYpos.get(j));
|
---|
337 | //Check if difference is less than 4 and the values are not duplicates.
|
---|
338 | if(Math.abs(redPixelYpos.get(i) - redPixelYpos.get(j)) < 4 && !redPixelYpos.get(j).equals(redPixelYpos.get(i))){
|
---|
339 | closeLineCount++;
|
---|
340 | }
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 | int clusterCount = closeLineCount/4;
|
---|
345 |
|
---|
346 | if(closeLineCount >= 4){
|
---|
347 | returnArray.add(true);
|
---|
348 | returnArray.add(closeLineCount);
|
---|
349 | returnArray.add(clusterCount);
|
---|
350 | }
|
---|
351 | else{
|
---|
352 | returnArray.add(false);
|
---|
353 | returnArray.add(closeLineCount);
|
---|
354 | returnArray.add(clusterCount);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | catch (Exception e) {
|
---|
358 | System.err.println(e);
|
---|
359 | }
|
---|
360 | return returnArray;
|
---|
361 | }
|
---|
362 | private static ArrayList ClassifierLineCluster(ArrayList<StartAndEndPoint> linePointsArray, Mat clustersFoundRGB){
|
---|
363 |
|
---|
364 | /*
|
---|
365 | ADDITION:
|
---|
366 | After clusters have been found.
|
---|
367 | Check if x positions intersect at all
|
---|
368 | StartXPos of p1
|
---|
369 |
|
---|
370 | This will check for a cluster of lines that are close together.
|
---|
371 | 1. Go through the list of Y positions(start point) in parsed array.
|
---|
372 | If, there is a small distance between them,
|
---|
373 | then, add to closeLineArray.
|
---|
374 |
|
---|
375 | Have all Y positions that are close to each other now.
|
---|
376 | Need to find the lines that are clustered together.
|
---|
377 |
|
---|
378 | Now check if there are four of these are close to each other.
|
---|
379 | 2. Go through list of closeLine.
|
---|
380 | Get first four lines, traversing down a step each iteration {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5}
|
---|
381 | If, those 4 lines are close together,
|
---|
382 | Then, add them to a new array that holds Line Cluster Values.
|
---|
383 | Go to line 4 positions down since, as do not want duplicates.
|
---|
384 |
|
---|
385 | 3.
|
---|
386 | */
|
---|
387 |
|
---|
388 | ArrayList returnArray = new ArrayList();
|
---|
389 | ArrayList<Double> closeLineYPos = new ArrayList();
|
---|
390 | ArrayList<double[]> clusterArray = new ArrayList();
|
---|
391 | int clusterCount = 0;
|
---|
392 | try {
|
---|
393 | if(linePointsArray.size()> 1) {
|
---|
394 |
|
---|
395 | /*
|
---|
396 | //Display input array TESTING PURPOSES
|
---|
397 | for (int i = 0; i < linePointsArray.size(); i++) {
|
---|
398 | System.out.println(linePointsArray.get(i).toString());
|
---|
399 | }
|
---|
400 | */
|
---|
401 |
|
---|
402 | //1. Check if y points are close together
|
---|
403 | //go thru list and compare values against each other
|
---|
404 | for (int i = 0; i < linePointsArray.size(); i++){
|
---|
405 | //System.out.println("i: "+ linePointsArray.get(i).getP1().y);
|
---|
406 | for (int j = 0; j < linePointsArray.size(); j++) {
|
---|
407 | //System.out.println("j: "+ linePointsArray.get(j).getP1().y);
|
---|
408 | //Check if difference is less than 4 and the values are not duplicates.
|
---|
409 | if(Math.abs(linePointsArray.get(j).getP1().y - linePointsArray.get(i).getP1().y) < 5){
|
---|
410 | if(linePointsArray.get(j).getP1().y != linePointsArray.get(i).getP1().y){
|
---|
411 | closeLineYPos.add(linePointsArray.get(j).getP1().y);
|
---|
412 |
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 | /*for (double num : closeLineYPos){
|
---|
418 | System.out.println(num);
|
---|
419 | } */
|
---|
420 |
|
---|
421 | //2. Now check if there are four of these are close to each other.
|
---|
422 | //Go through all of the items in this list and check if four of them are close together
|
---|
423 | //Check first four items, traverse down a step {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5}
|
---|
424 | //If 4 items are close together,
|
---|
425 | //Then add them to a new array that holds Line Cluster Values.
|
---|
426 | //Go down 4 positions down since, as do not want duplicates.
|
---|
427 |
|
---|
428 | //Now have an array of at least four lines that are close together.
|
---|
429 | //Sort array and remove duplicates
|
---|
430 | Collections.sort(closeLineYPos);
|
---|
431 |
|
---|
432 |
|
---|
433 |
|
---|
434 | closeLineYPos = removeDuplicates(closeLineYPos);
|
---|
435 |
|
---|
436 | //DISPLAYING AS EXCEPTED! WOO!
|
---|
437 |
|
---|
438 | for (double y : closeLineYPos){
|
---|
439 | System.out.println("CloseLineYPos: " + y);
|
---|
440 | }
|
---|
441 | if(closeLineYPos.size() >= 4) {
|
---|
442 | //FOR every item in array of CloseLines
|
---|
443 | for(int i= 0; i< closeLineYPos.size(); i++){
|
---|
444 | //If last comparator is at end of array.
|
---|
445 | if(i + 4 >= closeLineYPos.size()){
|
---|
446 | break;
|
---|
447 | }
|
---|
448 | else{
|
---|
449 |
|
---|
450 | //Add 4 values of CloseLine Array to a tempArray
|
---|
451 | double[] tempArray = new double[4];
|
---|
452 | tempArray[0] = closeLineYPos.get(i);
|
---|
453 | tempArray[1] = closeLineYPos.get(i + 1);
|
---|
454 | tempArray[2] = closeLineYPos.get(i + 2);
|
---|
455 | tempArray[3] = closeLineYPos.get(i + 3);
|
---|
456 |
|
---|
457 | //Check standard deviation between these 4 values.
|
---|
458 | //If it SD is less than 5 then it is considered to be a cluster of lines.
|
---|
459 |
|
---|
460 |
|
---|
461 | if(StandardDeviation(tempArray) < STANDARD_DEVIATION_THRESHOLD){
|
---|
462 | //System.out.println("tempArray PT: "+tempArray[0] + " , " + tempArray[1] + " , " + tempArray[2] + " , " + tempArray[3]);
|
---|
463 | //System.out.println("tempArray SD: " + StandardDeviation(tempArray));
|
---|
464 | //Store array
|
---|
465 | clusterArray.add(tempArray);
|
---|
466 | //If I + 4 is less than the size of the array then increment by 4
|
---|
467 | //Go down +4 positions in closeLineYPos array
|
---|
468 | if((i + 4 < closeLineYPos.size())){
|
---|
469 | //System.out.println("IF, i = " + i + " -> "+ (i+4) + ", CloseLineYpos size= " + closeLineYPos.size());
|
---|
470 | i = i+4;
|
---|
471 | }
|
---|
472 | else{
|
---|
473 | //break
|
---|
474 | //System.out.println("ELSE, i = " + i+ " closeLineYpos size= " + closeLineYPos.size());
|
---|
475 | Thread.sleep(2000);
|
---|
476 | break;
|
---|
477 | }
|
---|
478 | }
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 | /*
|
---|
483 | System.out.println("Cluster Coordinates: ");
|
---|
484 | for(double[] items : clusterArray){
|
---|
485 | for(int i = 0; i <items.length; i++){
|
---|
486 | System.out.println("ITEMS: "+ items[i]);
|
---|
487 | }
|
---|
488 | }
|
---|
489 | */
|
---|
490 | //Setup Drawing clusters found.
|
---|
491 | //For every pt given the input array
|
---|
492 | for(StartAndEndPoint pt : linePointsArray){
|
---|
493 | //Go through every the Arrays in the clusterArray(clustered lines)
|
---|
494 | for(int i =0; i < clusterArray.size(); i++){
|
---|
495 | //Go through every item in the array
|
---|
496 | for(double item : clusterArray.get(i)) {
|
---|
497 | //Check if the curr item is equal to current pt
|
---|
498 | if (item == pt.getP1().y){
|
---|
499 | //calculate a different colour for each line
|
---|
500 |
|
---|
501 | //Draw a line
|
---|
502 | Imgproc.line(clustersFoundRGB, pt.getP1(), pt.getP2(), new Scalar(0, 255, 0), 1, Imgproc.LINE_4, 0);
|
---|
503 | }
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | }
|
---|
508 |
|
---|
509 | clusterCount = clusterArray.size();
|
---|
510 | //SETUP RETURN ARRAY
|
---|
511 | if(clusterCount >= 1){
|
---|
512 | returnArray.add(true);
|
---|
513 | returnArray.add(closeLineYPos.size());
|
---|
514 | returnArray.add(clusterCount);
|
---|
515 | returnArray.add(clustersFoundRGB);
|
---|
516 | }
|
---|
517 | else{
|
---|
518 | returnArray.add(false);
|
---|
519 | returnArray.add(closeLineYPos.size());
|
---|
520 | returnArray.add(clusterCount);
|
---|
521 | }
|
---|
522 | }
|
---|
523 | }
|
---|
524 | catch (Exception e) {
|
---|
525 | System.err.println(e);
|
---|
526 | }
|
---|
527 | return returnArray;
|
---|
528 | }
|
---|
529 |
|
---|
530 | private static ArrayList ClassifierLineClusterPt(ArrayList<StartAndEndPoint> linePointsArray, Mat clustersFoundRGB){
|
---|
531 | /*
|
---|
532 | ADDITION:
|
---|
533 | This will check for a cluster of lines that are close together.
|
---|
534 | 1. Go through the list of Y positions(start point) in parsed array.
|
---|
535 | If, there is a small distance between them,
|
---|
536 | then, add to closeLineArray.
|
---|
537 |
|
---|
538 | Have all Y positions that are close to each other now.
|
---|
539 | Need to find the lines that are clustered together.
|
---|
540 |
|
---|
541 | Now check if there are four of these are close to each other.
|
---|
542 | 2. Go through list of closeLine.
|
---|
543 | Get first four lines, traversing down a step each iteration {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5}
|
---|
544 | If, those 4 lines are close together,
|
---|
545 | Then, add them to a new array that holds Line Cluster Values.
|
---|
546 | Go to line 4 positions down since, as do not want duplicates.
|
---|
547 |
|
---|
548 | 3.
|
---|
549 | */
|
---|
550 | ArrayList returnArray = new ArrayList();
|
---|
551 | ArrayList<StartAndEndPoint> closeLinePts = new ArrayList();
|
---|
552 | ArrayList<StartAndEndPoint[]> clusterPtArray = new ArrayList();
|
---|
553 | int clusterCount = 0;
|
---|
554 | try {
|
---|
555 | if(linePointsArray.size()> 1) {
|
---|
556 | /*
|
---|
557 | //Display input array TESTING PURPOSES
|
---|
558 | for (int i = 0; i < linePointsArray.size(); i++) {
|
---|
559 | System.out.println(linePointsArray.get(i).toString());
|
---|
560 | }
|
---|
561 | */
|
---|
562 | //1. Check if y points are close together
|
---|
563 | //go thru list and compare values against each other
|
---|
564 | for (int i = 0; i < linePointsArray.size(); i++){
|
---|
565 | //System.out.println("i: "+ linePointsArray.get(i).getP1().y);
|
---|
566 | for (int j = 0; j < linePointsArray.size(); j++) {
|
---|
567 | //System.out.println("j: "+ linePointsArray.get(j).getP1().y);
|
---|
568 | //Check if difference is less than 4 and the values are not duplicates.
|
---|
569 | if(Math.abs(linePointsArray.get(j).getP1().y - linePointsArray.get(i).getP1().y) < 5){
|
---|
570 | if(linePointsArray.get(j).getP1().y != linePointsArray.get(i).getP1().y){
|
---|
571 | closeLinePts.add(linePointsArray.get(i));
|
---|
572 | }
|
---|
573 | }
|
---|
574 | }
|
---|
575 | }
|
---|
576 | /*for (double num : closeLineYPos){
|
---|
577 | System.out.println(num);
|
---|
578 | } */
|
---|
579 |
|
---|
580 | //2. Now check if there are four of these are close to each other.
|
---|
581 | //Go through all of the items in this list and check if four of them are close together
|
---|
582 | //Check first four items, traverse down a step {0,1,2,3} -> {1,2,3,4} -> {2,3,4,5}
|
---|
583 | //If 4 items are close together,
|
---|
584 | //Then add them to a new array that holds Line Cluster Values.
|
---|
585 | //Go down 4 positions down since, as do not want duplicates.
|
---|
586 |
|
---|
587 | //Now have an array of at least four lines that are close together.
|
---|
588 | //Sort array and remove duplicates
|
---|
589 | Collections.sort(closeLinePts, new Comparator<StartAndEndPoint>() {
|
---|
590 | @Override
|
---|
591 | public int compare(StartAndEndPoint p1, StartAndEndPoint p2) {
|
---|
592 | return (int)(p1.getP1().y - p2.getP1().y);
|
---|
593 | }
|
---|
594 | });
|
---|
595 | closeLinePts = removeDuplicates(closeLinePts);
|
---|
596 | /*DISPLAYING AS EXCEPTED! WOO!
|
---|
597 | for (StartAndEndPoint pt : closeLinePts) {
|
---|
598 | System.out.println("CloseLinePTs: " + pt.getP1().y);
|
---|
599 | }
|
---|
600 | */
|
---|
601 |
|
---|
602 | if(closeLinePts.size() >= 4) {
|
---|
603 | //FOR every item in array of CloseLines
|
---|
604 | for(int i= 0; i< closeLinePts.size(); i++){
|
---|
605 | //If last comparator is at end of array.
|
---|
606 | if(i + 4 >= closeLinePts.size()){
|
---|
607 | break;
|
---|
608 | }
|
---|
609 | else{
|
---|
610 | //Add 4 values of CloseLinePt Array to a tempArray
|
---|
611 | StartAndEndPoint[] tempPtArray = new StartAndEndPoint[4];
|
---|
612 | tempPtArray[0] = closeLinePts.get(i);
|
---|
613 | tempPtArray[1] = closeLinePts.get(i + 1);
|
---|
614 | tempPtArray[2] = closeLinePts.get(i + 2);
|
---|
615 | tempPtArray[3] = closeLinePts.get(i + 3);
|
---|
616 |
|
---|
617 | //Check standard deviation between these 4 values.
|
---|
618 | //If it SD is less than 5 then it is considered to be a cluster of lines.
|
---|
619 | if(ClusterCheck(tempPtArray)){
|
---|
620 | //System.out.println("tempArray PT: "+tempArray[0] + " , " + tempArray[1] + " , " + tempArray[2] + " , " + tempArray[3]);
|
---|
621 | //System.out.println("tempArray SD: " + StandardDeviation(tempArray));
|
---|
622 | //Store array
|
---|
623 | clusterPtArray.add(tempPtArray);
|
---|
624 | //If I + 4 is less than the size of the array then increment by 4
|
---|
625 | //Go down +4 positions in closeLineYPos array
|
---|
626 | if((i + 4 < closeLinePts.size())){
|
---|
627 | //System.out.println("IF, i = " + i + " -> "+ (i+4) + ", CloseLineYpos size= " + closeLineYPos.size());
|
---|
628 | i = i+4;
|
---|
629 | }
|
---|
630 | else{
|
---|
631 | //break
|
---|
632 | //System.out.println("ELSE, i = " + i+ " closeLineYpos size= " + closeLineYPos.size());
|
---|
633 | Thread.sleep(2000);
|
---|
634 | break;
|
---|
635 | }
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 | }
|
---|
640 | /*
|
---|
641 | System.out.println("Cluster Coordinates: ");
|
---|
642 | for(double[] items : clusterArray){
|
---|
643 | for(int i = 0; i <items.length; i++){
|
---|
644 | System.out.println("ITEMS: "+ items[i]);
|
---|
645 | }
|
---|
646 | }
|
---|
647 | */
|
---|
648 | //Setup Drawing clusters found.
|
---|
649 | //For every pt given the input array
|
---|
650 | for(StartAndEndPoint pt : linePointsArray){
|
---|
651 | //Go through every the Arrays in the clusterArray(clustered lines)
|
---|
652 | for(int i =0; i < clusterPtArray.size(); i++){
|
---|
653 | //Go through every item in the array
|
---|
654 | for(StartAndEndPoint item : clusterPtArray.get(i)) {
|
---|
655 | //Check if the curr item is equal to current pt
|
---|
656 | if (item.getP1().y == pt.getP1().y){
|
---|
657 | //calculate a different colour for each line
|
---|
658 | //Draw a line
|
---|
659 | Imgproc.line(clustersFoundRGB, pt.getP1(), pt.getP2(), new Scalar(0, 255, 0), 1, Imgproc.LINE_4, 0);
|
---|
660 | }
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | }
|
---|
665 |
|
---|
666 | clusterCount = clusterPtArray.size();
|
---|
667 | //SETUP RETURN ARRAY
|
---|
668 | if(clusterCount >= 1){
|
---|
669 | returnArray.add(true);
|
---|
670 | returnArray.add(closeLinePts.size());
|
---|
671 | returnArray.add(clusterCount);
|
---|
672 | returnArray.add(clustersFoundRGB);
|
---|
673 | }
|
---|
674 | else{
|
---|
675 | returnArray.add(false);
|
---|
676 | returnArray.add(closeLinePts.size());
|
---|
677 | returnArray.add(clusterCount);
|
---|
678 | }
|
---|
679 | }
|
---|
680 | }
|
---|
681 | catch (Exception e) {
|
---|
682 | System.err.println(e);
|
---|
683 | }
|
---|
684 | return returnArray;
|
---|
685 | }
|
---|
686 |
|
---|
687 |
|
---|
688 |
|
---|
689 | public static void main(String[] args) {
|
---|
690 |
|
---|
691 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
692 |
|
---|
693 | try {
|
---|
694 | ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();
|
---|
695 |
|
---|
696 | //Variables
|
---|
697 | Mat edgesDetected = new Mat();
|
---|
698 | Mat edgesDetectedRGB = new Mat();
|
---|
699 | Mat clustersFoundRGB = new Mat();
|
---|
700 | String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
|
---|
701 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
702 | //mdp.39015097852365-2.png 176 lines Contents page.
|
---|
703 | //mdp.39015097852555-3.png 76 lines
|
---|
704 | //String default_file = directory+"SheetMusic/coo.31924062612282-9.png";
|
---|
705 | //String default_file ="TestImages/NotNot/mdp.39015080972303-3.png";
|
---|
706 |
|
---|
707 |
|
---|
708 | //System.out.println(default_file);
|
---|
709 | String default_file = "TestImages/NotSheetMusic01.png";
|
---|
710 | //String default_file = "TestImages/NotSheetMusic02.png";
|
---|
711 | //String default_file = "TestImages/SheetMusic01.png";
|
---|
712 | //String default_file = "TestImages/SheetMusic02.png";
|
---|
713 | //String default_file = "TestImages/vLine.png";
|
---|
714 | String filename = ((args.length > 0) ? args[0] : default_file);
|
---|
715 | File file = new File(filename);
|
---|
716 | if(!file.exists()){System.err.println("Image not found: "+ filename);}
|
---|
717 |
|
---|
718 | int horizontalLineCount =0;
|
---|
719 |
|
---|
720 | // Load an image
|
---|
721 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
722 | // Edge detection
|
---|
723 | Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, 4);
|
---|
724 |
|
---|
725 | //Convert to RGB for future use
|
---|
726 | Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
|
---|
727 | clustersFoundRGB = edgesDetectedRGB.clone();
|
---|
728 |
|
---|
729 | Mat linesP = new Mat(); // will hold the results of the detection
|
---|
730 | //(edgeDetectedImage, outputOfDetection(r,Ξ), resolution of rho, resolution of theta, threshold (minimum num of intersections)
|
---|
731 |
|
---|
732 | double minLineLength = edgesDetectedRGB.size().width/8;
|
---|
733 |
|
---|
734 | Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength,MAXLINEGAP); // runs the actual detection
|
---|
735 | //System.out.println("Before Gradient Filtering num lines: " + linesP.rows());
|
---|
736 |
|
---|
737 | // Draw the lines
|
---|
738 | for (int x = 0; x < linesP.rows(); x++) {
|
---|
739 | double[] l = linesP.get(x, 0);
|
---|
740 | Point p1 = new Point(l[0], l[1]);
|
---|
741 | Point p2 = new Point(l[2], l[3]);
|
---|
742 | double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
|
---|
743 |
|
---|
744 | if(m<=SLOPEGRADIENT) {
|
---|
745 | Imgproc.line(edgesDetectedRGB, p1, p2, new Scalar(0, 0, 255), 1, Imgproc.LINE_4, 0);
|
---|
746 | horizontalLineCount++;
|
---|
747 | pointArrayList.add(new StartAndEndPoint(p1, p2));
|
---|
748 | }
|
---|
749 |
|
---|
750 | }
|
---|
751 | //Point is a co ordinate (x, y)
|
---|
752 | //Prove by finding number of points from one end to other:
|
---|
753 | //Get width of image.
|
---|
754 | File filenameTest = new File("TestImages/NotSheetMusic02.png");
|
---|
755 | BufferedImage i = ImageIO.read(filenameTest);
|
---|
756 | BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
757 |
|
---|
758 |
|
---|
759 |
|
---|
760 | //Display Results
|
---|
761 | //HighGui.imshow("Source", original);
|
---|
762 | //HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
763 | //HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB);
|
---|
764 | if(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB).get(3) != null) {
|
---|
765 | HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
|
---|
766 | }
|
---|
767 | //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
|
---|
768 |
|
---|
769 |
|
---|
770 |
|
---|
771 | //System.out.println("LINE COUNT RESULT: " + ClassifierLineCount(horizontalLineCount) + '\t' +"LineCount: " + horizontalLineCount); //COUNT OF LINES CLASSIFICATION
|
---|
772 | //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
|
---|
773 | //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineCluster(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + ClassifierLineCluster(pointArrayList,clustersFoundRGB).get(1) + '\t' + "ClustersFound: " + ClassifierLineCluster(pointArrayList,clustersFoundRGB).get(2));
|
---|
774 | System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
|
---|
775 |
|
---|
776 | // Wait and Exit
|
---|
777 | HighGui.waitKey();
|
---|
778 | System.exit(0);
|
---|
779 | }
|
---|
780 | catch(Exception e){
|
---|
781 | System.err.println(e);
|
---|
782 | }
|
---|
783 | }
|
---|
784 | }
|
---|