Changeset 33444 for other-projects
- Timestamp:
- 2019-08-29T16:57:08+12:00 (5 years ago)
- Location:
- other-projects/is-sheet-music-encore/trunk/image-identification-dev-02
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/.idea/workspace.xml
r33439 r33444 15 15 <entry file="file://$PROJECT_DIR$/image-identification-development/src/Main.java"> 16 16 <provider selected="true" editor-type-id="text-editor"> 17 <state relative-caret-position=" 874">18 <caret line=" 116" column="48" selection-start-line="116" selection-start-column="48" selection-end-line="116" selection-end-column="48" />17 <state relative-caret-position="3154"> 18 <caret line="820" column="49" selection-start-line="820" selection-start-column="49" selection-end-line="820" selection-end-column="49" /> 19 19 <folding> 20 20 <element signature="imports" expanded="true" /> … … 40 40 <entry file="file://$PROJECT_DIR$/image-identification-development/src/MainMorph.java"> 41 41 <provider selected="true" editor-type-id="text-editor"> 42 <state relative-caret-position="2 66">43 <caret line=" 407" column="55" selection-start-line="407" selection-start-column="55" selection-end-line="407" selection-end-column="55" />42 <state relative-caret-position="221"> 43 <caret line="182" column="12" selection-start-line="182" selection-start-column="12" selection-end-line="182" selection-end-column="12" /> 44 44 <folding> 45 45 <element signature="imports" expanded="true" /> 46 <element signature="class#StartAndEndPoint#0;class#MainMorph#0" /> 47 <element signature="method#toBufferedImage#0;class#MainMorph#0" /> 48 <element signature="e#11164#13670#1" /> 49 <element signature="e#13754#15793#1" /> 50 <element signature="e#15861#18508#1" /> 51 <element signature="e#21594#25229#1" /> 46 <element signature="e#7379#7795#1" /> 47 <element signature="e#7580#7900#1" /> 48 <element signature="e#11581#14087#1" /> 49 <element signature="e#14171#16210#1" /> 50 <element signature="e#16278#18925#1" /> 51 <element signature="e#19007#24734#1" /> 52 <element signature="e#24817#28826#1" /> 53 <element signature="e#28892#30082#1" /> 54 <element signature="e#30149#34144#1" /> 55 <element signature="e#34814#34826#0" expanded="true" /> 52 56 </folding> 53 57 </state> … … 58 62 <entry file="file://$PROJECT_DIR$/../image-identification-terminal/javaClassifierComparison.java"> 59 63 <provider selected="true" editor-type-id="text-editor"> 60 <state relative-caret-position="4 332">64 <state relative-caret-position="4598"> 61 65 <caret line="356" selection-start-line="356" selection-end-line="356" /> 62 66 <folding> … … 111 115 <foldersAlwaysOnTop value="true" /> 112 116 </navigator> 113 <panes> 114 <pane id="ProjectPane" /> 115 <pane id="PackagesPane" /> 116 <pane id="Scope" /> 117 </panes> 117 <panes /> 118 118 </component> 119 119 <component name="PropertiesComponent"> … … 167 167 <component name="ToolWindowManager"> 168 168 <frame x="67" y="25" width="1613" height="1025" extended-state="6" /> 169 <editor active="true" />170 169 <layout> 171 170 <window_info content_ui="combo" id="Project" order="0" weight="0.19783024" /> … … 175 174 <window_info id="Favorites" order="4" side_tool="true" /> 176 175 <window_info anchor="bottom" id="Message" order="0" /> 177 <window_info anchor="bottom" id="Find" order="1" />178 <window_info a ctive="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.2494432" />179 <window_info anchor="bottom" id="Debug" order="3" weight="0. 4" />176 <window_info anchor="bottom" id="Find" order="1" weight="0.32962137" /> 177 <window_info anchor="bottom" id="Run" order="2" weight="0.2494432" /> 178 <window_info anchor="bottom" id="Debug" order="3" weight="0.3997773" /> 180 179 <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> 181 180 <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> … … 223 222 <entry file="file://$PROJECT_DIR$/image-identification-development/src/Main.java"> 224 223 <provider selected="true" editor-type-id="text-editor"> 225 <state relative-caret-position=" 874">226 <caret line=" 116" column="48" selection-start-line="116" selection-start-column="48" selection-end-line="116" selection-end-column="48" />224 <state relative-caret-position="3154"> 225 <caret line="820" column="49" selection-start-line="820" selection-start-column="49" selection-end-line="820" selection-end-column="49" /> 227 226 <folding> 228 227 <element signature="imports" expanded="true" /> … … 246 245 <entry file="file://$PROJECT_DIR$/../image-identification-terminal/javaClassifierComparison.java"> 247 246 <provider selected="true" editor-type-id="text-editor"> 248 <state relative-caret-position="4 332">247 <state relative-caret-position="4598"> 249 248 <caret line="356" selection-start-line="356" selection-end-line="356" /> 250 249 <folding> … … 263 262 <entry file="file://$PROJECT_DIR$/image-identification-development/src/MainMorph.java"> 264 263 <provider selected="true" editor-type-id="text-editor"> 265 <state relative-caret-position="2 66">266 <caret line=" 407" column="55" selection-start-line="407" selection-start-column="55" selection-end-line="407" selection-end-column="55" />264 <state relative-caret-position="221"> 265 <caret line="182" column="12" selection-start-line="182" selection-start-column="12" selection-end-line="182" selection-end-column="12" /> 267 266 <folding> 268 267 <element signature="imports" expanded="true" /> 269 <element signature="class#StartAndEndPoint#0;class#MainMorph#0" /> 270 <element signature="method#toBufferedImage#0;class#MainMorph#0" /> 271 <element signature="e#11164#13670#1" /> 272 <element signature="e#13754#15793#1" /> 273 <element signature="e#15861#18508#1" /> 274 <element signature="e#21594#25229#1" /> 268 <element signature="e#7379#7795#1" /> 269 <element signature="e#7580#7900#1" /> 270 <element signature="e#11581#14087#1" /> 271 <element signature="e#14171#16210#1" /> 272 <element signature="e#16278#18925#1" /> 273 <element signature="e#19007#24734#1" /> 274 <element signature="e#24817#28826#1" /> 275 <element signature="e#28892#30082#1" /> 276 <element signature="e#30149#34144#1" /> 277 <element signature="e#34814#34826#0" expanded="true" /> 275 278 </folding> 276 279 </state> -
other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java
r33439 r33444 63 63 import org.opencv.imgcodecs.Imgcodecs; 64 64 import org.opencv.imgproc.Imgproc; 65 import org.opencv.imgproc.Moments; 66 import org.opencv.osgi.OpenCVNativeLoader; 67 import org.opencv.photo.Photo; 68 65 66 import static org.opencv.core.Core.FILLED; 69 67 import static org.opencv.core.CvType.CV_8UC3; 70 68 import static org.opencv.highgui.HighGui.imshow; 71 69 import static org.opencv.imgcodecs.Imgcodecs.imwrite; 72 import java.awt.image.BufferedImage; 73 import java.awt.image.DataBufferByte; 70 74 71 import java.io.File; 75 72 import java.util.ArrayList; 76 import java.util.Collection;77 import java.util.Collections;78 import java.util.Comparator;79 import javax.imageio.ImageIO;80 73 81 74 //REFERENCES: … … 96 89 //http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html 97 90 //https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java 91 //https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values 98 92 99 93 … … 115 109 116 110 public class MainMorph { 111 //CODE VERSIONS 112 static int CODE_VERSION = 8; 113 static int IMAGE_VERSION = 3; 117 114 //GLOBAL_CONSTANTS 118 115 … … 122 119 123 120 // 124 static class StartAndEndPoint { 125 //PRIVATES 126 private Point _p1; 127 private Point _p2; 128 //CONSTRUCTOR 129 public StartAndEndPoint(Point p1, Point p2){ 130 _p1 = p1; 131 _p2 = p2; 132 } 133 //GETTERS 134 public Point getP1(){ 135 return _p1; 136 } 137 public Point getP2(){ 138 return _p2; 139 } 140 //SETTERS 141 public void setP1(Point p1){ 142 _p1 = p1; 143 } 144 public void setP2(Point p2){ 145 _p2 = p2; 146 } 147 148 //ToString 149 public String toString(){ 150 return "Start: " + _p1 + " End: " + _p2; 151 } 152 153 } 154 private static BufferedImage toBufferedImage(Mat mat){ 155 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 156 //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 157 //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object 158 try{ 159 int type = BufferedImage.TYPE_3BYTE_BGR; 160 int bufferSize = mat.channels() * mat.cols() * mat.rows(); 161 byte[] b = new byte[bufferSize]; 162 //get all the pixels 163 mat.get(0, 0, b); 164 BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type); 165 final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 166 System.arraycopy(b, 0, targetPixels, 0, b.length); 167 return image; 168 } 169 catch(Exception e){ 170 System.err.println(e); 171 } 172 return null; 173 } 174 175 private static void showWaitDestroy(String winname, Mat img) { 121 122 private static void imageViewer(String winName, Mat img) { 176 123 try { 177 HighGui.namedWindow(winname, HighGui.WINDOW_AUTOSIZE); 178 imshow(winname, img); 179 HighGui.resizeWindow(winname, 1000, 1000); 180 HighGui.moveWindow(winname, 500, 0); 181 HighGui.waitKey(0); 182 183 HighGui.destroyWindow(winname); 124 //Internal display - Overview - Will Distort High Res images 125 if(IMAGE_VERSION == 1) { 126 HighGui.namedWindow(winName, HighGui.WINDOW_NORMAL); 127 HighGui.resizeWindow(winName, 1000, 1000); 128 imshow(winName, img); 129 130 HighGui.moveWindow(winName, 500, 0); 131 HighGui.waitKey(0); 132 133 HighGui.destroyWindow(winName); 134 } 135 //Internal display - Segmented - Will _NOT_ Distort High Res images 136 if(IMAGE_VERSION == 2) { 137 HighGui.namedWindow(winName, HighGui.WINDOW_AUTOSIZE); 138 HighGui.resizeWindow(winName, 1000, 1000); 139 imshow(winName, img); 140 HighGui.moveWindow(winName, 500, 0); 141 HighGui.waitKey(0); 142 143 HighGui.destroyWindow(winName); 144 } 145 //External display - Save Images for manual viewing 146 if(IMAGE_VERSION == 3) { 147 //save file (testing purposes) 148 imwrite(winName+".jpg", img); 149 } 184 150 } 185 151 catch (Exception e){ … … 189 155 //MAIN 190 156 public static void main(String[] args) { 191 192 157 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 193 194 158 try { 195 196 ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();197 198 159 //Variables 199 int codeVersion = 4; 200 System.out.println("Running code version: " + codeVersion); 160 System.out.println("Running code version: " + CODE_VERSION); 201 161 Mat edgesDetected = new Mat(); 202 162 Mat mid = new Mat(); … … 218 178 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png"; 219 179 //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png"; 220 String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; 180 //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; 181 //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png"; 182 //String default_file =hiresDirectory+"MU/NotSheetMusic/mdp.39015096363935-1.png"; 183 String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png"; 221 184 222 185 //System.out.println(default_file); … … 236 199 // Load an image 237 200 Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 201 System.out.println("Width: " + original1.width() + " Height: " + original1.height()); 238 202 Mat original = original1.clone(); 239 203 … … 251 215 252 216 //dynamic morphology?? 253 if( codeVersion== 1) {217 if(CODE_VERSION == 1) { 254 218 int hori = original.width(); 255 219 int vert = original.height(); … … 261 225 262 226 Mat test = original.clone(); 263 showWaitDestroy("Original", test);227 imageViewer("Original", test); 264 228 265 229 System.out.println("hori: " + hori + '\t' + "vert: " + vert); … … 268 232 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100))); 269 233 Imgproc.erode(test,test,kernelErode); 270 showWaitDestroy("01 Erode", test);234 imageViewer("01 Erode", test); 271 235 272 236 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10))); 273 237 Imgproc.dilate(test, test, kernelDialate); 274 showWaitDestroy("02 Dialate", test);238 imageViewer("02 Dialate", test); 275 239 276 240 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5))); 277 241 Imgproc.erode(test,test,kernelErodeAgain); 278 showWaitDestroy(" 03 Erode Again", test);242 imageViewer(" 03 Erode Again", test); 279 243 280 244 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3)); 281 245 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose); 282 showWaitDestroy("04 Close", test);246 imageViewer("04 Close", test); 283 247 284 248 Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 285 showWaitDestroy("05 Binarized", test);249 imageViewer("05 Binarized", test); 286 250 287 251 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20))); 288 252 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen); 289 showWaitDestroy(" 06 Open", test);253 imageViewer(" 06 Open", test); 290 254 291 255 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100))); 292 256 Imgproc.dilate(test, test, kernelDialateAgain); 293 showWaitDestroy("07 Dialate", test);257 imageViewer("07 Dialate", test); 294 258 295 259 296 260 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2))); 297 261 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain); 298 showWaitDestroy(" 08 Close Again (Final)", test);262 imageViewer(" 08 Close Again (Final)", test); 299 263 } 300 264 //Successful hardcode for morhpology 301 if ( codeVersion== 2) {265 if (CODE_VERSION == 2) { 302 266 303 267 //MAKE SURE BLACK & WHITE 304 268 Mat test = original.clone(); 305 showWaitDestroy("00 Binarized Original", test);269 imageViewer("00 Binarized Original", test); 306 270 307 271 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1)); 308 272 Imgproc.erode(test,test,kernelErode); 309 showWaitDestroy("01 Erode", test);273 imageViewer("01 Erode", test); 310 274 311 275 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10)); 312 276 Imgproc.dilate(test, test, kernelDialate); 313 showWaitDestroy("02 Dialate", test);277 imageViewer("02 Dialate", test); 314 278 315 279 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20)); 316 280 Imgproc.erode(test,test,kernelErodeAgain); 317 showWaitDestroy(" 03 Erode Again", test);281 imageViewer(" 03 Erode Again", test); 318 282 319 283 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20)); 320 284 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose); 321 showWaitDestroy("04 Close", test);285 imageViewer("04 Close", test); 322 286 323 287 // Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY); 324 // showWaitDestroy("05 Binarized", test);288 // imageViewer("05 Binarized", test); 325 289 326 290 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4)); 327 291 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen); 328 showWaitDestroy(" 06 Open", test);292 imageViewer(" 06 Open", test); 329 293 330 294 // Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10)); 331 295 // Imgproc.dilate(test, test, kernelDialateAgain); 332 // showWaitDestroy("07 Dialate", test);296 // imageViewer("07 Dialate", test); 333 297 334 298 //FIGURE OUT FLOOD FILL!! … … 338 302 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50)); 339 303 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain); 340 showWaitDestroy(" 08 Close Again (Final)", test);304 imageViewer(" 08 Close Again (Final)", test); 341 305 342 306 } 343 307 //Tutorial/Demo Code 344 if ( codeVersion== 3) {308 if (CODE_VERSION == 3) { 345 309 Mat horizontal = original.clone(); 346 310 Mat vertical = original.clone(); … … 353 317 Imgproc.dilate(horizontal, horizontal, horizontalStructure); 354 318 // Show extracted horizontal lines 355 showWaitDestroy("horizontal", horizontal);319 imageViewer("horizontal", horizontal); 356 320 // Specify size on vertical axis 357 321 int vertical_size = vertical.rows() / 30; … … 362 326 Imgproc.dilate(vertical, vertical, verticalStructure); 363 327 // Show extracted vertical lines 364 showWaitDestroy("vertical", vertical);328 imageViewer("vertical", vertical); 365 329 // Inverse vertical image 366 330 Core.bitwise_not(vertical, vertical); 367 showWaitDestroy("vertical_bit", vertical);331 imageViewer("vertical_bit", vertical); 368 332 // Extract edges and smooth image according to the logic 369 333 // 1. extract edges … … 375 339 Mat edges = new Mat(); 376 340 Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2); 377 showWaitDestroy("edges", edges);341 imageViewer("edges", edges); 378 342 // Step 2 379 343 Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1); 380 344 Imgproc.dilate(edges, edges, kernel); 381 showWaitDestroy("dilate", edges);345 imageViewer("dilate", edges); 382 346 // Step 3 383 347 Mat smooth = new Mat(); … … 388 352 smooth.copyTo(vertical, edges); 389 353 // Show final result 390 showWaitDestroy("smooth - final", vertical);354 imageViewer("smooth - final", vertical); 391 355 System.exit(0); 392 356 } 393 357 //Better morphology attempt - static 394 if( codeVersion==4) {358 if(CODE_VERSION ==4) { 395 359 396 360 //Display Original 397 showWaitDestroy("original", original1);361 imageViewer("original", original1); 398 362 399 363 Mat test = original.clone(); 400 401 showWaitDestroy("00 Inverse Binarized Original", test); 364 Mat pre = original.clone(); 365 Mat dst = new Mat(); 366 367 imageViewer("00 Inverse Binarized Original", test); 402 368 403 369 //remove large items of no interest pre proc … … 411 377 //denoize 412 378 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)); 413 Imgproc.morphologyEx( test,test, Imgproc.MORPH_OPEN, denoize);414 showWaitDestroy("Denoize", test);379 Imgproc.morphologyEx(pre,pre, Imgproc.MORPH_OPEN, denoize); 380 imageViewer("Denoize - PRE", pre); 415 381 416 382 //close up gaps 417 Mat gapcloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)); 418 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, gapcloser); 419 showWaitDestroy("gap closer", test); 420 421 Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 50)); 422 Imgproc.erode(test,test, kernelRemoveLarge); 423 showWaitDestroy("Remove Large", test); 383 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 384 Imgproc.morphologyEx(pre,pre,Imgproc.MORPH_CLOSE, gapCloser); 385 imageViewer("gap closer - PRE", pre); 386 387 Mat kernelHighlightLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10)); 388 Imgproc.erode(pre,pre, kernelHighlightLarge); 389 imageViewer("Highlight Large - PRE", pre); 390 391 //change white pixels to red 392 ArrayList<MatOfPoint> contoursPre = new ArrayList<MatOfPoint>(); 393 Mat hierarchyPre = new Mat(); 394 395 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method. 396 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points 397 //(hierarchy) output array: Optional output vector, containing information about the image topology. 398 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a 399 400 Imgproc.findContours(pre, contoursPre, hierarchyPre, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 401 402 //Draw contours and record areas 403 404 Mat drawingPre = Mat.zeros(test.size(), CvType.CV_8UC3); 405 Mat mask = new Mat(drawingPre.size(), CV_8UC3,new Scalar(0)); 406 Scalar colorPre = new Scalar(255, 255, 255); 407 Imgproc.drawContours(mask, contoursPre, -1, new Scalar(255, 255, 255), FILLED); 408 Imgproc.fillPoly(drawingPre, contoursPre,colorPre); 409 imageViewer("DRAWINGPRE", drawingPre); 410 411 412 //FIIIIIIIIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXX 413 //Remove from main Mat 414 Core.bitwise_not(mask,mask); 415 imageViewer("MASK", mask); 416 imageViewer("TEST", test); 417 418 drawingPre.copyTo(test, mask); 419 imageViewer("COMBINE", test); 424 420 425 421 //start staff line detection … … 427 423 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1)); 428 424 Imgproc.erode(test,test,kernelErode); 429 showWaitDestroy("01 Erode", test);425 imageViewer("01 Erode plus pre", test); 430 426 431 427 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3)); 432 428 Imgproc.dilate(test,test,kernelDilate); 433 showWaitDestroy("02 Dilate", test);429 imageViewer("02 Dilate", test); 434 430 435 431 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4)); 436 432 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening); 437 showWaitDestroy("03 Open", test);433 imageViewer("03 Open", test); 438 434 439 435 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8)); 440 436 Imgproc.erode(test,test,kernelErode02); 441 showWaitDestroy("04 Erode (Final)", test);437 imageViewer("04 Erode (Final)", test); 442 438 443 439 … … 475 471 476 472 //Show in a window 477 showWaitDestroy("Contours", drawing);473 imageViewer("Contours", drawing); 478 474 } 479 475 //Better morphology attempt - dynamic 480 if( codeVersion==5) {476 if(CODE_VERSION ==5) { 481 477 int hori = original.width(); 482 478 int vert = original.height(); … … 494 490 System.out.println(hori + " " + vert + " " + sizeX1 + " " + sizeY1); 495 491 //Display Original 496 showWaitDestroy("original", original1);492 imageViewer("original", original1); 497 493 498 494 Mat test = original.clone(); 499 showWaitDestroy("00 Inverse Binarized Original", test);495 imageViewer("00 Inverse Binarized Original", test); 500 496 501 497 //Remove very large and wide black spaces (8th of the page) 502 498 //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8)); 503 499 //Imgproc.erode(test,test, kernelRemoveLarge); 504 // showWaitDestroy("Remove Large", test);500 //imageViewer("Remove Large", test); 505 501 506 502 //Eliminate things that are not long and thin 507 503 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1)); 508 504 Imgproc.erode(test,test,kernelErode); 509 showWaitDestroy("01 Erode", test);505 imageViewer("01 Erode", test); 510 506 511 507 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3)); 512 508 Imgproc.dilate(test,test,kernelDilate); 513 showWaitDestroy("02 Dilate", test);509 imageViewer("02 Dilate", test); 514 510 515 511 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4)); 516 512 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose); 517 showWaitDestroy("03 Open", test);513 imageViewer("03 Open", test); 518 514 519 515 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8)); 520 516 Imgproc.erode(test,test,kernelErode02); 521 showWaitDestroy("04 Erode (Final)", test);517 imageViewer("04 Erode (Final)", test); 522 518 523 519 … … 554 550 555 551 //Show in a window 556 showWaitDestroy("Contours", drawing); 557 } 558 //ADD PRE PROC 552 imageViewer("Contours", drawing); 553 } 554 //MASK UNDERSTANDING 555 if(CODE_VERSION ==6) { 556 String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png"; 557 Mat mask = new Mat(); 558 Mat dst = new Mat(); 559 //Get source image and binerize 560 Mat src = Imgcodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE); 561 Imgproc.adaptiveThreshold(original1, src,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C); 562 imageViewer("src", src); 563 564 //Find unwanted material, then invert it so mask removes not keeps. 565 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1)); 566 Imgproc.erode(src,mask,kernelErode); 567 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10)); 568 Imgproc.dilate(mask, mask, kernelDilate); 569 Core.bitwise_not(mask,mask); 570 imageViewer("mask", mask); 571 572 //Copy source image to new Mat, with mask in use 573 src.copyTo(dst, mask); 574 imageViewer("dst", dst); 575 576 577 578 579 } 580 //Mask implementation 581 if(CODE_VERSION ==7) { 582 583 //Display Original 584 imageViewer("original", original1); 585 586 Mat src = original.clone(); 587 Mat test = original.clone(); 588 Mat mask = new Mat(); 589 Mat dst = new Mat(); 590 591 imageViewer("00 Inverse Binarized Original", src); 592 593 //denoize 594 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)); 595 Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize); 596 imageViewer("01 Denoize - mask", mask); 597 598 //close up gaps 599 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 600 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser); 601 imageViewer("02 gap closer - mask", mask); 602 603 //Isolate large items 604 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8)); 605 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge); 606 imageViewer("03 Isolate Large - mask", mask); 607 Core.bitwise_not(mask,mask); 608 609 //Remove unwanted large items from image 610 src.copyTo(dst, mask); 611 imageViewer("04 Large Items Removed", dst); 612 613 //start staff line detection 614 615 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1)); 616 Imgproc.erode(dst,test,kernelErode); 617 imageViewer("11 Erode plus pre", test); 618 619 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3)); 620 Imgproc.dilate(test,test,kernelDilate); 621 imageViewer("12 Dilate", test); 622 623 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4)); 624 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening); 625 imageViewer("13 Open", test); 626 627 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8)); 628 Imgproc.erode(test,test,kernelErode02); 629 imageViewer("14 Erode (Final)", test); 630 631 632 //DETECT OUTLINE AND FIND AREA OF THESE LINES. 633 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 634 Mat hierarchy = new Mat(); 635 636 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method. 637 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points 638 //(hierarchy) output array: Optional output vector, containing information about the image topology. 639 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a 640 641 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 642 643 //Draw contours and record areas 644 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3); 645 int areaCounter = 0; 646 647 648 Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), FILLED); 649 // for (int i = 0; i < contours.size(); i++) { 650 // Scalar color = new Scalar(0, i, i); 651 // double area = Imgproc.contourArea(contours.get(i)); 652 // Imgproc.drawContours(drawing, contours, i, color, FILLED); 653 // System.out.println("AREA: " + area); 654 // 655 // } 656 imageViewer("Contours found", drawing); 657 658 //Classifier Calculation 659 if(areaCounter >= THRESHOLD_AREA_COUNT){ 660 System.out.println("THIS IS SHEET MUSIC"); 661 System.out.println(areaCounter); 662 } 663 664 665 } 666 //Mask implementation - HIGH RES NUMBER MOD 667 if(CODE_VERSION ==8) { 668 669 //Display Original 670 imageViewer("original", original1); 671 672 Mat src = original.clone(); 673 Mat test = original.clone(); 674 Mat mask = new Mat(); 675 Mat dst = new Mat(); 676 677 imageViewer("00 Inverse Binarized Original", src); 678 679 //Close then Open 680 681 // Mat firstKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 682 // Imgproc.morphologyEx(src, mask, Imgproc.MORPH_CLOSE, firstKernel); 683 // imageViewer("01 Closed - mask", mask); 684 // 685 // Mat secondKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 686 // Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, secondKernel); 687 // imageViewer("02 Open - mask", mask); 688 689 690 //denoize 691 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 692 Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize); 693 imageViewer("01 Denoize - mask", mask); 694 695 //close up gaps 696 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 697 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser); 698 imageViewer("02 gap closer - mask", mask); 699 700 //Isolate large items 701 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8)); 702 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge); 703 imageViewer("03 Isolate Large - mask", mask); 704 Core.bitwise_not(mask,mask); 705 706 //Remove unwanted large items from image 707 src.copyTo(dst, mask); 708 imageViewer("04 Large Items Removed", dst); 709 710 //start staff line detection 711 712 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2 713 Imgproc.erode(dst,test,kernelErode); 714 imageViewer("11 Erode plus pre", test); 715 716 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3 717 Imgproc.dilate(test,test,kernelDilate); 718 imageViewer("12 Dilate", test); 719 720 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4 721 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening); 722 imageViewer("13 Open", test); 723 724 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1 725 Imgproc.erode(test,test,kernelErode02); 726 imageViewer("14 Erode (Final)", test); 727 728 729 //DETECT OUTLINE AND FIND AREA OF THESE LINES. 730 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 731 Mat hierarchy = new Mat(); 732 733 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method. 734 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points 735 //(hierarchy) output array: Optional output vector, containing information about the image topology. 736 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a 737 738 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 739 740 //Draw contours and record areas 741 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3); 742 int areaCounter = 0; 743 744 //Have created a preprocess to remove large objects. 745 //Need to now finalized Classifier, re try area detection. 746 //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter) 747 //Just use area and periemter to determine if sheet music 748 //Discuss with david before weekend perhaps? 749 750 Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8 751 // for (int i = 0; i < contours.size(); i++) { 752 // Scalar color = new Scalar(0, i, i); 753 // double area = Imgproc.contourArea(contours.get(i)); 754 // Imgproc.drawContours(drawing, contours, i, color, FILLED); 755 // System.out.println("AREA: " + area); 756 // 757 // } 758 imageViewer("Contours found", drawing); 759 760 //Classifier Calculation 761 if(areaCounter >= THRESHOLD_AREA_COUNT){ 762 System.out.println("THIS IS SHEET MUSIC"); 763 System.out.println(areaCounter); 764 } 765 766 767 } 559 768 560 769 //USE stuc element, to rule out large wide and long pieces of black and white.
Note:
See TracChangeset
for help on using the changeset viewer.