Changeset 33070


Ignore:
Timestamp:
05/13/19 20:18:52 (22 months ago)
Author:
cpb16
Message:

Completed Straight Line Finding Code

Location:
other-projects/is-sheet-music-encore/trunk/image-identification
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • other-projects/is-sheet-music-encore/trunk/image-identification/.idea/workspace.xml

    r33069 r33070  
    22<project version="4">
    33  <component name="ChangeListManager">
    4     <list default="true" readonly="true" id="9bad4f92-4a7d-46a2-b6f0-94214c9f55cc" name="Default" comment="" />
     4    <list default="true" readonly="true" id="9bad4f92-4a7d-46a2-b6f0-94214c9f55cc" name="Default" comment="">
     5      <change beforePath="$PROJECT_DIR$/src/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
     6    </list>
    57    <ignored path="$PROJECT_DIR$/out/" />
    68    <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
     
    1618        <entry file="file://$PROJECT_DIR$/src/Main.java">
    1719          <provider selected="true" editor-type-id="text-editor">
    18             <state relative-caret-position="566">
    19               <caret line="94" column="73" selection-start-line="94" selection-start-column="73" selection-end-line="94" selection-end-column="73" />
     20            <state relative-caret-position="107">
     21              <caret line="19" lean-forward="true" selection-start-line="19" selection-end-line="19" />
    2022              <folding>
    2123                <element signature="imports" expanded="true" />
     
    8486    <configuration>$USER_HOME$/.subversion</configuration>
    8587    <supportedVersion>125</supportedVersion>
     88  </component>
     89  <component name="SvnFileUrlMappingImpl">
     90    <option name="myMappingRoots">
     91      <list>
     92        <SvnCopyRootSimple>
     93          <option name="myVcsRoot" value="$PROJECT_DIR$" />
     94          <option name="myCopyRoot" value="$PROJECT_DIR$" />
     95        </SvnCopyRootSimple>
     96        <SvnCopyRootSimple>
     97          <option name="myVcsRoot" value="$PROJECT_DIR$/.idea" />
     98          <option name="myCopyRoot" value="$PROJECT_DIR$/.idea" />
     99        </SvnCopyRootSimple>
     100      </list>
     101    </option>
     102    <option name="myMoreRealMappingRoots">
     103      <list>
     104        <SvnCopyRootSimple>
     105          <option name="myVcsRoot" value="$PROJECT_DIR$" />
     106          <option name="myCopyRoot" value="$PROJECT_DIR$" />
     107        </SvnCopyRootSimple>
     108      </list>
     109    </option>
    86110  </component>
    87111  <component name="TaskManager">
     
    116140      <window_info id="UI Designer" order="1" />
    117141      <window_info anchor="right" id="Theme Preview" order="2" />
    118       <window_info anchor="bottom" id="Debug" order="7" weight="0.4" />
     142      <window_info anchor="bottom" id="Debug" order="7" weight="0.3997955" />
    119143      <window_info id="Favorites" order="3" side_tool="true" />
    120144      <window_info anchor="right" content_ui="combo" id="Hierarchy" order="7" weight="0.25" />
     
    132156      <window_info anchor="bottom" id="Run" order="6" visible="true" weight="0.34662578" />
    133157      <window_info anchor="right" id="Theme Preview" order="2" />
    134       <window_info anchor="bottom" id="Cvs" order="8" weight="0.25" />
    135158      <window_info content_ui="combo" id="Project" order="5" visible="true" weight="0.18382749" />
    136159      <window_info id="Structure" order="6" side_tool="true" weight="0.25" />
     
    138161      <window_info anchor="right" id="Ant Build" order="6" weight="0.25" />
    139162      <window_info anchor="bottom" id="TODO" order="10" />
     163      <window_info anchor="bottom" id="Version Control" order="2" />
    140164      <window_info id="Image Layers" order="4" />
    141165      <window_info id="Designer" order="0" />
     
    147171      <window_info anchor="bottom" id="Event Log" order="1" side_tool="true" />
    148172      <window_info anchor="bottom" id="Inspection" order="9" weight="0.4" />
    149       <window_info anchor="bottom" id="Version Control" order="2" />
     173      <window_info anchor="bottom" id="Cvs" order="8" weight="0.25" />
    150174      <window_info id="Capture Tool" order="2" />
    151175      <window_info anchor="bottom" id="Message" order="4" />
     
    166190    <entry file="file://$PROJECT_DIR$/src/Main.java">
    167191      <provider selected="true" editor-type-id="text-editor">
     192        <state relative-caret-position="1410">
     193          <caret line="94" column="73" selection-start-line="94" selection-start-column="73" selection-end-line="94" selection-end-column="73" />
     194          <folding>
     195            <element signature="imports" expanded="true" />
     196          </folding>
     197        </state>
     198      </provider>
     199    </entry>
     200    <entry file="file://$PROJECT_DIR$/src/Main.java">
     201      <provider selected="true" editor-type-id="text-editor">
    168202        <state relative-caret-position="495">
    169203          <caret line="33" column="19" selection-start-line="33" selection-start-column="19" selection-end-line="33" selection-end-column="31" />
     
    203237    <entry file="file://$PROJECT_DIR$/src/Main.java">
    204238      <provider selected="true" editor-type-id="text-editor">
    205         <state relative-caret-position="566">
    206           <caret line="94" column="73" selection-start-line="94" selection-start-column="73" selection-end-line="94" selection-end-column="73" />
     239        <state relative-caret-position="107">
     240          <caret line="19" lean-forward="true" selection-start-line="19" selection-end-line="19" />
    207241          <folding>
    208242            <element signature="imports" expanded="true" />
  • 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.