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-terminal/javaImageClassifier.java

    r33221 r33243  
    1212import javax.imageio.ImageIO;
    1313import java.util.logging.Logger;
     14import java.util.ArrayList;
    1415
    1516//REFERENCES:
     
    2829public class javaImageClassifier{
    2930    //Constants
    30     static int CLASSIFIER_HOUGHLINESP_MIN = 5;
    31     static int CLASSIFIER_HOUGHLINESP_MAX = 40;
     31
     32    static int CLASSIFIER_HOUGHLINESP_MIN   = 10;
     33    static int CLASSIFIER_HOUGHLINESP_MAX   = 65;
     34    static int HOUGHLINEP_THRESHOLD         = 10;
     35    static int MINLINECOUNT                 = 40; //50
     36    static double MAXLINEGAP                = 4;
     37    static double SLOPEGRADIENT             = 0.02; //0.01
    3238
    3339    public static void main(String[] args) {   
     
    3743            }
    3844            else {
     45                ArrayList result_refined = null;
    3946                Boolean result = null;
    4047                String imageFilename = args[0];
    4148                String classifierType = args[1];
    42                 String outputFilename = args[2];           
     49                String outputFilename = args[2];
     50                //Prep Writing output to disc
     51                File log = new File(outputFilename);
     52                FileWriter fileWriter = new FileWriter(log, true);
     53                BufferedWriter bw = new BufferedWriter(fileWriter);         
    4354                //Execute classifierType defined from arguement
     55               
     56                //Split output by tab for processing in next java program
     57                //imageFilename = 1, result = 3, classifierType = 4
    4458                switch(classifierType){
    4559                case "houghlinesP":
    4660                    result = setup_HoughLinesP(imageFilename); //true or false
     61                    bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result + '\t' + classifierType + '\n');
    4762                    break;
    4863                case "houghlinesP-refined":
    49                     result = setup_HoughLinesP_refined(imageFilename);
     64                    result_refined = setup_HoughLinesP_refined(imageFilename);
     65                    bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result_refined.get(0) + '\t' + "Number of lines:" + '\t' + result_refined.get(1) + '\t' + classifierType + '\n');
    5066                    break;
    5167                default:
     
    5369                    break;
    5470                }           
    55                 //Write output to disc
    56                 File log = new File(outputFilename);
    57                 FileWriter fileWriter = new FileWriter(log, true);
    58                 BufferedWriter bw = new BufferedWriter(fileWriter);
    59                 //Split output by tab for processing in next java program
    60                 //imageFilename = 1, result = 3, classifierType = 4
    61                 bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result + '\t' + classifierType + '\n');
     71
    6272                bw.close();             
    6373            }
     
    8191        Mat edgesDetected = new Mat();
    8292        Mat edgesDetectedRGB = new Mat();
     93        Mat edgesExtra = new Mat();
    8394        Mat edgesDetectedRGBProb;
    8495        // Load an image
     
    108119 }
    109120 
    110      private static Boolean setup_HoughLinesP_refined(String filename){
     121     private static ArrayList setup_HoughLinesP_refined(String filename){
    111122    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    112123    Boolean isSheetMusic = null;
     124    ArrayList returnArray = new ArrayList();
    113125    try{
     126       
    114127        //Variables
     128        int horizontalLineCount =0;
    115129        Mat edgesDetected = new Mat();
    116130        Mat edgesDetectedRGB = new Mat();
     131        Mat edgesExtra = new Mat();
    117132        Mat edgesDetectedRGBProb;
    118133        // Load an image
    119134        Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
    120135        // Edge detection
    121         Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
     136        //Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
     137        Imgproc.adaptiveThreshold(original, edgesDetected,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV,15, 2);
     138        //Imgproc.medianBlur(edgesExtra, edgesDetected, 3);
     139       
     140       
    122141        //Copy edges to the images that will display the results in BGR
    123142        Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
    124143        // Probabilistic Line Transform
    125144        Mat linesP = new Mat(); // will hold the results of the detection
    126         double minLineLength = edgesDetectedRGB.size().width/4;
    127         //Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, minLineLength, 10);// runs the actual detection
    128             Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 10, minLineLength, 5);// remote testing
     145        double minLineLength = edgesDetectedRGB.size().width/8;
     146        //Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 10, minLineLength, MAXLINEGAP);// runs the actual detection
     147        Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 720, HOUGHLINEP_THRESHOLD, minLineLength, MAXLINEGAP); //TESTING
    129148        // Draw the lines
    130149       
     
    139158            //System.out.println(l[2]);
    140159            //System.out.println(l[3]);
    141             if(m<0.1) {
     160            if(m<SLOPEGRADIENT) {
    142161                //System.out.println("m: " + m);
    143                 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);
     162                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);
     163                horizontalLineCount++;
    144164            }
    145165        }
     
    148168        BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
    149169        //Calculate if its sheet music or not
    150         isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg); 
    151 
     170        isSheetMusic = classifier_HoughLinesP_refined(horizontalLineCount); 
     171        returnArray.add(isSheetMusic);
     172        returnArray.add(horizontalLineCount);
    152173           
    153174    }
     
    155176            System.err.println(e);
    156177        }
    157         return isSheetMusic;
     178        return returnArray;
    158179 }
    159180 
     
    193214        return false;
    194215    }
     216   
     217    private static boolean classifier_HoughLinesP_refined(int lineCount){
     218        if(lineCount>MINLINECOUNT){
     219            return true;
     220        }
     221        else{
     222            return false;
     223        }
     224    }
     225   
    195226        private static  BufferedImage toBufferedImage(Mat mat){
    196227        //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Note: See TracChangeset for help on using the changeset viewer.