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 |
|
---|
62 | import org.opencv.highgui.HighGui;
|
---|
63 | import org.opencv.imgcodecs.Imgcodecs;
|
---|
64 | import org.opencv.imgproc.Imgproc;
|
---|
65 | import org.opencv.imgproc.Moments;
|
---|
66 |
|
---|
67 | import static org.opencv.core.Core.FILLED;
|
---|
68 | import static org.opencv.core.CvType.CV_8UC3;
|
---|
69 | import static org.opencv.highgui.HighGui.createJFrame;
|
---|
70 | import static org.opencv.highgui.HighGui.imshow;
|
---|
71 | import static org.opencv.imgcodecs.Imgcodecs.imread;
|
---|
72 | import static org.opencv.imgcodecs.Imgcodecs.imwrite;
|
---|
73 |
|
---|
74 | import java.io.File;
|
---|
75 | import java.lang.reflect.Array;
|
---|
76 | import java.util.ArrayList;
|
---|
77 |
|
---|
78 | //REFERENCES:
|
---|
79 | //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
|
---|
80 | //https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
|
---|
81 | //https://www.wikihow.com/Calculate-Percentage-in-Java
|
---|
82 | //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
|
---|
83 | //https://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
|
---|
84 | //https://www.programiz.com/java-programming/examples/standard-deviation
|
---|
85 | //https://www.geeksforgeeks.org/how-to-remove-duplicates-from-arraylist-in-java/
|
---|
86 | //https://stackoverflow.com/questions/7988486/how-do-you-calculate-the-variance-median-and-standard-deviation-in-c-or-java/7988556
|
---|
87 | //https://stackoverflow.com/questions/10396970/sort-a-list-that-contains-a-custom-class
|
---|
88 | //https://stackoverflow.com/questions/37946482/crop-images-area-with-opencv-java
|
---|
89 | //https://docs.opencv.org/3.4/dd/dd7/tutorial_morph_lines_detection.html
|
---|
90 | //https://docs.opencv.org/3.4/d0/d49/tutorial_moments.html
|
---|
91 | //https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/moments/moments.html
|
---|
92 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
93 | //http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html
|
---|
94 | //https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
|
---|
95 | //https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values
|
---|
96 | //https://stackoverflow.com/questions/18345969/how-to-get-the-mass-center-of-a-contour-android-opencv
|
---|
97 | //https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
|
---|
98 |
|
---|
99 |
|
---|
100 | //GOAL for 21st
|
---|
101 |
|
---|
102 |
|
---|
103 | //Classifier 01
|
---|
104 | //Have args so can call "java image-identification-classifier01 XX XX"
|
---|
105 | //args can be parameters in algorthim such as threshold or theta?
|
---|
106 | //Run on 5000 images.
|
---|
107 | //Record success rates
|
---|
108 | //All done with makefile
|
---|
109 |
|
---|
110 |
|
---|
111 | //But first understand houghline transform
|
---|
112 | //Know what the algorithm being used is doing.
|
---|
113 | //MAke constants for this classifier
|
---|
114 | //Make java be able to run on CMD line
|
---|
115 |
|
---|
116 | public class MainMorph {
|
---|
117 | static public class Pair{
|
---|
118 | //Privates
|
---|
119 | private Boolean _b;
|
---|
120 | private Integer _i;
|
---|
121 |
|
---|
122 | //Constructor
|
---|
123 | public Pair(Boolean b, Integer i){
|
---|
124 | _b = b;
|
---|
125 | _i = i;
|
---|
126 | }
|
---|
127 | public Pair(){
|
---|
128 | _b = null;
|
---|
129 | _i = null;
|
---|
130 | }
|
---|
131 |
|
---|
132 | //Getters
|
---|
133 | public Boolean getBoolean() {return _b;}
|
---|
134 | public Integer getInteger() {return _i;}
|
---|
135 |
|
---|
136 | //Setters
|
---|
137 | public void setBoolean (Boolean b){_b = b;}
|
---|
138 | public void setInteger (Integer i){_i = i;}
|
---|
139 |
|
---|
140 | //ToString
|
---|
141 | public String toString() {return "Boolean: " + _b + " Integer: " + _i;}
|
---|
142 | }
|
---|
143 |
|
---|
144 | //CODE VERSIONS
|
---|
145 | static int CODE_VERSION = 9;
|
---|
146 | static int IMAGE_VERSION = 3;
|
---|
147 | //GLOBAL_CONSTANTS
|
---|
148 |
|
---|
149 | static double THRESHOLD_C = 4;
|
---|
150 | static double THRESHOLD_AREA_SIZE = 10000;
|
---|
151 | static double THRESHOLD_AREA_COUNT = 10;
|
---|
152 |
|
---|
153 | //
|
---|
154 |
|
---|
155 | private static void imageViewer(String winName, Mat img) {
|
---|
156 | try {
|
---|
157 | //Internal display - Overview - Will Distort High Res images
|
---|
158 | if(IMAGE_VERSION == 1) {
|
---|
159 | HighGui.namedWindow(winName, HighGui.WINDOW_NORMAL);
|
---|
160 | HighGui.resizeWindow(winName, 1000, 1000);
|
---|
161 | imshow(winName, img);
|
---|
162 |
|
---|
163 | HighGui.moveWindow(winName, 500, 0);
|
---|
164 | HighGui.waitKey(0);
|
---|
165 |
|
---|
166 | HighGui.destroyWindow(winName);
|
---|
167 | }
|
---|
168 | //Internal display - Segmented - Will _NOT_ Distort High Res images
|
---|
169 | if(IMAGE_VERSION == 2) {
|
---|
170 | HighGui.namedWindow(winName, HighGui.WINDOW_AUTOSIZE);
|
---|
171 | HighGui.resizeWindow(winName, 1000, 1000);
|
---|
172 | imshow(winName, img);
|
---|
173 | HighGui.moveWindow(winName, 500, 0);
|
---|
174 | HighGui.waitKey(0);
|
---|
175 |
|
---|
176 | HighGui.destroyWindow(winName);
|
---|
177 | }
|
---|
178 | //External display - Save Images for manual viewing
|
---|
179 | if(IMAGE_VERSION == 3) {
|
---|
180 | //save file (testing purposes)
|
---|
181 | imwrite(winName+".jpg", img);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | catch (Exception e){
|
---|
185 | e.printStackTrace();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | //MAIN
|
---|
190 | public static void main(String[] args) {
|
---|
191 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
192 | try {
|
---|
193 | //Variables
|
---|
194 | System.out.println("Running Code version: " + CODE_VERSION + " Image Version: " +IMAGE_VERSION);
|
---|
195 | Mat mid = new Mat();
|
---|
196 | Mat edgesDetectedRGB = new Mat();
|
---|
197 | Mat clustersFoundRGB = new Mat();
|
---|
198 |
|
---|
199 | String testDirectory = "/Scratch/cpb16/is-sheet-music-encore/image-identification-dev-02/image-identification-development/";
|
---|
200 | String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
|
---|
201 | String hiresDirectory = "/Scratch/cpb16/is-sheet-music-encore/hires-download-images/";
|
---|
202 |
|
---|
203 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
204 | //mdp.39015097852365-2.png 176 lines Contents page.
|
---|
205 | //mdp.39015097852555-3.png 76 lines
|
---|
206 | //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOTNOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
---|
207 | //coo.31924062612282-9.png 8 lines
|
---|
208 | //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY? DUE TO IMAGE LARGE, ZOOM IN
|
---|
209 | //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
|
---|
210 | //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
|
---|
211 | //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
|
---|
212 | //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png";
|
---|
213 | //String default_file =hiresDirectory+"MU/SheetMusic/bc.ark+=13960=t2j72dt1p-10.png";
|
---|
214 | //String default_file =hiresDirectory+"MU/SheetMusic/bc.ark+=13960=t2j72dt1p-7.png";
|
---|
215 | //String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
|
---|
216 | //String default_file =hiresDirectory+"MU/NotSheetMusic/mdp.39015096363935-1.png";
|
---|
217 | //String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
|
---|
218 | //String default_file = "/Scratch/cpb16/is-sheet-music-encore/image-identification-terminal/TestImages/hi-res-test-coo.31924062612282-9.png";
|
---|
219 |
|
---|
220 | //String default_file = hiresDirectory+"/BK/NotSheetMusic/aeu.ark+=13960=t2s47k537-4.png"; //centre example
|
---|
221 | //String default_file = hiresDirectory+"/BK/NotSheetMusic/aeu.ark+=13960=t3tt4xf2t-2.png"; //cross
|
---|
222 | //TestNew images used
|
---|
223 | //String default_file = hiresDirectory+"/MU/SheetMusic/aeu.ark+=13960=t93787r1w-10.png"; //Bleed
|
---|
224 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-10.png"; //Handwritten
|
---|
225 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-7.png"; //Handwritten 3072 4176
|
---|
226 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-8.png"; //Handwritten
|
---|
227 | //String default_file = hiresDirectory+"/MU/SheetMusic/bc.ark+=13960=t2j72dt1p-9.png"; //Handwritten
|
---|
228 | //String default_file = hiresDirectory+"/MU/SheetMusic/coo.31924062612282-9.png"; //Snippet
|
---|
229 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-10.png"; //Generated
|
---|
230 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-7.png";
|
---|
231 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-8.png";
|
---|
232 | //String default_file = hiresDirectory+"/MU/SheetMusic/dul1.ark+=13960=t2x41569k-9.png";
|
---|
233 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-10.png"; //contentpage
|
---|
234 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; //Image evaluation
|
---|
235 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-2.png"; //large numbers
|
---|
236 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-3.png";
|
---|
237 | String default_file = hiresDirectory+"/MU/SheetMusic/mdp.39015080921409-9.png";
|
---|
238 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-4.png";
|
---|
239 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-5.png";
|
---|
240 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-6.png";
|
---|
241 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-7.png";
|
---|
242 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-8.png";
|
---|
243 | //String default_file = hiresDirectory+"/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-9.png";
|
---|
244 |
|
---|
245 |
|
---|
246 |
|
---|
247 | //String default_file = testDirectory+"TestImages/MorphTester.png";
|
---|
248 | //String default_file = testDirectory+"TestImages/NotSheetMusic01.png";
|
---|
249 | //String default_file = testDirectory+"TestImages/NotSheetMusic02.png";
|
---|
250 | //String default_file = testDirectory+"TestImages/SheetMusic01.png";
|
---|
251 | //String default_file = testDirectory+"TestImages/SheetMusic02.png";
|
---|
252 | //String default_file = testDirectory+"TestImages/vLine.png";
|
---|
253 | String filename = ((args.length > 0) ? args[0] : default_file);
|
---|
254 | File file = new File(filename);
|
---|
255 | if(!file.exists()){System.err.println("Image not found: "+ filename);}
|
---|
256 |
|
---|
257 | int horizontalLineCount =0;
|
---|
258 |
|
---|
259 | // Load an image
|
---|
260 | Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
261 | System.out.println("Width: " + original.width() + " Height: " + original.height());
|
---|
262 | Mat binarizedOriginal = original.clone();
|
---|
263 |
|
---|
264 | Imgproc.adaptiveThreshold(original, binarizedOriginal,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
265 | //TEST PARAMETERSImgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 531,1);
|
---|
266 | //Imgproc.threshold(original,original, 127, 255, Imgproc.THRESH_BINARY);
|
---|
267 |
|
---|
268 |
|
---|
269 | //****************MORPHOLOGY****************************************************************************************
|
---|
270 | //ADDIOTIONAL FILTERING TO STOP STREAKS
|
---|
271 | //LOOK INTO STREAKS MORPHOGOLY.
|
---|
272 | //****************MORPHOLOGY****************************************************************************************
|
---|
273 |
|
---|
274 | // Create the images that will use to extract the horizontal and vertical lines
|
---|
275 |
|
---|
276 | //dynamic morphology??
|
---|
277 | if(CODE_VERSION == 1) {
|
---|
278 | int hori = binarizedOriginal.width();
|
---|
279 | int vert = binarizedOriginal.height();
|
---|
280 | //Find ratio between 100 and width and 100 and height
|
---|
281 | int divX = hori/10;
|
---|
282 | int divY = vert/10;
|
---|
283 | int sizeX = (hori/divX) * 10;
|
---|
284 | int sizeY = (vert/divY) * 10;
|
---|
285 |
|
---|
286 | Mat test = binarizedOriginal.clone();
|
---|
287 | imageViewer("Original", test);
|
---|
288 |
|
---|
289 | System.out.println("hori: " + hori + '\t' + "vert: " + vert);
|
---|
290 | System.out.println("sizeX: " + sizeX + '\t' + "sizeY: " + sizeY);
|
---|
291 |
|
---|
292 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
|
---|
293 | Imgproc.erode(test,test,kernelErode);
|
---|
294 | imageViewer("01 Erode", test);
|
---|
295 |
|
---|
296 | Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
|
---|
297 | Imgproc.dilate(test, test, kernelDialate);
|
---|
298 | imageViewer("02 Dialate", test);
|
---|
299 |
|
---|
300 | Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
|
---|
301 | Imgproc.erode(test,test,kernelErodeAgain);
|
---|
302 | imageViewer(" 03 Erode Again", test);
|
---|
303 |
|
---|
304 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
|
---|
305 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
|
---|
306 | imageViewer("04 Close", test);
|
---|
307 |
|
---|
308 | Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
309 | imageViewer("05 Binarized", test);
|
---|
310 |
|
---|
311 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
|
---|
312 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
313 | imageViewer(" 06 Open", test);
|
---|
314 |
|
---|
315 | Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
|
---|
316 | Imgproc.dilate(test, test, kernelDialateAgain);
|
---|
317 | imageViewer("07 Dialate", test);
|
---|
318 |
|
---|
319 |
|
---|
320 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
|
---|
321 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
322 | imageViewer(" 08 Close Again (Final)", test);
|
---|
323 | }
|
---|
324 | //Successful hardcode for morhpology
|
---|
325 | if (CODE_VERSION == 2) {
|
---|
326 |
|
---|
327 | //MAKE SURE BLACK & WHITE
|
---|
328 | Mat test = binarizedOriginal.clone();
|
---|
329 | imageViewer("00 Binarized Original", test);
|
---|
330 |
|
---|
331 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
|
---|
332 | Imgproc.erode(test,test,kernelErode);
|
---|
333 | imageViewer("01 Erode", test);
|
---|
334 |
|
---|
335 | Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
|
---|
336 | Imgproc.dilate(test, test, kernelDialate);
|
---|
337 | imageViewer("02 Dialate", test);
|
---|
338 |
|
---|
339 | Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
|
---|
340 | Imgproc.erode(test,test,kernelErodeAgain);
|
---|
341 | imageViewer(" 03 Erode Again", test);
|
---|
342 |
|
---|
343 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
|
---|
344 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
|
---|
345 | imageViewer("04 Close", test);
|
---|
346 |
|
---|
347 | Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
|
---|
348 | imageViewer("05 Binarized", test);
|
---|
349 |
|
---|
350 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
351 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
352 | imageViewer(" 06 Open", test);
|
---|
353 |
|
---|
354 | Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
|
---|
355 | Imgproc.dilate(test, test, kernelDialateAgain);
|
---|
356 | imageViewer("07 Dialate", test);
|
---|
357 |
|
---|
358 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
|
---|
359 | Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
360 | imageViewer(" 08 Close Again (Final)", test);
|
---|
361 |
|
---|
362 | //FIGURE OUT FLOOD FILL!!
|
---|
363 | //Imgproc.floodFill(test,test, new Point(1,1), new Scalar(2));
|
---|
364 |
|
---|
365 | }
|
---|
366 | //Tutorial/Demo Code
|
---|
367 | if (CODE_VERSION == 3) {
|
---|
368 | Mat horizontal = binarizedOriginal.clone();
|
---|
369 | Mat vertical = binarizedOriginal.clone();
|
---|
370 | // Specify size on horizontal axis
|
---|
371 | int horizontal_size = horizontal.cols() / 50;
|
---|
372 | // Create structure element for extracting horizontal lines through morphology operations
|
---|
373 | Mat horizontalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontal_size, 2));
|
---|
374 | // Apply morphology operations
|
---|
375 | Imgproc.erode(horizontal, horizontal, horizontalStructure);
|
---|
376 | Imgproc.dilate(horizontal, horizontal, horizontalStructure);
|
---|
377 | // Show extracted horizontal lines
|
---|
378 | imageViewer("horizontal", horizontal);
|
---|
379 | // Specify size on vertical axis
|
---|
380 | int vertical_size = vertical.rows() / 30;
|
---|
381 | // Create structure element for extracting vertical lines through morphology operations
|
---|
382 | Mat verticalStructure = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, vertical_size));
|
---|
383 | // Apply morphology operations
|
---|
384 | Imgproc.erode(vertical, vertical, verticalStructure);
|
---|
385 | Imgproc.dilate(vertical, vertical, verticalStructure);
|
---|
386 | // Show extracted vertical lines
|
---|
387 | imageViewer("vertical", vertical);
|
---|
388 | // Inverse vertical image
|
---|
389 | Core.bitwise_not(vertical, vertical);
|
---|
390 | imageViewer("vertical_bit", vertical);
|
---|
391 | // Extract edges and smooth image according to the logic
|
---|
392 | // 1. extract edges
|
---|
393 | // 2. dilate(edges)
|
---|
394 | // 3. src.copyTo(smooth)
|
---|
395 | // 4. blur smooth img
|
---|
396 | // 5. smooth.copyTo(src, edges)
|
---|
397 | // Step 1
|
---|
398 | Mat edges = new Mat();
|
---|
399 | Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
|
---|
400 | imageViewer("edges", edges);
|
---|
401 | // Step 2
|
---|
402 | Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
|
---|
403 | Imgproc.dilate(edges, edges, kernel);
|
---|
404 | imageViewer("dilate", edges);
|
---|
405 | // Step 3
|
---|
406 | Mat smooth = new Mat();
|
---|
407 | vertical.copyTo(smooth);
|
---|
408 | // Step 4
|
---|
409 | Imgproc.blur(smooth, smooth, new Size(2, 2));
|
---|
410 | // Step 5
|
---|
411 | smooth.copyTo(vertical, edges);
|
---|
412 | // Show final result
|
---|
413 | imageViewer("smooth - final", vertical);
|
---|
414 | System.exit(0);
|
---|
415 | }
|
---|
416 | //Better morphology attempt - static
|
---|
417 | if(CODE_VERSION == 4) {
|
---|
418 |
|
---|
419 | //Display Original
|
---|
420 | imageViewer("original", original);
|
---|
421 |
|
---|
422 | Mat test = binarizedOriginal.clone();
|
---|
423 | Mat pre = binarizedOriginal.clone();
|
---|
424 | Mat dst = new Mat();
|
---|
425 |
|
---|
426 | imageViewer("00 Inverse Binarized Original", test);
|
---|
427 |
|
---|
428 | //remove large items of no interest pre proc
|
---|
429 | //denoize
|
---|
430 | //heal
|
---|
431 | //fnd large images, write to a seperate mat.
|
---|
432 | //draw these onto orignal image(binerized) in red
|
---|
433 | //turn all red pixels in image to black
|
---|
434 |
|
---|
435 |
|
---|
436 | //denoize
|
---|
437 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
|
---|
438 | Imgproc.morphologyEx(pre,pre, Imgproc.MORPH_OPEN, denoize);
|
---|
439 | imageViewer("Denoize - PRE", pre);
|
---|
440 |
|
---|
441 | //close up gaps
|
---|
442 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
443 | Imgproc.morphologyEx(pre,pre,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
444 | imageViewer("gap closer - PRE", pre);
|
---|
445 |
|
---|
446 | Mat kernelHighlightLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));
|
---|
447 | Imgproc.erode(pre,pre, kernelHighlightLarge);
|
---|
448 | imageViewer("Highlight Large - PRE", pre);
|
---|
449 |
|
---|
450 | //change white pixels to red
|
---|
451 | ArrayList<MatOfPoint> contoursPre = new ArrayList<MatOfPoint>();
|
---|
452 | Mat hierarchyPre = new Mat();
|
---|
453 |
|
---|
454 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
455 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
456 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
457 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
458 |
|
---|
459 | Imgproc.findContours(pre, contoursPre, hierarchyPre, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
460 |
|
---|
461 | //Draw contours and record areas
|
---|
462 |
|
---|
463 | Mat drawingPre = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
464 | Mat mask = new Mat(drawingPre.size(), CV_8UC3,new Scalar(0));
|
---|
465 | Scalar colorPre = new Scalar(255, 255, 255);
|
---|
466 | Imgproc.drawContours(mask, contoursPre, -1, new Scalar(255, 255, 255), FILLED);
|
---|
467 | Imgproc.fillPoly(drawingPre, contoursPre,colorPre);
|
---|
468 | imageViewer("DRAWINGPRE", drawingPre);
|
---|
469 |
|
---|
470 |
|
---|
471 | //FIIIIIIIIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXX
|
---|
472 | //Remove from main Mat
|
---|
473 | Core.bitwise_not(mask,mask);
|
---|
474 | imageViewer("MASK", mask);
|
---|
475 | imageViewer("TEST", test);
|
---|
476 |
|
---|
477 | drawingPre.copyTo(test, mask);
|
---|
478 | imageViewer("COMBINE", test);
|
---|
479 |
|
---|
480 | //start staff line detection
|
---|
481 |
|
---|
482 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
|
---|
483 | Imgproc.erode(test,test,kernelErode);
|
---|
484 | imageViewer("01 Erode plus pre", test);
|
---|
485 |
|
---|
486 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
|
---|
487 | Imgproc.dilate(test,test,kernelDilate);
|
---|
488 | imageViewer("02 Dilate", test);
|
---|
489 |
|
---|
490 | Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
491 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
|
---|
492 | imageViewer("03 Open", test);
|
---|
493 |
|
---|
494 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
|
---|
495 | Imgproc.erode(test,test,kernelErode02);
|
---|
496 | imageViewer("04 Erode (Final)", test);
|
---|
497 |
|
---|
498 |
|
---|
499 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
500 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
501 | Mat hierarchy = new Mat();
|
---|
502 |
|
---|
503 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
504 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
505 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
506 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
507 |
|
---|
508 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
509 |
|
---|
510 | //Draw contours and record areas
|
---|
511 | Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
512 | Mat drawing2 = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
513 | int areaCounter = 0;
|
---|
514 | for (int i = 0; i < contours.size(); i++) {
|
---|
515 | double area = Imgproc.contourArea(contours.get(i));
|
---|
516 | if(area > THRESHOLD_AREA_SIZE ) {
|
---|
517 | areaCounter++;
|
---|
518 | Scalar color = new Scalar(0, 0, 255);
|
---|
519 | Imgproc.drawContours(drawing, contours, i, color, 1);
|
---|
520 | System.out.println("AREA: " + area);
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | //Classifier Calculation
|
---|
525 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
526 | System.out.println("THIS IS SHEET MUSIC");
|
---|
527 | System.out.println(areaCounter);
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | //Show in a window
|
---|
532 | imageViewer("Contours", drawing);
|
---|
533 | }
|
---|
534 | //Better morphology attempt - dynamic
|
---|
535 | if(CODE_VERSION == 5) {
|
---|
536 | int hori = binarizedOriginal.width();
|
---|
537 | int vert = binarizedOriginal.height();
|
---|
538 | //Find ratio between 100 and width and 100 and height
|
---|
539 | int sizeX100 = 10 * (hori/68);
|
---|
540 | int sizeY100 = 10 * (vert/46);
|
---|
541 | int sizeX10 = (hori/68);
|
---|
542 | int sizeY10 = (vert/46);
|
---|
543 | int sizeX1 = (hori/68)/10;
|
---|
544 | int sizeY1 = (vert/46)/10;
|
---|
545 |
|
---|
546 | //SizeX should always be a 68th * 10. Based off the defualt tester image "coo.*"
|
---|
547 | //SizeT should always be a 46th * 10
|
---|
548 |
|
---|
549 | System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1);
|
---|
550 | //Display Original
|
---|
551 | imageViewer("original", original);
|
---|
552 |
|
---|
553 | Mat test = binarizedOriginal.clone();
|
---|
554 | imageViewer("00 Inverse Binarized Original", test);
|
---|
555 |
|
---|
556 | //Remove very large and wide black spaces (8th of the page)
|
---|
557 | //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8));
|
---|
558 | //Imgproc.erode(test,test, kernelRemoveLarge);
|
---|
559 | //imageViewer("Remove Large", test);
|
---|
560 |
|
---|
561 | //Eliminate things that are not long and thin
|
---|
562 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
|
---|
563 | Imgproc.erode(test,test,kernelErode);
|
---|
564 | imageViewer("01 Erode", test);
|
---|
565 |
|
---|
566 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
|
---|
567 | Imgproc.dilate(test,test,kernelDilate);
|
---|
568 | imageViewer("02 Dilate", test);
|
---|
569 |
|
---|
570 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
|
---|
571 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
|
---|
572 | imageViewer("03 Open", test);
|
---|
573 |
|
---|
574 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
|
---|
575 | Imgproc.erode(test,test,kernelErode02);
|
---|
576 | imageViewer("04 Erode (Final)", test);
|
---|
577 |
|
---|
578 |
|
---|
579 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
580 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
581 | Mat hierarchy = new Mat();
|
---|
582 |
|
---|
583 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
584 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
585 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
586 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
587 |
|
---|
588 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
589 |
|
---|
590 | //Draw contours and record areas
|
---|
591 | Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
592 | int areaCounter = 0;
|
---|
593 | for (int i = 0; i < contours.size(); i++) {
|
---|
594 | double area = Imgproc.contourArea(contours.get(i));
|
---|
595 | if(area > THRESHOLD_AREA_SIZE ) {
|
---|
596 | areaCounter++;
|
---|
597 | Scalar color = new Scalar(0, 0, 255);
|
---|
598 | Imgproc.drawContours(drawing, contours, i, color, 1);
|
---|
599 | System.out.println("AREA: " + area);
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | //Classifier Calculation
|
---|
604 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
605 | System.out.println("THIS IS SHEET MUSIC");
|
---|
606 | System.out.println(areaCounter);
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | //Show in a window
|
---|
611 | imageViewer("Contours", drawing);
|
---|
612 | }
|
---|
613 | //MASK UNDERSTANDING
|
---|
614 | if(CODE_VERSION == 6) {
|
---|
615 | String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
|
---|
616 | Mat mask = new Mat();
|
---|
617 | Mat dst = new Mat();
|
---|
618 | //Get source image and binerize
|
---|
619 | Mat src = Imgcodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
620 | Imgproc.adaptiveThreshold(original, src,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
621 | imageViewer("src", src);
|
---|
622 |
|
---|
623 | //Find unwanted material, then invert it so mask removes not keeps.
|
---|
624 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
|
---|
625 | Imgproc.erode(src,mask,kernelErode);
|
---|
626 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
|
---|
627 | Imgproc.dilate(mask, mask, kernelDilate);
|
---|
628 | Core.bitwise_not(mask,mask);
|
---|
629 | imageViewer("mask", mask);
|
---|
630 |
|
---|
631 | //Copy source image to new Mat, with mask in use
|
---|
632 | src.copyTo(dst, mask);
|
---|
633 | imageViewer("dst", dst);
|
---|
634 |
|
---|
635 |
|
---|
636 |
|
---|
637 |
|
---|
638 | }
|
---|
639 | //Mask implementation
|
---|
640 | if(CODE_VERSION == 7) {
|
---|
641 |
|
---|
642 | //Display Original
|
---|
643 | imageViewer("original", original);
|
---|
644 |
|
---|
645 | Mat src = binarizedOriginal.clone();
|
---|
646 | Mat test = binarizedOriginal.clone();
|
---|
647 | Mat mask = new Mat();
|
---|
648 | Mat dst = new Mat();
|
---|
649 |
|
---|
650 | imageViewer("00 Inverse Binarized Original", src);
|
---|
651 |
|
---|
652 | //denoize
|
---|
653 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
|
---|
654 | Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
|
---|
655 | imageViewer("01 Denoize - mask", mask);
|
---|
656 |
|
---|
657 | //close up gaps
|
---|
658 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
659 | Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
660 | imageViewer("02 gap closer - mask", mask);
|
---|
661 |
|
---|
662 | //Isolate large items
|
---|
663 | Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
|
---|
664 | Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
|
---|
665 | imageViewer("03 Isolate Large - mask", mask);
|
---|
666 | Core.bitwise_not(mask,mask);
|
---|
667 |
|
---|
668 | //Remove unwanted large items from image
|
---|
669 | src.copyTo(dst, mask);
|
---|
670 | imageViewer("04 Large Items Removed", dst);
|
---|
671 |
|
---|
672 | //start staff line detection
|
---|
673 |
|
---|
674 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
|
---|
675 | Imgproc.erode(dst,test,kernelErode);
|
---|
676 | imageViewer("11 Erode plus pre", test);
|
---|
677 |
|
---|
678 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
|
---|
679 | Imgproc.dilate(test,test,kernelDilate);
|
---|
680 | imageViewer("12 Dilate", test);
|
---|
681 |
|
---|
682 | Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
683 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
|
---|
684 | imageViewer("13 Open", test);
|
---|
685 |
|
---|
686 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
|
---|
687 | Imgproc.erode(test,test,kernelErode02);
|
---|
688 | imageViewer("14 Erode (Final)", test);
|
---|
689 |
|
---|
690 |
|
---|
691 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
692 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
693 | Mat hierarchy = new Mat();
|
---|
694 |
|
---|
695 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
696 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
697 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
698 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
699 |
|
---|
700 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
701 |
|
---|
702 | //Draw contours and record areas
|
---|
703 | Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
704 | int areaCounter = 0;
|
---|
705 |
|
---|
706 |
|
---|
707 | Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), FILLED);
|
---|
708 | // for (int i = 0; i < contours.size(); i++) {
|
---|
709 | // Scalar color = new Scalar(0, i, i);
|
---|
710 | // double area = Imgproc.contourArea(contours.get(i));
|
---|
711 | // Imgproc.drawContours(drawing, contours, i, color, FILLED);
|
---|
712 | // System.out.println("AREA: " + area);
|
---|
713 | //
|
---|
714 | // }
|
---|
715 | imageViewer("Contours found", drawing);
|
---|
716 |
|
---|
717 | //Classifier Calculation
|
---|
718 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
719 | System.out.println("THIS IS SHEET MUSIC");
|
---|
720 | System.out.println(areaCounter);
|
---|
721 | }
|
---|
722 |
|
---|
723 |
|
---|
724 | }
|
---|
725 | //Mask implementations (LARGE AND SMALL) - HIGH RES NUMBER MOD
|
---|
726 | if(CODE_VERSION == 8) {
|
---|
727 |
|
---|
728 | //Display Original
|
---|
729 | imageViewer("original", original);
|
---|
730 |
|
---|
731 | Mat test = binarizedOriginal.clone();
|
---|
732 |
|
---|
733 |
|
---|
734 | imageViewer("00 Inverse Binarized Original", test);
|
---|
735 |
|
---|
736 |
|
---|
737 | //************************************
|
---|
738 | //Large Object Removal
|
---|
739 | //************************************
|
---|
740 | Mat srcLOR = binarizedOriginal.clone();
|
---|
741 | Mat maskLOR = new Mat();
|
---|
742 | Mat dstLOR = new Mat();
|
---|
743 |
|
---|
744 | //denoize
|
---|
745 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
746 | Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
|
---|
747 | imageViewer("01 Denoize - mask", maskLOR);
|
---|
748 |
|
---|
749 | //close up gaps
|
---|
750 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
751 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
752 | imageViewer("02 gap closer - mask", maskLOR);
|
---|
753 |
|
---|
754 | //Isolate large items
|
---|
755 | Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
|
---|
756 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
|
---|
757 | imageViewer("03 Isolate Large - mask", maskLOR);
|
---|
758 |
|
---|
759 | //Remove large items from image
|
---|
760 | Core.bitwise_not(maskLOR,maskLOR);
|
---|
761 | srcLOR.copyTo(dstLOR, maskLOR);
|
---|
762 | imageViewer("04 Large Items Removed", dstLOR);
|
---|
763 |
|
---|
764 | //****************************************
|
---|
765 | //Small object removal (SOR)
|
---|
766 | //****************************************
|
---|
767 |
|
---|
768 | Mat srcSOR = dstLOR.clone();
|
---|
769 | Mat maskSOR = new Mat();
|
---|
770 | Mat dstSOR = new Mat();
|
---|
771 |
|
---|
772 | Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
773 | Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
|
---|
774 | imageViewer("11 show small - mask", maskSOR);
|
---|
775 |
|
---|
776 | Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
777 | Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
|
---|
778 | imageViewer("12 highlight small - mask", maskSOR);
|
---|
779 |
|
---|
780 | /* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
|
---|
781 | Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
|
---|
782 | imageViewer("13 isolate small - mask", maskSOR);
|
---|
783 | */
|
---|
784 |
|
---|
785 | //Remove small items from image
|
---|
786 | Core.bitwise_not(maskSOR, maskSOR);
|
---|
787 | srcSOR.copyTo(dstSOR, maskSOR);
|
---|
788 | imageViewer("14 Small Items Removed", dstSOR);
|
---|
789 |
|
---|
790 |
|
---|
791 | //****************************************
|
---|
792 | //start staff line detection
|
---|
793 | //****************************************
|
---|
794 |
|
---|
795 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
|
---|
796 | Imgproc.erode(dstSOR,test,kernelErode);
|
---|
797 | imageViewer("21 Erode plus pre", test);
|
---|
798 |
|
---|
799 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
|
---|
800 | Imgproc.dilate(test,test,kernelDilate);
|
---|
801 | imageViewer("22 Dilate", test);
|
---|
802 |
|
---|
803 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
804 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
|
---|
805 | imageViewer("23 Close", test);
|
---|
806 |
|
---|
807 |
|
---|
808 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
|
---|
809 | Imgproc.erode(test,test,kernelErode02);
|
---|
810 | imageViewer("24 Erode (Final)", test);
|
---|
811 |
|
---|
812 | //********************************************************************************
|
---|
813 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
814 | //********************************************************************************
|
---|
815 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
816 | ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
|
---|
817 | ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
|
---|
818 | Mat hierarchy = new Mat();
|
---|
819 |
|
---|
820 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
821 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
822 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
823 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
824 |
|
---|
825 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
826 |
|
---|
827 | System.out.println(contours.size());
|
---|
828 | //Draw contours and record areas
|
---|
829 | Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
830 | Mat largeContoursFound = allContoursFound.clone() ;
|
---|
831 | Mat postContoursFound = allContoursFound.clone();
|
---|
832 | int areaCounter = 0;
|
---|
833 |
|
---|
834 | //Have created a preprocess to remove large objects.
|
---|
835 | //Need to now finalized Classifier, re try area detection.
|
---|
836 | //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
|
---|
837 | //Just use area and periemter to determine if sheet music
|
---|
838 | //Discuss with david before weekend perhaps?
|
---|
839 |
|
---|
840 | Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
|
---|
841 | for (int i = 0; i < contours.size(); i++) {
|
---|
842 | double area = Imgproc.contourArea(contours.get(i));
|
---|
843 | if(area > 100) {
|
---|
844 | System.out.println("AREA: " + area);
|
---|
845 | Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
|
---|
846 | //create list of large coutours found
|
---|
847 | largeContours.add(contours.get(i));
|
---|
848 | }
|
---|
849 | }
|
---|
850 | imageViewer("80 All Contours found", allContoursFound);
|
---|
851 | imageViewer("81 Large Contours Found", largeContoursFound);
|
---|
852 |
|
---|
853 | //*****************************************************************
|
---|
854 | //Circles on processed images
|
---|
855 | //*****************************************************************
|
---|
856 | // //Init arrays
|
---|
857 | // Mat circleOutput = allContoursFound.clone();
|
---|
858 | // MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()];
|
---|
859 | // Rect[] boundRect = new Rect[largeContours.size()];
|
---|
860 | // Point[] centers = new Point[largeContours.size()];
|
---|
861 | // float[][] radius = new float[largeContours.size()][1];
|
---|
862 | //
|
---|
863 | // //Fill arrays
|
---|
864 | // for (int i = 0; i < largeContours.size(); i++) {
|
---|
865 | // contoursPoly[i] = new MatOfPoint2f();
|
---|
866 | // Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
|
---|
867 | // boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
|
---|
868 | // centers[i] = new Point();
|
---|
869 | // Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
|
---|
870 | //
|
---|
871 | // }
|
---|
872 | // //Draw circle for each large contour
|
---|
873 | // for (int i = 0; i < largeContours.size(); i++) {
|
---|
874 | // Imgproc.rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2);
|
---|
875 | // Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
|
---|
876 | // }
|
---|
877 | // imageViewer("82 Circles found", circleOutput);
|
---|
878 |
|
---|
879 | //********************************************************************************
|
---|
880 | //Centroids -Everything must be to scale
|
---|
881 | //********************************************************************************
|
---|
882 |
|
---|
883 | ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
|
---|
884 | Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
|
---|
885 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
886 | mu.add(i, Imgproc.moments(largeContours.get(i), false));
|
---|
887 | Moments p = mu.get(i);
|
---|
888 | int x = (int) (p.get_m10() / p.get_m00());
|
---|
889 | int y = (int) (p.get_m01() / p.get_m00());
|
---|
890 | Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
|
---|
891 | }
|
---|
892 | imageViewer("83 Centres found", centreOutput);
|
---|
893 |
|
---|
894 |
|
---|
895 | //***********************************************
|
---|
896 | //PostProcessing - Morphology Classifier
|
---|
897 | // Use dilation to "Connect the dots"
|
---|
898 | // Testing showed the centroids were clustered together
|
---|
899 | // Then use area or perimeter as a classifier filter
|
---|
900 | //REFINEREFINEREIFEN
|
---|
901 | //REFINEREFINEREIFEN
|
---|
902 | //REFINEREFINEREIFEN
|
---|
903 | //REFINEREFINEREIFEN
|
---|
904 | //REFINEREFINEREIFEN
|
---|
905 | //REFINEREFINEREIFEN
|
---|
906 | // FIX UP CLASSIFIER COMPARISON.
|
---|
907 | // MORPHOLOGIES FUNCTIONS RETURN NULL AS THEIR RETURN VARIABLES. WHY?
|
---|
908 | //REFINEREFINEREIFEN
|
---|
909 | //REFINEREFINEREIFEN
|
---|
910 | //REFINEREFINEREIFEN
|
---|
911 | //REFINEREFINEREIFEN
|
---|
912 | //REFINEREFINEREIFEN
|
---|
913 | //***********************************************
|
---|
914 |
|
---|
915 | Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
|
---|
916 | Imgproc.dilate(centreOutput,centreOutput,postDilate);
|
---|
917 | imageViewer("91 PostDilated", centreOutput);
|
---|
918 |
|
---|
919 | Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
920 | Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
|
---|
921 | imageViewer("92 PostClose", centreOutput);
|
---|
922 |
|
---|
923 | Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
|
---|
924 | Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
|
---|
925 | imageViewer("93 PostDenoize", centreOutput);
|
---|
926 |
|
---|
927 | //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50));
|
---|
928 | //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline);
|
---|
929 |
|
---|
930 |
|
---|
931 |
|
---|
932 | //Find area
|
---|
933 | Mat centreOutputGrey = new Mat();
|
---|
934 | Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
|
---|
935 | Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
936 |
|
---|
937 | for (int i = 0; i < postContours.size(); i++) {
|
---|
938 | double area = Imgproc.contourArea(postContours.get(i));
|
---|
939 | if(area > THRESHOLD_AREA_SIZE) {
|
---|
940 | System.out.println("POST AREA: " + area);
|
---|
941 | Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
|
---|
942 | areaCounter++;
|
---|
943 | }
|
---|
944 | }
|
---|
945 |
|
---|
946 |
|
---|
947 |
|
---|
948 | imageViewer("93 PostEND", postContoursFound);
|
---|
949 |
|
---|
950 | //Classifier Calculation
|
---|
951 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
952 | System.out.println("THIS IS SHEET MUSIC");
|
---|
953 | System.out.println(areaCounter);
|
---|
954 | }
|
---|
955 |
|
---|
956 |
|
---|
957 | }
|
---|
958 | //ClassifierComparison - Using code version 8
|
---|
959 | if(CODE_VERSION == 81) {
|
---|
960 |
|
---|
961 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
962 | Pair returnVariables = new Pair();
|
---|
963 | int FILLED = -1;
|
---|
964 | try{
|
---|
965 | //Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
966 | //System.out.println("Width: " + original1.width() + " Height: " + original1.height());
|
---|
967 | //Mat original = original1.clone();
|
---|
968 | //Imgproc.adaptiveThreshold(original1, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
969 |
|
---|
970 | Mat test = binarizedOriginal.clone();
|
---|
971 | //************************************
|
---|
972 | //Large Object Removal
|
---|
973 | //************************************
|
---|
974 | Mat srcLOR = binarizedOriginal.clone();
|
---|
975 | Mat maskLOR = new Mat();
|
---|
976 | Mat dstLOR = new Mat();
|
---|
977 |
|
---|
978 | //denoize
|
---|
979 | Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
980 | Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
|
---|
981 |
|
---|
982 | //close up gaps
|
---|
983 | Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
984 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
|
---|
985 |
|
---|
986 | //Isolate large items
|
---|
987 | Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
|
---|
988 | Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
|
---|
989 |
|
---|
990 | //Remove large items from image
|
---|
991 | Core.bitwise_not(maskLOR,maskLOR);
|
---|
992 | srcLOR.copyTo(dstLOR, maskLOR);
|
---|
993 |
|
---|
994 | //****************************************
|
---|
995 | //Small object removal (SOR)
|
---|
996 | //****************************************
|
---|
997 |
|
---|
998 | Mat srcSOR = dstLOR.clone();
|
---|
999 | Mat maskSOR = new Mat();
|
---|
1000 | Mat dstSOR = new Mat();
|
---|
1001 |
|
---|
1002 | Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
1003 | Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
|
---|
1004 |
|
---|
1005 | Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
|
---|
1006 | Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
|
---|
1007 |
|
---|
1008 |
|
---|
1009 | /* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
|
---|
1010 | Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
|
---|
1011 | imageViewer("13 isolate small - mask", maskSOR);
|
---|
1012 | */
|
---|
1013 |
|
---|
1014 | //Remove small items from image
|
---|
1015 | Core.bitwise_not(maskSOR, maskSOR);
|
---|
1016 | srcSOR.copyTo(dstSOR, maskSOR);
|
---|
1017 |
|
---|
1018 |
|
---|
1019 | //****************************************
|
---|
1020 | //start staff line detection
|
---|
1021 | //****************************************
|
---|
1022 |
|
---|
1023 | Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
|
---|
1024 | Imgproc.erode(dstSOR,test,kernelErode);
|
---|
1025 |
|
---|
1026 |
|
---|
1027 | Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
|
---|
1028 | Imgproc.dilate(test,test,kernelDilate);
|
---|
1029 |
|
---|
1030 |
|
---|
1031 | Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
1032 | Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
|
---|
1033 |
|
---|
1034 |
|
---|
1035 |
|
---|
1036 | Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
|
---|
1037 | Imgproc.erode(test,test,kernelErode02);
|
---|
1038 |
|
---|
1039 |
|
---|
1040 | //********************************************************************************
|
---|
1041 | //DETECT OUTLINE AND FIND AREA OF THESE LINES.
|
---|
1042 | //********************************************************************************
|
---|
1043 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
1044 | ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
|
---|
1045 | ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
|
---|
1046 | Mat hierarchy = new Mat();
|
---|
1047 |
|
---|
1048 | //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
|
---|
1049 | //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points
|
---|
1050 | //(hierarchy) output array: Optional output vector, containing information about the image topology.
|
---|
1051 | //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
|
---|
1052 |
|
---|
1053 | Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
1054 |
|
---|
1055 | System.out.println(contours.size());
|
---|
1056 | //Draw contours and record areas
|
---|
1057 | Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
|
---|
1058 | Mat largeContoursFound = allContoursFound.clone() ;
|
---|
1059 | Mat postContoursFound = allContoursFound.clone();
|
---|
1060 | int areaCounter = 0;
|
---|
1061 |
|
---|
1062 | //Have created a preprocess to remove large objects.
|
---|
1063 | //Need to now finalized Classifier, re try area detection.
|
---|
1064 | //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
|
---|
1065 | //Just use area and periemter to determine if sheet music
|
---|
1066 | //Discuss with david before weekend perhaps?
|
---|
1067 |
|
---|
1068 | Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
|
---|
1069 | for (int i = 0; i < contours.size(); i++) {
|
---|
1070 | double area = Imgproc.contourArea(contours.get(i));
|
---|
1071 | if(area > 100) {
|
---|
1072 | //System.out.println("AREA: " + area);
|
---|
1073 | Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
|
---|
1074 | //create list of large coutours found
|
---|
1075 | largeContours.add(contours.get(i));
|
---|
1076 | }
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | //*****************************************************************
|
---|
1080 | //Circles and centres on processed images
|
---|
1081 | //*****************************************************************
|
---|
1082 |
|
---|
1083 | //Init arrays
|
---|
1084 | Mat circleOutput = allContoursFound.clone();
|
---|
1085 | MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()];
|
---|
1086 | Point[] centers = new Point[largeContours.size()];
|
---|
1087 | float[][] radius = new float[largeContours.size()][1];
|
---|
1088 |
|
---|
1089 | //Fill arrays
|
---|
1090 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
1091 | contoursPoly[i] = new MatOfPoint2f();
|
---|
1092 | Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
|
---|
1093 | centers[i] = new Point();
|
---|
1094 | Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
|
---|
1095 |
|
---|
1096 | }
|
---|
1097 | //Draw circle for each large contour
|
---|
1098 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
1099 | Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 |
|
---|
1103 | //********************************************************************************
|
---|
1104 | //Centroids - Everything must be to scale
|
---|
1105 | //********************************************************************************
|
---|
1106 |
|
---|
1107 | ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
|
---|
1108 | Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
|
---|
1109 | for (int i = 0; i < largeContours.size(); i++) {
|
---|
1110 | mu.add(i, Imgproc.moments(largeContours.get(i), false));
|
---|
1111 | Moments p = mu.get(i);
|
---|
1112 | int x = (int) (p.get_m10() / p.get_m00());
|
---|
1113 | int y = (int) (p.get_m01() / p.get_m00());
|
---|
1114 | Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | //***********************************************
|
---|
1118 | //PostProcessing - Morphology Classifier
|
---|
1119 | // Use dilation to "Connect the dots"
|
---|
1120 | // Testing showed the centroids were clustered together
|
---|
1121 | // Then use area or perimeter as a classifier filter
|
---|
1122 | //***********************************************
|
---|
1123 |
|
---|
1124 | Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
|
---|
1125 | Imgproc.dilate(centreOutput,centreOutput,postDilate);
|
---|
1126 |
|
---|
1127 | Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
|
---|
1128 | Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
|
---|
1129 |
|
---|
1130 | Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
|
---|
1131 | Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
|
---|
1132 |
|
---|
1133 | //Find area
|
---|
1134 | Mat centreOutputGrey = new Mat();
|
---|
1135 | Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
|
---|
1136 | Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
1137 |
|
---|
1138 | for (int i = 0; i < postContours.size(); i++) {
|
---|
1139 | double area = Imgproc.contourArea(postContours.get(i));
|
---|
1140 | if(area > THRESHOLD_AREA_SIZE) {
|
---|
1141 | //System.err.println("POST AREA: " + area);
|
---|
1142 | Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
|
---|
1143 | areaCounter++;
|
---|
1144 | }
|
---|
1145 | }
|
---|
1146 | //Classifier Calculation
|
---|
1147 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
1148 | returnVariables.setBoolean(true);
|
---|
1149 | returnVariables.setInteger(areaCounter);
|
---|
1150 | }
|
---|
1151 | }
|
---|
1152 | catch(Exception e){
|
---|
1153 | System.err.println(e);
|
---|
1154 | }
|
---|
1155 | //return returnVariables;
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | //Reset after david chat
|
---|
1159 | if(CODE_VERSION == 9){
|
---|
1160 | //Display Original
|
---|
1161 | imageViewer("original", original);
|
---|
1162 | imageViewer("000 Inverse Binarized Original", binarizedOriginal);
|
---|
1163 |
|
---|
1164 |
|
---|
1165 | //************************************
|
---|
1166 | //1. Large Object Remover
|
---|
1167 | //************************************
|
---|
1168 | Mat srcLOR = binarizedOriginal.clone();
|
---|
1169 | Mat maskLOR = new Mat();
|
---|
1170 | Mat dstLOR = new Mat();
|
---|
1171 |
|
---|
1172 | //Remove small objects in prep for masking (De-Noise)
|
---|
1173 | Mat removeSmallLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
1174 | Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, removeSmallLOR);
|
---|
1175 | imageViewer("001 De-noise", maskLOR);
|
---|
1176 |
|
---|
1177 | //Heal the large items
|
---|
1178 | Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25));
|
---|
1179 | Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR);
|
---|
1180 | imageViewer("002 heal objects in mask", maskLOR);
|
---|
1181 |
|
---|
1182 | //IsolateLarge
|
---|
1183 | Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30));
|
---|
1184 | Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR);
|
---|
1185 | imageViewer("003 Isolate large", maskLOR);
|
---|
1186 |
|
---|
1187 | Core.bitwise_not(maskLOR,maskLOR);
|
---|
1188 | srcLOR.copyTo(dstLOR, maskLOR);
|
---|
1189 | imageViewer("100 Large Items Removed", dstLOR);
|
---|
1190 |
|
---|
1191 | // //CODE FROM PRESENTATION
|
---|
1192 | // Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
|
---|
1193 | // Imgproc.erode(dstTOR,base,kernelErode);
|
---|
1194 | // imageViewer("201 Erode", base);
|
---|
1195 | //
|
---|
1196 | // Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
|
---|
1197 | // Imgproc.dilate(base, base, kernelDialate);
|
---|
1198 | // imageViewer("202 Dialate", base);
|
---|
1199 | //
|
---|
1200 | // Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
|
---|
1201 | // Imgproc.erode(base,base,kernelErodeAgain);
|
---|
1202 | // imageViewer(" 203 Erode Again", base);
|
---|
1203 | //
|
---|
1204 | // Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
|
---|
1205 | // Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelClose);
|
---|
1206 | // imageViewer("204 Close", base);
|
---|
1207 | //
|
---|
1208 | // Imgproc.threshold(base,base, 127, 255, Imgproc.THRESH_BINARY);
|
---|
1209 | // imageViewer("205 Binarized", base);
|
---|
1210 | //
|
---|
1211 | // Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
|
---|
1212 | // Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
1213 | // imageViewer("206 Open", base);
|
---|
1214 | //
|
---|
1215 | // Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
|
---|
1216 | // Imgproc.dilate(base, base, kernelDialateAgain);
|
---|
1217 | // imageViewer("207 Dialate", base);
|
---|
1218 | //
|
---|
1219 | // Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
|
---|
1220 | // Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
1221 | // imageViewer("208 Close Again (Final)", base);
|
---|
1222 |
|
---|
1223 | //***********************************
|
---|
1224 | // //3.1 Remove big outlines
|
---|
1225 | // //***********************************
|
---|
1226 | // Mat srcTOR = base.clone();
|
---|
1227 | // Mat maskTOR = new Mat();
|
---|
1228 | // Mat dstTOR = new Mat();
|
---|
1229 | //
|
---|
1230 | // //Remove small objects in prep for masking (De-Noise)
|
---|
1231 | // Mat removeSmallTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
1232 | // Imgproc.morphologyEx(srcTOR,maskTOR, Imgproc.MORPH_OPEN, removeSmallTOR);
|
---|
1233 | // imageViewer("2101 Remove Small from mask", maskTOR);
|
---|
1234 | //
|
---|
1235 | // //heal the large items
|
---|
1236 | // Mat healTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
|
---|
1237 | // Imgproc.morphologyEx(maskTOR,maskTOR, Imgproc.MORPH_CLOSE, healTOR);
|
---|
1238 | // imageViewer("2102 heal objects in mask", maskTOR);
|
---|
1239 | //
|
---|
1240 | // //Highlight black outline
|
---|
1241 | // Mat highlightLargeTOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
|
---|
1242 | // Imgproc.erode(maskTOR,maskTOR,highlightLargeTOR);
|
---|
1243 | // imageViewer("2103 highlight objects", maskTOR);
|
---|
1244 | //
|
---|
1245 | // Core.bitwise_not(maskTOR, maskTOR);
|
---|
1246 | // srcTOR.copyTo(dstTOR, maskTOR);
|
---|
1247 | // imageViewer("2200 Black outline Removed", dstTOR);
|
---|
1248 |
|
---|
1249 |
|
---|
1250 |
|
---|
1251 |
|
---|
1252 |
|
---|
1253 |
|
---|
1254 |
|
---|
1255 | //4. Classify like line clusters.
|
---|
1256 | Mat base = binarizedOriginal.clone();
|
---|
1257 | //***********************************
|
---|
1258 | //3. Isolate straight lines
|
---|
1259 | //***********************************
|
---|
1260 | //Heal lines
|
---|
1261 | Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(82,1));
|
---|
1262 | Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL);
|
---|
1263 | imageViewer("202 heal objects in mask", base);
|
---|
1264 |
|
---|
1265 | //Make White Blobs
|
---|
1266 |
|
---|
1267 | //NEED THIS??
|
---|
1268 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
|
---|
1269 | Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
1270 | imageViewer("203 Open", base);
|
---|
1271 | //NEED THIS?
|
---|
1272 |
|
---|
1273 | Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
|
---|
1274 | Imgproc.dilate(base, base, kernelDilateAgain);
|
---|
1275 | imageViewer("204 Dilate", base);
|
---|
1276 |
|
---|
1277 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50));
|
---|
1278 | Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
1279 | imageViewer("205 Close Again (Final)", base);
|
---|
1280 |
|
---|
1281 | //***********************************
|
---|
1282 | //4. Find 'Clusters'
|
---|
1283 | // Need to find areas, using bounding boxes
|
---|
1284 | // Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music
|
---|
1285 | //***********************************
|
---|
1286 |
|
---|
1287 | //*****************************************************************
|
---|
1288 | //4.1 Prep Rectangles on processed images
|
---|
1289 | //*****************************************************************
|
---|
1290 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
1291 | Mat hierarchy = new Mat();
|
---|
1292 | Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
1293 | //Draw contours and record areas
|
---|
1294 | Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3);
|
---|
1295 | Mat rectOutput = allContoursFound.clone();
|
---|
1296 | Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
|
---|
1297 | //Init arrays
|
---|
1298 | MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
|
---|
1299 | Rect[] boundRect = new Rect[contours.size()];
|
---|
1300 |
|
---|
1301 | //Fill arrays
|
---|
1302 | for (int i = 0; i < contours.size(); i++) {
|
---|
1303 | contoursPoly[i] = new MatOfPoint2f();
|
---|
1304 | Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true);
|
---|
1305 | boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
|
---|
1306 | }
|
---|
1307 | //Draw circle for each large contour
|
---|
1308 | for (int i = 0; i < contours.size(); i++) {
|
---|
1309 | if(boundRect[i].height > 100){
|
---|
1310 | System.out.println("i:"+ i + " Height: " + boundRect[i].height);
|
---|
1311 | if(boundRect[i].width > 300){
|
---|
1312 | System.out.println("i:"+ i + " Width: " + boundRect[i].width);
|
---|
1313 | Imgproc.rectangle(rectOutput, boundRect[i].tl(), boundRect[i].br(), new Scalar(255, 0, 0), 20);
|
---|
1314 | }
|
---|
1315 | }
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 |
|
---|
1319 | imageViewer("4000 Rect found", rectOutput);
|
---|
1320 |
|
---|
1321 |
|
---|
1322 |
|
---|
1323 | }
|
---|
1324 | //ClassfierComparsion - Reset after david chat
|
---|
1325 | if(CODE_VERSION == 91){
|
---|
1326 | System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
---|
1327 | Pair returnVariables = new Pair();
|
---|
1328 | int areaCounter=0;
|
---|
1329 | //Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
|
---|
1330 | //Mat binarizedOriginal = original.clone();
|
---|
1331 | //Imgproc.adaptiveThreshold(original, binarizedOriginal,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
|
---|
1332 | try{
|
---|
1333 | //************************************
|
---|
1334 | //1. Large Object Remover
|
---|
1335 | //************************************
|
---|
1336 | Mat srcLOR = binarizedOriginal.clone();
|
---|
1337 | Mat maskLOR = new Mat();
|
---|
1338 | Mat dstLOR = new Mat();
|
---|
1339 |
|
---|
1340 | //Remove small objects in prep for masking (De-Noise)
|
---|
1341 | Mat removeSmallLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
|
---|
1342 | Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, removeSmallLOR);
|
---|
1343 |
|
---|
1344 | //Heal the large items
|
---|
1345 | Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25));
|
---|
1346 | Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR);
|
---|
1347 |
|
---|
1348 | //IsolateLarge
|
---|
1349 | Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30));
|
---|
1350 | Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR);
|
---|
1351 |
|
---|
1352 | Core.bitwise_not(maskLOR,maskLOR);
|
---|
1353 | srcLOR.copyTo(dstLOR, maskLOR);
|
---|
1354 |
|
---|
1355 | Mat base = binarizedOriginal.clone();
|
---|
1356 | //***********************************
|
---|
1357 | //3. Isolate straight lines
|
---|
1358 | //***********************************
|
---|
1359 | //Heal lines
|
---|
1360 | Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(82,1));
|
---|
1361 | Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL);
|
---|
1362 |
|
---|
1363 | //Make White Blobs
|
---|
1364 | Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
|
---|
1365 | Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
|
---|
1366 |
|
---|
1367 | Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
|
---|
1368 | Imgproc.dilate(base, base, kernelDilateAgain);
|
---|
1369 |
|
---|
1370 | Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50));
|
---|
1371 | Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
|
---|
1372 |
|
---|
1373 | //***********************************
|
---|
1374 | //4. Find 'Clusters'
|
---|
1375 | // Need to find areas, using bounding boxes
|
---|
1376 | // Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music
|
---|
1377 | //***********************************
|
---|
1378 |
|
---|
1379 | //*****************************************************************
|
---|
1380 | //4.1 Prep Rectangles on processed images
|
---|
1381 | //*****************************************************************
|
---|
1382 | ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
|
---|
1383 | Mat hierarchy = new Mat();
|
---|
1384 | Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
---|
1385 | //Draw contours and record areas
|
---|
1386 | Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3);
|
---|
1387 | Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
|
---|
1388 | //Init arrays
|
---|
1389 | MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
|
---|
1390 | Rect[] boundRect = new Rect[contours.size()];
|
---|
1391 |
|
---|
1392 | //Fill arrays
|
---|
1393 | for (int i = 0; i < contours.size(); i++) {
|
---|
1394 | contoursPoly[i] = new MatOfPoint2f();
|
---|
1395 | Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true);
|
---|
1396 | boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
|
---|
1397 | }
|
---|
1398 | //Draw circle for each large contour
|
---|
1399 | //MAKE THE COMPARING VALUES GLOBAL CONSTANTS
|
---|
1400 | for (int i = 0; i < contours.size(); i++) {
|
---|
1401 | if(boundRect[i].height > 100){
|
---|
1402 | if(boundRect[i].width > 300){
|
---|
1403 | areaCounter++;
|
---|
1404 | }
|
---|
1405 | }
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | //Classifier Calculation
|
---|
1409 | if(areaCounter >= THRESHOLD_AREA_COUNT){
|
---|
1410 | returnVariables.setBoolean(true);
|
---|
1411 | returnVariables.setInteger(areaCounter);
|
---|
1412 | }
|
---|
1413 | }
|
---|
1414 | catch(Exception e){
|
---|
1415 | System.err.println(e);
|
---|
1416 | }
|
---|
1417 | //return returnVariables;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 | //****************MORPHOLOGY****************************************************************************************
|
---|
1421 |
|
---|
1422 | //BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
|
---|
1423 |
|
---|
1424 | //Display Results
|
---|
1425 | //HighGui.imshow("Source", original);
|
---|
1426 | //HighGui.imshow("Just Edges", justEdges); //TESTING
|
---|
1427 |
|
---|
1428 |
|
---|
1429 | //imshow("LINESFOUND", edgesDetectedRGB);
|
---|
1430 | //HighGui.resizeWindow("LINESFOUND", 1000,1000);
|
---|
1431 |
|
---|
1432 | //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
|
---|
1433 | //HighGui.imshow("Detected Lines (in red) - negative", edgesDetectedRGBProb);
|
---|
1434 |
|
---|
1435 | //COUNT OF LINES CLASSIFICATION
|
---|
1436 | //System.out.println("LINE CLUSTER RESULT: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(0) + '\t' + "LinesFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(1) + '\t' + "ClustersFound: " + ClassifierLineClusterOLD(toBeClassifiedImg).get(2));
|
---|
1437 | //System.out.println("NEW CLUSTER RESULTS: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(0) + '\t' + "LinesFound: " + horizontalLineCount + '\t' + "ClustersFound: " + ClassifierLineClusterPt(pointArrayList,clustersFoundRGB).get(1));
|
---|
1438 | //System.out.println(ClassifierLineClusterPt(pointArrayList, clustersFoundRGB));
|
---|
1439 |
|
---|
1440 | //System.out.println("TEST: " + LineCountOrCluster(horizontalLineCount, pointArrayList, clustersFoundRGB));
|
---|
1441 |
|
---|
1442 | // Wait and Exit
|
---|
1443 | //HighGui.waitKey();
|
---|
1444 | System.exit(0);
|
---|
1445 | }
|
---|
1446 | catch(Exception e){
|
---|
1447 | System.err.println(e);
|
---|
1448 | }
|
---|
1449 | }
|
---|
1450 | }
|
---|