Ignore:
Timestamp:
2019-05-13T20:18:52+12:00 (5 years ago)
Author:
cpb16
Message:

Completed Straight Line Finding Code

File:
1 edited

Legend:

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

    r33069 r33070  
    55import org.opencv.imgproc.Imgproc;
    66import static org.opencv.imgcodecs.Imgcodecs.imwrite;
    7 
     7import java.awt.image.BufferedImage;
     8import java.awt.image.DataBufferByte;
     9import java.io.File;
     10import javax.imageio.ImageIO;
    811
    912//REFERENCES:
    10 //https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.html
     13//https://docs.opencv.org/3.4.3/d9/db0/tutorial_hough_lines.
     14//https://stackoverflow.com/questions/43443309/count-red-pixel-in-a-given-image
     15//https://www.wikihow.com/Calculate-Percentage-in-Java
     16//https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
    1117
    1218
    1319public class Main {
    1420
    15     private Scalar meanOfProcessed;
     21    private static  BufferedImage toBufferedImage(Mat mat){
     22        //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     23        //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     24        //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
     25        try{
     26            int type = BufferedImage.TYPE_3BYTE_BGR;
     27            int bufferSize = mat.channels() * mat.cols() * mat.rows();
     28            byte[] b = new byte[bufferSize];
     29            //get all the pixels
     30            mat.get(0, 0, b);
     31            BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
     32            final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
     33            System.arraycopy(b, 0, targetPixels, 0, b.length);
     34            return image;
     35        }
     36        catch(Exception e){
     37            System.err.println(e);
     38        }
     39        return null;
     40    }
     41
     42    private static boolean Classifier(BufferedImage img){
     43        try {
     44            //Read file
     45            //BufferedImage img = ImageIO.read(new File(processedFile));
     46            int x = img.getWidth();
     47            int y = img.getHeight();
     48            int pixelCount  = 0;
     49            int redCount    = 0;
     50            float percentage = 0;
     51
     52            //Go Thru every pixel
     53            for(int i=0; i < y; i++){
     54                for(int j=0;j < x; j++){
     55                    //Get value for current pixels RGB value
     56                    int currPixelRGB = img.getRGB(j, i);
     57                    //Check if pixel is red (hex value of red)
     58                    if(currPixelRGB == 0xFFFF0000){
     59                        redCount++;
     60                    }
     61                    pixelCount++;
     62                }
     63            }
     64            //Calculate percentage of Red in image
     65            percentage = ((float)redCount/(float)pixelCount)*(float)100;
     66            //If more than %10 and less than %50 then its sheet music!
     67            if(percentage > 10 && percentage < 50){
     68                return true;}
     69        }
     70        catch (Exception e) {
     71            System.err.println(e);
     72        }
     73        return false;
     74    }
    1675
    1776    public static void main(String[] args) {
    1877        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    19 /*
    20         // write your code here
    21         System.out.println("Welcome to OpenCV " + Core.VERSION);
    22         Mat m = new Mat(5, 10, CvType.CV_8UC1, new Scalar(0));
    23         System.out.println("OpenCV Mat: " + m);
    24         Mat mr1 = m.row(1);
    25         mr1.setTo(new Scalar(1));
    26         Mat mc5 = m.col(0);
    27         mc5.setTo(new Scalar(8));
    28         System.out.println("OpenCV Mat data:\n" + m.dump());
    29  */
    30     try {
     78        try {
    3179            //Variables
    3280            Mat edgesDetected = new Mat();
    3381            Mat edgesDetectedRGB = new Mat();
    3482            Mat edgesDetectedRGBProb;
    35             Mat justEdges;
    36 
    37 
    38 
     83            Mat justEdges; //TESTING
    3984            String default_file = "Test.png";
    4085            String filename = ((args.length > 0) ? args[0] : default_file);
     
    4893            edgesDetectedRGBProb = edgesDetectedRGB.clone();
    4994
    50             justEdges = edgesDetectedRGBProb.clone();
    51 
    52 
     95            justEdges = edgesDetectedRGBProb.clone();//TESTING
    5396
    5497            // Standard Hough Line Transform
     
    66109            }
    67110
    68 
    69 
    70111            // Probabilistic Line Transform
    71112            Mat linesP = new Mat(); // will hold the results of the detection
     
    77118            }
    78119
    79             //save output
    80             imwrite(filename+"_HoughLineP", edgesDetectedRGBProb);
    81             String processedFile = filename+"_HoughLineP";
    82             //Load an image
    83             Mat processed = Imgcodecs.imread(processedFile, Imgcodecs.IMREAD_COLOR);
    84             System.out.println(Core.mean(processed));
    85             String meanOfProcessed = (Core.mean(processed).toString());
    86             //OUTPUTS [ BLUE , GREEN , RED, ALPHA?
    87             //Core.mean GETS scalar of image (array of BGR) and calculates the average for each
    88             //https://docs.opencv.org/java/2.4.2/org/opencv/core/Core.html#mean(org.opencv.core.Mat)
     120            //Convert MAT into a BufferedImage
     121            BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGBProb);
    89122
    90             //With this logic, then if can say if the RED in the array is between 'x' and 'y' then its sheet music
    91             System.out.println(meanOfProcessed); //PRINTS AS EXPECTED
     123            //Calculate if its sheet music or not
     124            Boolean isSheetMusic = Classifier(toBeClassifiedImg);
     125            System.out.println("Filename: " + filename + "  Status: " + isSheetMusic);
    92126
    93             //CONVERT STRING TO ARRAY OR FIND ANOTHER WAY OF GETTING TO RED VALUE
    94             //SAVE AS VARIABLE : RedValue.
    95             //IF redValue > 'x' and redValue < 'y' THEN it is sheet music
     127            //Save Processed Image
     128            String processedFile = null;
     129            if (isSheetMusic == true) {
     130                //NEED FIGURE OUT HOW RUN IN SCRIPT. THEN USE ARGS as filename + "HoughLineP + "png"
     131                processedFile = "SheetMusic/Test_HoughLineP.png";
     132            }else {
     133                processedFile = "Test_HoughLineP.png";
     134            }
     135            imwrite(processedFile, edgesDetectedRGBProb);
    96136
     137            //Display Results
     138            HighGui.imshow("Source", original);
     139            HighGui.imshow("Just Edges", justEdges); //TESTING
     140            HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB);
     141            HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb);
    97142
    98 
    99             // Show results
    100             //HighGui.imshow("Source", original);
    101             //HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB);
    102             HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb);
    103             //HighGui.imshow("Just Edges", justEdges);
    104143            // Wait and Exit
    105144            HighGui.waitKey();
     
    107146        }
    108147        catch(Exception e){
    109          System.err.println(e);
     148            System.err.println(e);
    110149        }
    111150    }
Note: See TracChangeset for help on using the changeset viewer.