Changeset 33447 for other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaClassifierComparison.java
- Timestamp:
- 2019-08-30T18:03:01+12:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaClassifierComparison.java
r33439 r33447 1 2 1 import org.opencv.core.*; 3 2 import org.opencv.core.Point; … … 5 4 import org.opencv.imgcodecs.Imgcodecs; 6 5 import org.opencv.imgproc.Imgproc; 7 import static org.opencv.imgcodecs.Imgcodecs.imwrite; 6 import org.opencv.imgproc.Moments; 7 //import org.opencv.core.Core.FILLED; 8 //import org.opencv.imgcodecs.Imgcodecs.imwrite; 8 9 import java.awt.image.BufferedImage; 9 10 import java.awt.image.DataBufferByte; … … 200 201 break; 201 202 case "morphology": 202 algorithmResult = Algorithm_Morphology (imageFilename);203 algorithmResult = Algorithm_MorphologyOLD(imageFilename); 203 204 bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + algorithmResult.getBoolean() + '\t' + "Number of areas:" + '\t' + algorithmResult.getInteger() + '\t' + classifierType + '\n'); 204 205 break; … … 324 325 return returnVariables; 325 326 } 326 private static Pair Algorithm_Morphology(String filename){ 327 private static Pair Algorithm_MorphologyOLD(String filename){ 328 327 329 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 328 330 Boolean isSheetMusic = null; … … 365 367 } 366 368 } 369 370 371 372 373 374 367 375 //Calculates if sheet music or not 368 376 if(areaCounter >= THRESHOLD_AREA_COUNT){ … … 376 384 return returnVariables; 377 385 } 378 386 private static Pair Algorithm_Morphology(String filename){ 387 388 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 389 Boolean isSheetMusic = null; 390 Pair returnVariables = new Pair(); 391 try{ 392 int FILLED = -1; 393 //Display Original 394 //imageViewer("original", original1); 395 Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE); 396 Mat test = original.clone(); 397 //imageViewer("00 Inverse Binarized Original", test); 398 399 400 //************************************ 401 //Large Object Removal 402 //************************************ 403 Mat srcLOR = original.clone(); 404 Mat maskLOR = new Mat(); 405 Mat dstLOR = new Mat(); 406 407 //denoize 408 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 409 Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize); 410 //imageViewer("01 Denoize - mask", maskLOR); 411 412 //close up gaps 413 Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5)); 414 Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser); 415 //imageViewer("02 gap closer - mask", maskLOR); 416 417 //Isolate large items 418 Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8)); 419 Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge); 420 //imageViewer("03 Isolate Large - mask", maskLOR); 421 422 //Remove large items from image 423 Core.bitwise_not(maskLOR,maskLOR); 424 srcLOR.copyTo(dstLOR, maskLOR); 425 //imageViewer("04 Large Items Removed", dstLOR); 426 427 //**************************************** 428 //Small object removal (SOR) 429 //**************************************** 430 431 Mat srcSOR = dstLOR.clone(); 432 Mat maskSOR = new Mat(); 433 Mat dstSOR = new Mat(); 434 435 Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7)); 436 Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR); 437 //imageViewer("11 show small - mask", maskSOR); 438 439 Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7)); 440 Imgproc.dilate(maskSOR, maskSOR, highlightSmall); 441 //imageViewer("12 highlight small - mask", maskSOR); 442 443 /* Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10)); 444 Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall); 445 imageViewer("13 isolate small - mask", maskSOR); 446 */ 447 448 //Remove small items from image 449 Core.bitwise_not(maskSOR, maskSOR); 450 srcSOR.copyTo(dstSOR, maskSOR); 451 //imageViewer("14 Small Items Removed", dstSOR); 452 453 454 //**************************************** 455 //start staff line detection 456 //**************************************** 457 458 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2 459 Imgproc.erode(dstSOR,test,kernelErode); 460 //imageViewer("21 Erode plus pre", test); 461 462 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3 463 Imgproc.dilate(test,test,kernelDilate); 464 //imageViewer("22 Dilate", test); 465 466 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4 467 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose); 468 //imageViewer("23 Close", test); 469 470 471 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1 472 Imgproc.erode(test,test,kernelErode02); 473 //imageViewer("24 Erode (Final)", test); 474 475 //******************************************************************************** 476 //DETECT OUTLINE AND FIND AREA OF THESE LINES. 477 //******************************************************************************** 478 ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 479 ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>(); 480 ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>(); 481 Mat hierarchy = new Mat(); 482 483 //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method. 484 //(contours) output array of arrays: Detected contours. Each contour is stored as a vector of points 485 //(hierarchy) output array: Optional output vector, containing information about the image topology. 486 //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a 487 488 Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 489 490 System.out.println(contours.size()); 491 //Draw contours and record areas 492 Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3); 493 Mat largeContoursFound = allContoursFound.clone() ; 494 Mat postContoursFound = allContoursFound.clone(); 495 int areaCounter = 0; 496 497 //Have created a preprocess to remove large objects. 498 //Need to now finalized Classifier, re try area detection. 499 //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter) 500 //Just use area and periemter to determine if sheet music 501 //Discuss with david before weekend perhaps? 502 503 Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8 504 for (int i = 0; i < contours.size(); i++) { 505 double area = Imgproc.contourArea(contours.get(i)); 506 if(area > 100) { 507 //System.out.println("AREA: " + area); 508 Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED); 509 //create list of large coutours found 510 largeContours.add(contours.get(i)); 511 } 512 } 513 //imageViewer("80 All Contours found", allContoursFound); 514 //imageViewer("81 Large Contours Found", largeContoursFound); 515 516 //***************************************************************** 517 //Circles and centres on processed images 518 //***************************************************************** 519 520 //Init arrays 521 Mat circleOutput = allContoursFound.clone(); 522 MatOfPoint2f[] contoursPoly = new MatOfPoint2f[largeContours.size()]; 523 Point[] centers = new Point[largeContours.size()]; 524 float[][] radius = new float[largeContours.size()][1]; 525 526 //Fill arrays 527 for (int i = 0; i < largeContours.size(); i++) { 528 contoursPoly[i] = new MatOfPoint2f(); 529 Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true); 530 centers[i] = new Point(); 531 Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]); 532 533 } 534 //Draw circle for each large contour 535 for (int i = 0; i < largeContours.size(); i++) { 536 Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1); 537 } 538 //imageViewer("82 Circles found", circleOutput); 539 540 //******************************************************************************** 541 //Centroids - Everything must be to scale 542 //******************************************************************************** 543 544 ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size()); 545 Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3); 546 for (int i = 0; i < largeContours.size(); i++) { 547 mu.add(i, Imgproc.moments(largeContours.get(i), false)); 548 Moments p = mu.get(i); 549 int x = (int) (p.get_m10() / p.get_m00()); 550 int y = (int) (p.get_m01() / p.get_m00()); 551 Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30); 552 } 553 //imageViewer("83 Centres found", centreOutput); 554 555 556 //*********************************************** 557 //PostProcessing - Morphology Classifier 558 // Use dilation to "Connect the dots" 559 // Testing showed the centroids were clustered together 560 // Then use area or perimeter as a classifier filter 561 //*********************************************** 562 563 Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15)); 564 Imgproc.dilate(centreOutput,centreOutput,postDilate); 565 //imageViewer("91 PostDilated", centreOutput); 566 567 Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4 568 Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose); 569 //imageViewer("92 PostClose", centreOutput); 570 571 Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100)); 572 Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize); 573 //imageViewer("93 PostDenoize", centreOutput); 574 575 //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50)); 576 //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline); 577 578 //Find area 579 Mat centreOutputGrey = new Mat(); 580 Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY); 581 Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 582 583 for (int i = 0; i < postContours.size(); i++) { 584 double area = Imgproc.contourArea(postContours.get(i)); 585 if(area > THRESHOLD_AREA_SIZE) { 586 System.out.println("POST AREA: " + area + "AREA COUNTER: " + areaCounter); 587 //Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED); 588 areaCounter++; 589 } 590 } 591 592 //imageViewer("93 PostEND", postContoursFound); 593 594 //Calculates if sheet music or not 595 if(areaCounter >= THRESHOLD_AREA_COUNT){ 596 returnVariables = new Pair(true, areaCounter); 597 //returnVariables.setBoolean(true); 598 //returnVariables.setInteger(areaCounter); 599 System.out.println("TEST RETURN VARIABLES: "+ returnVariables.toString()); 600 } 601 602 } 603 catch(Exception e){ 604 System.err.println(e); 605 } 606 return returnVariables; 607 } 379 608 //****************** 380 609 //CLASSIFIER FUNCTIONS
Note:
See TracChangeset
for help on using the changeset viewer.