Ignore:
Timestamp:
2019-06-27T15:19:39+12:00 (5 years ago)
Author:
cpb16
Message:

Had break through with the refined houghlinesP algorithm overall accurarcy rate of 93%

File:
1 edited

Legend:

Unmodified
Added
Removed
  • other-projects/is-sheet-music-encore/trunk/image-identification-development/src/Main.java

    r33170 r33243  
    44import org.opencv.imgcodecs.Imgcodecs;
    55import org.opencv.imgproc.Imgproc;
     6import org.opencv.photo.Photo;
    67import static org.opencv.imgcodecs.Imgcodecs.imwrite;
    78import java.awt.image.BufferedImage;
    89import java.awt.image.DataBufferByte;
    910import java.io.File;
     11import java.util.ArrayList;
    1012import javax.imageio.ImageIO;
    1113
     
    3739
    3840    //GLOBAL_CONSTANTS
     41    static int CLASSIFIER_HOUGHLINESP_MIN   = 10;
     42    static int CLASSIFIER_HOUGHLINESP_MAX   = 65;
     43    static int HOUGHLINEP_THRESHOLD         = 10;
     44    static int MINLINECOUNT                 = 40;
     45    static double MAXLINEGAP                = 4;
     46    static double SLOPEGRADIENT             = 0.02;
    3947    //SHOULD TURN INTO ARGS
    4048
     
    5967        return null;
    6068    }
    61 
    62     private static boolean Classifier(BufferedImage img){
     69    private static boolean ClassifierPixelCount(BufferedImage img){
    6370        try {
    6471            //Read file
     
    8592            percentage = ((float)redCount/(float)pixelCount)*(float)100;
    8693            //If more than %10 and less than %50 then its sheet music!
    87             if(percentage > 10 && percentage < 50){ //MAKE THESE CONSTANTS!!
     94            if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){ //MAKE THESE CONSTANTS!!
    8895                return true;}
    8996        }
     
    93100        return false;
    94101    }
     102    private static boolean Classifier(int lineCount){
     103
     104           if(lineCount>MINLINECOUNT){
     105                return true;
     106            }
     107            else{
     108                return false;
     109            }
     110    }
     111
    95112
    96113    public static void main(String[] args) {
    97114        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    98115        try {
     116            //temp array for terminalversion
     117
     118            ArrayList returnArray = new ArrayList();
     119            returnArray.add(true);
     120            returnArray.add(10);
     121
     122
    99123            //Variables
    100124            Mat edgesDetected = new Mat();
    101125            Mat edgesDetectedRGB = new Mat();
     126            Mat edgesExtra = new Mat();
    102127            Mat edgesDetectedRGBProb;
    103128            Mat edgeDoesntMakeSense;
    104129            Mat justEdges; //TESTING
     130
     131            String directory = "/Scratch/cpb16/is-sheet-music-encore/download-images/MU/";
     132            //!!!!!!!!!!!!!!!!!!!!!!!!!!!NOT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     133            //mdp.39015097852365-2.png 176 lines    Contents page.
     134            //mdp.39015097852555-3.png 76  lines
     135            String default_file = directory+"SheetMusic/mdp.39015080972303-3.png";
     136            //String default_file ="TestImages/NotNot/mdp.39015080972303-3.png";
     137
     138
     139            //System.out.println(default_file);
    105140            //String default_file = "TestImages/NotSheetMusic01.png";
    106             String default_file = "TestImages/NotSheetMusic02.png";
     141            //String default_file = "TestImages/NotSheetMusic02.png";
    107142            //String default_file = "TestImages/SheetMusic01.png";
    108143            //String default_file = "TestImages/SheetMusic02.png";
    109144            //String default_file = "TestImages/vLine.png";
    110145            String filename = ((args.length > 0) ? args[0] : default_file);
     146            File file = new File(filename);
     147            if(!file.exists()){System.err.println("Image not found: "+ filename);}
     148
     149            int horizontalLineCount =0;
    111150
    112151            // Load an image
     
    114153            // Edge detection
    115154            //01 CANNY
    116             Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
     155            //Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
     156            //Imgproc.Canny(original, edgesDetected,0, 100, 3, false );
    117157            //Imgproc.Canny(original, edgesDetected,80, 120);
    118158            //02 BINARYINV
    119             //Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2);
     159            Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2);
     160
     161            //Imgproc.adaptiveThreshold(original, edgesExtra,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2);
     162            //Imgproc.medianBlur(edgesExtra, edgesDetected, 3);
    120163            //03 BINARY
    121164            //Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY,15, 2);
     
    127170
    128171
     172
     173
    129174            //Convert to RGB for future use
    130175            Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
     
    136181            //(edgeDetectedImage, outputOfDetection(r,Ξ), resolution of rho, resolution of theta, threshold (minimum num of intersections)
    137182
    138             double minLineLength = edgesDetectedRGB.size().width/4;
    139             Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, minLineLength, 10); // runs the actual detection
    140 
    141 
     183            double minLineLength = edgesDetectedRGB.size().width/8;
     184
     185            Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength,MAXLINEGAP); // runs the actual detection
     186            //Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, HOUGHLINEP_THRESHOLD, minLineLength,MAXLINEGAP); // runs the actual detection
     187            System.out.println("Before Graident Filtering num lines: " + linesP.rows());
    142188
    143189            //Imgproc.HoughLinesP(edgesDetected,linesP,1,Math.PI/2, 50, 80, 5);
     
    170216
    171217
    172 
    173 
    174 
    175 
    176                 if(m<=0.1) {
     218                if(m<=SLOPEGRADIENT) {
    177219                    //System.out.println("m: " + m);
    178                     Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
     220                    Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 1, Imgproc.LINE_AA, 0);
     221                    horizontalLineCount++;
    179222                }
    180223
     
    189232
    190233            BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
    191             System.out.println("Result: " + Classifier(toBeClassifiedImg));
    192 
     234            System.out.println("Result: " + Classifier(horizontalLineCount));
     235
     236
     237            System.out.println();
    193238            //Display Results
    194             //HighGui.imshow("Source", original);
     239            HighGui.imshow("Source", original);
    195240            //HighGui.imshow("Just Edges", justEdges); //TESTING
    196241            HighGui.imshow("Detected Lines (in red) - positive", edgesDetectedRGB);
Note: See TracChangeset for help on using the changeset viewer.