Ignore:
Timestamp:
2019-09-07T14:30:32+12:00 (5 years ago)
Author:
cpb16
Message:

Running new morphology version after quick meeting with david last week. init tests are looking good. Running on whole corpus now...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaClassifierComparison.java

    r33449 r33458  
    328328        return returnVariables;
    329329    }
    330     private static Pair Algorithm_MorphologyOLD(String filename){
     330    private static Pair Algorithm_MorphologyOLD2(String filename){
    331331
    332332        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     
    386386        return returnVariables;
    387387    }
    388     private static Pair Algorithm_Morphology(String filename){
     388    private static Pair Algorithm_MorphologyOLD(String filename){
    389389
    390390        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     
    573573                }
    574574            }
     575            //Classifier Calculation
     576            if(areaCounter >= THRESHOLD_AREA_COUNT){
     577                returnVariables.setBoolean(true);
     578                returnVariables.setInteger(areaCounter);
     579            }
     580        }
     581        catch(Exception e){
     582            System.err.println(e);
     583        }
     584        return returnVariables;
     585    }
     586    private static Pair Algorithm_Morphology(String filename){
     587        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     588        Pair returnVariables = new Pair();
     589        int areaCounter=0;
     590        Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
     591        Mat binarizedOriginal = original.clone();
     592        Imgproc.adaptiveThreshold(original, binarizedOriginal,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
     593        try{
     594            //************************************
     595            //1. Large Object Remover
     596            //************************************
     597            Mat srcLOR = binarizedOriginal.clone();
     598            Mat maskLOR = new Mat();
     599            Mat dstLOR = new Mat();
     600
     601            //Remove small objects in prep for masking  (De-Noise)
     602            Mat removeSmallLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
     603            Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, removeSmallLOR);
     604
     605            //Heal the large items
     606            Mat healLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(25,25));
     607            Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_CLOSE, healLOR);
     608
     609            //IsolateLarge
     610            Mat isolateLargeLOR = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(30,30));
     611            Imgproc.morphologyEx(maskLOR,maskLOR, Imgproc.MORPH_OPEN, isolateLargeLOR);
     612
     613            Core.bitwise_not(maskLOR,maskLOR);
     614            srcLOR.copyTo(dstLOR, maskLOR);
     615
     616            Mat base = binarizedOriginal.clone();
     617            //***********************************
     618            //3. Isolate straight lines
     619            //***********************************
     620            //Heal lines
     621            Mat healISL = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(82,1));
     622            Imgproc.morphologyEx(dstLOR,base, Imgproc.MORPH_OPEN, healISL);
     623
     624            //Make White Blobs
     625            Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,1));
     626            Imgproc.morphologyEx(base,base,Imgproc.MORPH_OPEN, kernelOpen);
     627
     628            Mat kernelDilateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
     629            Imgproc.dilate(base, base, kernelDilateAgain);
     630
     631            Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(12,50));
     632            Imgproc.morphologyEx(base,base,Imgproc.MORPH_CLOSE, kernelCloseAgain);
     633
     634            //***********************************
     635            //4. Find 'Clusters'
     636            //   Need to find areas, using bounding boxes
     637            //   Find how tall the bounding box is, if its taller than 'x' then classify as Sheet Music
     638            //***********************************
     639
     640            //*****************************************************************
     641            //4.1 Prep Rectangles on processed images
     642            //*****************************************************************
     643            ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     644            Mat hierarchy = new Mat();
     645            Imgproc.findContours(base, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     646            //Draw contours and record areas
     647            Mat allContoursFound = Mat.zeros(base.size(), CvType.CV_8UC3);
     648            Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
     649            //Init arrays
     650            MatOfPoint2f[] contoursPoly  = new MatOfPoint2f[contours.size()];
     651            Rect[] boundRect = new Rect[contours.size()];
     652
     653            //Fill arrays
     654            for (int i = 0; i < contours.size(); i++) {
     655                contoursPoly[i] = new MatOfPoint2f();
     656                Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 1, true);
     657                boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
     658            }
     659            //Draw circle for each large contour
     660            //MAKE THE COMPARING VALUES GLOBAL CONSTANTS
     661            for (int i = 0; i < contours.size(); i++) {
     662                if(boundRect[i].height > 100){
     663                    if(boundRect[i].width > 300){
     664                        areaCounter++;
     665                    }
     666                }
     667            }
     668
    575669            //Classifier Calculation
    576670            if(areaCounter >= THRESHOLD_AREA_COUNT){
Note: See TracChangeset for help on using the changeset viewer.