Ignore:
Timestamp:
2019-08-26T16:44:52+12:00 (5 years ago)
Author:
cpb16
Message:

made progress with morphology. Need to have a better area dimension threshold setup

File:
1 edited

Legend:

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

    r33415 r33437  
    3434//False =classifierType + 0 + Filename + Status
    3535public class javaClassifierComparison {
     36
     37    //MAKE THIS A PROPERTIES FILE. THIS WILL BE SET AND CHECKED FROM OUTSIDE THE LOOP
    3638    //GLOBALS Constants
    3739    static int CLASSIFIER_HOUGHLINESP_MIN = 10;
     
    4143    static int MINLINECOUNT = 40;
    4244    static int MAXLINEGAP = 1;
    43     static double THRESHOLD_C               = 4;
     45    static double THRESHOLD_C = 4;
    4446    static double SLOPEGRADIENT = 0.02;
    4547    static double CLUSTER_DISTANCE_MAX = 40;
    4648    static double CLUSTER_DISTANCE_MIN = 2;
     49    //MORPHOLOGY
     50    static double THRESHOLD_AREA_SIZE  = 1000;
     51    static double THRESHOLD_AREA_COUNT = 4;
    4752
    4853
     
    113118                System.out.println("Usage: imageClassifier <inputFilename> <classifierType> <outputFilename>");
    114119            } else {
    115                 Pair houghlinesPResult = new Pair();
     120                Pair algorithmResult = new Pair();
    116121
    117122                Boolean result = null;
     
    132137                    case "count":
    133138                        enableLineClustering = false;
    134                         houghlinesPResult = Algorithm_HoughLinesP(imageFilename, enableLineClustering);
    135                         bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + houghlinesPResult.getBoolean() + '\t' + "Number of lines:" + '\t' + houghlinesPResult.getInteger() + '\t' + classifierType + '\n');
     139                        algorithmResult = Algorithm_HoughLinesP_Single(imageFilename, enableLineClustering);
     140                        bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + algorithmResult.getBoolean() + '\t' + "Number of lines:" + '\t' + algorithmResult.getInteger() + '\t' + classifierType + '\n');
    136141                        break;
    137142                    case "cluster":
    138143                        enableLineClustering = true;
    139                         houghlinesPResult = Algorithm_HoughLinesP(imageFilename, enableLineClustering);
    140                         bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + houghlinesPResult.getBoolean() + '\t' + "Number of lines:" + '\t' + houghlinesPResult.getInteger() + '\t' + classifierType + '\n');
     144                        algorithmResult = Algorithm_HoughLinesP_Single(imageFilename, enableLineClustering);
     145                        bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + algorithmResult.getBoolean() + '\t' + "Number of lines:" + '\t' + algorithmResult.getInteger() + '\t' + classifierType + '\n');
     146                        break;
     147                    case "combo":
     148                        algorithmResult = Algorithm_HoughLinesP_Combo(imageFilename);
     149                        bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + algorithmResult.getBoolean() + '\t' + "Number of lines:" + '\t' + algorithmResult.getInteger() + '\t' + classifierType + '\n');
    141150                        break;
    142151                    case "morphology":
    143                         //result_cluster = setup_Cluster(imageFilename);
    144                         //bw.write(result_cluster);
     152                        algorithmResult = Algorithm_Morphology(imageFilename);
     153                        bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + algorithmResult.getBoolean() + '\t' + "Number of areas:" + '\t' + algorithmResult.getInteger() + '\t' + classifierType + '\n');
    145154                        break;
    146155                    default:
     
    148157                        break;
    149158                }
    150 
    151159                bw.close();
    152160            }
     
    159167    //ALGORITHM FUNCTIONS
    160168    //******************
    161     private static Pair Algorithm_HoughLinesP(String filename, Boolean enableLineClusterDetection){
     169    private static Pair Algorithm_HoughLinesP_Single(String filename, Boolean enableLineClusterDetection){
    162170        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    163171        Boolean isSheetMusic = null;
     
    170178            Mat edgesExtra = new Mat();
    171179            Mat edgesDetectedRGBProb;
    172             ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();
     180            ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<StartAndEndPoint>();
    173181
    174182            //****************EXPLANATION**************************************************
     
    204212                returnVariables.setBoolean(isSheetMusic);
    205213                returnVariables.setInteger(horizontalLineCount);
     214            }
     215        }
     216        catch(Exception e){
     217            System.err.println(e);
     218        }
     219        return returnVariables;
     220    }
     221    private static Pair Algorithm_HoughLinesP_Combo(String filename){
     222        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     223        Boolean isSheetMusic = null;
     224        Pair returnVariables = new Pair();
     225        try{
     226            //Variables
     227            int horizontalLineCount =0;
     228            Mat edgesDetected = new Mat();
     229            Mat edgesDetectedRGB = new Mat();
     230            Mat edgesExtra = new Mat();
     231            Mat edgesDetectedRGBProb;
     232            ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<StartAndEndPoint>();
     233
     234            //****************EXPLANATION**************************************************
     235            //
     236            //Load an image in greyscale
     237            //Additional matrix to hold results of line detection
     238            //Inversed Binarization of image
     239            //Detect lines in image
     240            //Go thru every line detected and check its gradient is less than SLOPEGRADIENT
     241            //
     242            //****************EXPLANATION**************************************************
     243
     244            Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
     245            Mat linesP = new Mat(); //will hold the results of the detection
     246            Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, THRESHOLD_C);
     247            double minLineLength = edgesDetected.size().width/8;
     248            Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength, MAXLINEGAP);
     249            for (int x = 0; x < linesP.rows(); x++) {
     250                double[] l = linesP.get(x, 0);
     251                Point p1 = new Point(l[0], l[1]);
     252                Point p2 = new Point(l[2], l[3]);
     253                double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
     254                if(m<SLOPEGRADIENT) {
     255                    horizontalLineCount++;
     256                    pointArrayList.add(new StartAndEndPoint(p1, p2));
     257                }
     258            }
     259
     260            //Calculate if its sheet music or not
     261            isSheetMusic = Classifier_LineCounter(horizontalLineCount);
     262            if(isSheetMusic == true){
     263                returnVariables.setBoolean(isSheetMusic);
     264                returnVariables.setInteger(horizontalLineCount);
     265            }
     266            else if (isSheetMusic == false){
     267                returnVariables = Classifier_ClusterDetection(pointArrayList);
     268            }
     269        }
     270        catch(Exception e){
     271            System.err.println(e);
     272        }
     273        return returnVariables;
     274    }
     275    private static Pair Algorithm_Morphology(String filename){
     276        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     277        Boolean isSheetMusic = null;
     278        Pair returnVariables = new Pair();
     279        try{
     280            //Variables
     281            int areaCounter = 0;
     282            Mat edgesDetectedRGB = new Mat();
     283            Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
     284
     285            ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     286            Mat hierarchy = new Mat();
     287
     288            //Thresholds
     289            Imgproc.adaptiveThreshold(original, original,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
     290            Mat processed = original.clone();
     291            //Morphological Processing
     292            Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
     293            Imgproc.erode(processed,processed,kernelErode);
     294
     295            Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
     296            Imgproc.dilate(processed,processed,kernelDilate);
     297
     298            Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
     299            Imgproc.morphologyEx(processed, processed, Imgproc.MORPH_CLOSE, kernelOpening);
     300
     301            Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
     302            Imgproc.erode(processed,processed,kernelErode02);
     303
     304            //Detect contours
     305            Imgproc.findContours(processed, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
     306
     307            //Record areas
     308            for (int i = 0; i < contours.size(); i++) {
     309                double area = Imgproc.contourArea(contours.get(i));
     310                //Check if area detected meets threshold
     311                if(area > THRESHOLD_AREA_SIZE) {
     312                    areaCounter++;
     313                    //System.out.println("AREA: " + area);
     314                }
     315            }
     316            //Calculates if sheet music or not
     317            if(areaCounter >= THRESHOLD_AREA_COUNT){
     318                returnVariables.setBoolean(true);
     319                returnVariables.setInteger(areaCounter);
    206320            }
    207321        }
Note: See TracChangeset for help on using the changeset viewer.