Ignore:
Timestamp:
2019-06-20T20:42:56+12:00 (5 years ago)
Author:
cpb16
Message:

refined houghlineP alogirthm

File:
1 edited

Legend:

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

    r33141 r33170  
    3333    public static void main(String[] args) {   
    3434        try {
    35             if (args.length != 2) {
    36             System.out.println("Usage: imageClassifier <inputFilename> <classifierType>");
     35            if (args.length != 3) {
     36            System.out.println("Usage: imageClassifier <inputFilename> <classifierType> <outputFilename>");
    3737            }
    3838            else {
    3939                Boolean result = null;
    4040                String imageFilename = args[0];
    41                 String classifierType = args[1];           
     41                String classifierType = args[1];
     42                String outputFilename = args[2];           
    4243                //Execute classifierType defined from arguement
    4344                switch(classifierType){
    4445                case "houghlinesP":
    45                     result = setup_HoughLineP(imageFilename); //true or false
     46                    result = setup_HoughLinesP(imageFilename); //true or false
     47                    break;
     48                case "houghlinesP-refined":
     49                    result = setup_HoughLinesP_refined(imageFilename);
    4650                    break;
    4751                default:
    48                     System.out.println("unknown");
     52                    System.out.println("unknown algorithm");
    4953                    break;
    5054                }           
    5155                //Write output to disc
    52                 File log = new File("log.txt");
     56                File log = new File(outputFilename);
    5357                FileWriter fileWriter = new FileWriter(log, true);
    5458                BufferedWriter bw = new BufferedWriter(fileWriter);
     
    6670    //True = 1 + Filename + Status
    6771    //False= 0 + Filename + Status
    68     private static Boolean setup_HoughLineP(String filename){
     72   
     73 //******************
     74 //CLASSIFIER FUNCTIONS
     75 //******************
     76    private static Boolean setup_HoughLinesP(String filename){
    6977    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    7078    Boolean isSheetMusic = null;
     
    8593        // Draw the lines
    8694        for (int x = 0; x < linesP.rows(); x++) {
    87         double[] l = linesP.get(x, 0);
    88         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);
     95            double[] l = linesP.get(x, 0);
     96            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);
    8997        }
    9098       
     
    92100        BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
    93101        //Calculate if its sheet music or not
    94         isSheetMusic = classifier_HoughLineP(toBeClassifiedImg); 
    95                
    96         //Save Processed Image
    97         String processedFile = filename;
    98         if (isSheetMusic == true) {
    99         processedFile = "proc_T_"+filename;
    100         }else {
    101         processedFile = "proc_F_"+filename;
    102         }
    103         imwrite(processedFile, edgesDetectedRGB);                   
     102        isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg);               
    104103    }
    105104    catch(Exception e){
     
    107106        }
    108107        return isSheetMusic;
    109     /*   
    110     if (isSheetMusic == true){   
    111         return (1 + "\t" + "Filename: " + filename + "  Status: " + isSheetMusic  +"\t" );
    112     }   
    113     else{
    114         return (0 + "\t" + "Filename: " + filename + "  Status: " + isSheetMusic  +"\t" );
    115     } */
    116108 }
    117109 
     110     private static Boolean setup_HoughLinesP_refined(String filename){
     111    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     112    Boolean isSheetMusic = null;
     113    try{
     114        //Variables
     115        Mat edgesDetected = new Mat();
     116        Mat edgesDetectedRGB = new Mat();
     117        Mat edgesDetectedRGBProb;
     118        // Load an image
     119        Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
     120        // Edge detection
     121        Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
     122        //Copy edges to the images that will display the results in BGR
     123        Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
     124        // Probabilistic Line Transform
     125        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        // Draw the lines
     129       
     130        for (int x = 0; x < linesP.rows(); x++) {
     131            double[] l = linesP.get(x, 0);
     132            //New angles
     133            Point p1 = new Point(l[0], l[1]);
     134            Point p2 = new Point(l[2], l[3]);
     135            double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
     136            //System.out.println(l[0]);
     137            //System.out.println(l[1]);
     138            //System.out.println(l[2]);
     139            //System.out.println(l[3]);
     140            if(m<0.1) {
     141                //System.out.println("m: " + m);
     142                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);
     143            }
     144        }
     145       
     146        //Convert MAT into a BufferedImage
     147        BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
     148        //Calculate if its sheet music or not
     149        isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg); 
     150           
     151    }
     152    catch(Exception e){
     153            System.err.println(e);
     154        }
     155        return isSheetMusic;
     156 }
     157 
    118158 //******************
    119159 //INTERNAL FUNCTIONS
    120160 //******************
    121     private static boolean classifier_HoughLineP(BufferedImage img){
     161    private static boolean classifier_HoughLinesP(BufferedImage img){
    122162    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    123163    try {
     
    143183        percentage = ((float)redCount/(float)pixelCount)*(float)100;
    144184        //If more than %10 and less than %50 then its sheet music!
    145         if(percentage > 10 && percentage < 50){
     185        if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){
    146186            return true;}
    147187        }
Note: See TracChangeset for help on using the changeset viewer.