Changeset 33447


Ignore:
Timestamp:
2019-08-30T18:03:01+12:00 (5 years ago)
Author:
cpb16
Message:

starting to implement terminal version of new morphology. need to fix. return bariables always returning null

Location:
other-projects/is-sheet-music-encore/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • other-projects/is-sheet-music-encore/trunk/Makefile

    r33439 r33447  
    2727   
    2828   
    29 run-morphology-100-test:
    30     cd image-identification-terminal/log-all-hires-new; rm log-morphology-MU-test.txt
    31     cd image-identification-terminal; javac javaAccuracyCalculator.java
    32     cd image-identification-terminal; javac -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar javaClassifierComparison.java;
    33     cd image-identification-terminal; ./runClassifer.sh /Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU morphology log-all-hires-new/log-morphology-MU-test.txt -testNew
    34     cd image-identification-terminal; java javaAccuracyCalculator log-all-hires-new/log-morphology-MU-test.txt morphology-MU log-all-hires-new/results.txt
    35     cd image-identification-terminal; less log-all-hires-new/results.txt
    36     cd image-identification-terminal; less log-all-hires-new/log-morphology-MU-test.txt
     29run-morphology-20-test:
     30    #cd image-identification-terminal/log-all-hires-new/test; rm log-morphology-MU-test.txt
     31    cd image-identification-terminal; javac javaAccuracyCalculator.java
     32    cd image-identification-terminal; javac -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar javaClassifierComparison.java;
     33    cd image-identification-terminal; ./runClassifer.sh /Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU morphology log-all-hires-new/test/log-morphology-MU-test.txt -testNew
     34    cd image-identification-terminal; date >> log-all-hires-new/test/results.txt
     35    cd image-identification-terminal; cat configClassifierComparison.properties >> log-all-hires-new/test/results.txt
     36    cd image-identification-terminal; java javaAccuracyCalculator log-all-hires-new/test/log-morphology-MU-test.txt morphology-MU log-all-hires-new/test/results.txt
     37    cd image-identification-terminal; less log-all-hires-new/test/results.txt
     38    cd image-identification-terminal; less log-all-hires-new/test/log-morphology-MU-test.txt
    3739
    3840run-morph-single:
    3941    cd image-identification-terminal; make testrun-classifier-single-morph
     42run-count-single:
     43    cd image-identification-terminal; make testrun-classifier-single-count
     44run-cluster-single:
     45    cd image-identification-terminal; make testrun-classifier-single-cluster
     46run-combo-single:
     47    cd image-identification-terminal; make testrun-classifier-single-combo
    4048#**********
    4149#HI-RES
  • other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/.idea/workspace.xml

    r33444 r33447  
    1515        <entry file="file://$PROJECT_DIR$/image-identification-development/src/Main.java">
    1616          <provider selected="true" editor-type-id="text-editor">
    17             <state relative-caret-position="3154">
    18               <caret line="820" column="49" selection-start-line="820" selection-start-column="49" selection-end-line="820" selection-end-column="49" />
     17            <state relative-caret-position="-969">
     18              <caret line="803" selection-start-line="803" selection-end-line="803" />
    1919              <folding>
    2020                <element signature="imports" expanded="true" />
     
    4040        <entry file="file://$PROJECT_DIR$/image-identification-development/src/MainMorph.java">
    4141          <provider selected="true" editor-type-id="text-editor">
    42             <state relative-caret-position="221">
    43               <caret line="182" column="12" selection-start-line="182" selection-start-column="12" selection-end-line="182" selection-end-column="12" />
     42            <state relative-caret-position="321">
     43              <caret line="855" column="42" selection-start-line="855" selection-start-column="42" selection-end-line="855" selection-end-column="42" />
    4444              <folding>
    4545                <element signature="imports" expanded="true" />
    46                 <element signature="e#7379#7795#1" />
    47                 <element signature="e#7580#7900#1" />
    48                 <element signature="e#11581#14087#1" />
    49                 <element signature="e#14171#16210#1" />
    50                 <element signature="e#16278#18925#1" />
    51                 <element signature="e#19007#24734#1" />
    52                 <element signature="e#24817#28826#1" />
    53                 <element signature="e#28892#30082#1" />
    54                 <element signature="e#30149#34144#1" />
    55                 <element signature="e#34814#34826#0" expanded="true" />
     46                <element signature="e#10614#13084#1" />
     47                <element signature="e#13169#15172#1" />
     48                <element signature="e#15241#17864#1" />
     49                <element signature="e#17947#23618#1" />
     50                <element signature="e#23702#27679#1" />
     51                <element signature="e#27746#28924#1" />
     52                <element signature="e#28992#32955#1" />
     53                <element signature="e#35980#35992#0" expanded="true" />
     54                <element signature="e#37714#37726#0" expanded="true" />
     55                <element signature="e#37797#37809#0" expanded="true" />
     56                <element signature="e#41273#41282#0" expanded="true" />
     57                <element signature="e#44803#56145#1" />
     58                <element signature="e#48966#48978#0" expanded="true" />
     59                <element signature="e#49049#49061#0" expanded="true" />
     60                <element signature="e#49131#49143#0" expanded="true" />
     61                <element signature="e#52896#52905#0" expanded="true" />
    5662              </folding>
    5763            </state>
     
    6268        <entry file="file://$PROJECT_DIR$/../image-identification-terminal/javaClassifierComparison.java">
    6369          <provider selected="true" editor-type-id="text-editor">
    64             <state relative-caret-position="4598">
    65               <caret line="356" selection-start-line="356" selection-end-line="356" />
     70            <state relative-caret-position="406">
     71              <caret line="606" column="5" lean-forward="true" selection-start-line="606" selection-start-column="5" selection-end-line="606" selection-end-column="5" />
    6672              <folding>
    6773                <element signature="imports" expanded="true" />
     74                <element signature="method#init#0;class#javaClassifierComparison#0" />
     75                <element signature="e#3021#4256#0" />
     76                <element signature="class#StartAndEndPoint#0;class#javaClassifierComparison#0" />
     77                <element signature="class#Pair#0;class#javaClassifierComparison#0" />
    6878                <element signature="method#Algorithm_HoughLinesP_Single#0;class#javaClassifierComparison#0" />
    6979                <element signature="method#Algorithm_HoughLinesP_Combo#0;class#javaClassifierComparison#0" />
     80                <element signature="method#Algorithm_Morphology#0;class#javaClassifierComparison#0" />
     81                <element signature="method#Algorithm_Morphology#0;class#javaClassifierComparison#0" />
     82                <element signature="e#20655#20667#0" expanded="true" />
     83                <element signature="e#20738#20750#0" expanded="true" />
     84                <element signature="e#20820#20832#0" expanded="true" />
     85                <element signature="e#24593#24602#0" expanded="true" />
    7086                <element signature="method#Classifier_LineCounter#0;class#javaClassifierComparison#0" />
    7187                <element signature="method#Classifier_ClusterDetection#0;class#javaClassifierComparison#0" />
    7288                <element signature="method#removeDuplicates#0;class#javaClassifierComparison#0" />
     89                <element signature="method#VarianceCalc#0;class#javaClassifierComparison#0" />
    7390                <element signature="method#lineComparison#0;class#javaClassifierComparison#0" />
    7491                <element signature="method#ClusterCheck#0;class#javaClassifierComparison#0" />
     
    86103      <find>ClassifierLineClusterPt</find>
    87104      <find>Object</find>
     105      <find>areaCounter</find>
     106      <find>THRESHOLD_AREA_COUNT</find>
     107      <find>imageViewer</find>
     108      <find>coo.31924062612282-9.png</find>
     109      <find>FILLED</find>
    88110    </findStrings>
    89111  </component>
     
    175197      <window_info anchor="bottom" id="Message" order="0" />
    176198      <window_info anchor="bottom" id="Find" order="1" weight="0.32962137" />
    177       <window_info anchor="bottom" id="Run" order="2" weight="0.2494432" />
     199      <window_info anchor="bottom" id="Run" order="2" weight="0.27616927" />
    178200      <window_info anchor="bottom" id="Debug" order="3" weight="0.3997773" />
    179201      <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
     
    222244    <entry file="file://$PROJECT_DIR$/image-identification-development/src/Main.java">
    223245      <provider selected="true" editor-type-id="text-editor">
    224         <state relative-caret-position="3154">
    225           <caret line="820" column="49" selection-start-line="820" selection-start-column="49" selection-end-line="820" selection-end-column="49" />
     246        <state relative-caret-position="-969">
     247          <caret line="803" selection-start-line="803" selection-end-line="803" />
    226248          <folding>
    227249            <element signature="imports" expanded="true" />
     
    245267    <entry file="file://$PROJECT_DIR$/../image-identification-terminal/javaClassifierComparison.java">
    246268      <provider selected="true" editor-type-id="text-editor">
    247         <state relative-caret-position="4598">
    248           <caret line="356" selection-start-line="356" selection-end-line="356" />
     269        <state relative-caret-position="406">
     270          <caret line="606" column="5" lean-forward="true" selection-start-line="606" selection-start-column="5" selection-end-line="606" selection-end-column="5" />
    249271          <folding>
    250272            <element signature="imports" expanded="true" />
     273            <element signature="method#init#0;class#javaClassifierComparison#0" />
     274            <element signature="e#3021#4256#0" />
     275            <element signature="class#StartAndEndPoint#0;class#javaClassifierComparison#0" />
     276            <element signature="class#Pair#0;class#javaClassifierComparison#0" />
    251277            <element signature="method#Algorithm_HoughLinesP_Single#0;class#javaClassifierComparison#0" />
    252278            <element signature="method#Algorithm_HoughLinesP_Combo#0;class#javaClassifierComparison#0" />
     279            <element signature="method#Algorithm_Morphology#0;class#javaClassifierComparison#0" />
     280            <element signature="method#Algorithm_Morphology#0;class#javaClassifierComparison#0" />
     281            <element signature="e#20655#20667#0" expanded="true" />
     282            <element signature="e#20738#20750#0" expanded="true" />
     283            <element signature="e#20820#20832#0" expanded="true" />
     284            <element signature="e#24593#24602#0" expanded="true" />
    253285            <element signature="method#Classifier_LineCounter#0;class#javaClassifierComparison#0" />
    254286            <element signature="method#Classifier_ClusterDetection#0;class#javaClassifierComparison#0" />
    255287            <element signature="method#removeDuplicates#0;class#javaClassifierComparison#0" />
     288            <element signature="method#VarianceCalc#0;class#javaClassifierComparison#0" />
    256289            <element signature="method#lineComparison#0;class#javaClassifierComparison#0" />
    257290            <element signature="method#ClusterCheck#0;class#javaClassifierComparison#0" />
     
    262295    <entry file="file://$PROJECT_DIR$/image-identification-development/src/MainMorph.java">
    263296      <provider selected="true" editor-type-id="text-editor">
    264         <state relative-caret-position="221">
    265           <caret line="182" column="12" selection-start-line="182" selection-start-column="12" selection-end-line="182" selection-end-column="12" />
     297        <state relative-caret-position="321">
     298          <caret line="855" column="42" selection-start-line="855" selection-start-column="42" selection-end-line="855" selection-end-column="42" />
    266299          <folding>
    267300            <element signature="imports" expanded="true" />
    268             <element signature="e#7379#7795#1" />
    269             <element signature="e#7580#7900#1" />
    270             <element signature="e#11581#14087#1" />
    271             <element signature="e#14171#16210#1" />
    272             <element signature="e#16278#18925#1" />
    273             <element signature="e#19007#24734#1" />
    274             <element signature="e#24817#28826#1" />
    275             <element signature="e#28892#30082#1" />
    276             <element signature="e#30149#34144#1" />
    277             <element signature="e#34814#34826#0" expanded="true" />
     301            <element signature="e#10614#13084#1" />
     302            <element signature="e#13169#15172#1" />
     303            <element signature="e#15241#17864#1" />
     304            <element signature="e#17947#23618#1" />
     305            <element signature="e#23702#27679#1" />
     306            <element signature="e#27746#28924#1" />
     307            <element signature="e#28992#32955#1" />
     308            <element signature="e#35980#35992#0" expanded="true" />
     309            <element signature="e#37714#37726#0" expanded="true" />
     310            <element signature="e#37797#37809#0" expanded="true" />
     311            <element signature="e#41273#41282#0" expanded="true" />
     312            <element signature="e#44803#56145#1" />
     313            <element signature="e#48966#48978#0" expanded="true" />
     314            <element signature="e#49049#49061#0" expanded="true" />
     315            <element signature="e#49131#49143#0" expanded="true" />
     316            <element signature="e#52896#52905#0" expanded="true" />
    278317          </folding>
    279318        </state>
  • other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java

    r33444 r33447  
    6363import org.opencv.imgcodecs.Imgcodecs;
    6464import org.opencv.imgproc.Imgproc;
     65import org.opencv.imgproc.Moments;
    6566
    6667import static org.opencv.core.Core.FILLED;
    6768import static org.opencv.core.CvType.CV_8UC3;
     69import static org.opencv.highgui.HighGui.createJFrame;
    6870import static org.opencv.highgui.HighGui.imshow;
    6971import static org.opencv.imgcodecs.Imgcodecs.imwrite;
    7072
    7173import java.io.File;
     74import java.lang.reflect.Array;
    7275import java.util.ArrayList;
    7376
     
    9093//https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
    9194//https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values
     95//https://stackoverflow.com/questions/18345969/how-to-get-the-mass-center-of-a-contour-android-opencv
     96//https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html
    9297
    9398
     
    115120
    116121    static double THRESHOLD_C    = 4;
    117     static double THRESHOLD_AREA_SIZE  = 1000;
    118     static double THRESHOLD_AREA_COUNT = 2;
     122    static double THRESHOLD_AREA_SIZE  = 10000;
     123    static double THRESHOLD_AREA_COUNT = 10;
    119124
    120125    //
     
    153158        }
    154159    }
     160
     161
     162
    155163    //MAIN
    156164    public static void main(String[] args) {
     
    158166        try {
    159167            //Variables
    160             System.out.println("Running code version: " + CODE_VERSION);
    161             Mat edgesDetected = new Mat();
     168            System.out.println("Running Code version: " + CODE_VERSION + " Image Version: " +IMAGE_VERSION);
    162169            Mat mid = new Mat();
    163170            Mat edgesDetectedRGB = new Mat();
     
    175182            //String default_file = directory+"NotSheetMusic/coo.31924062612282-9.png";
    176183            //String default_file = directory+"NotSheetMusic/mdp.39015097852365-2.png";
    177             //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY?
     184            //String default_file =testDirectory+"TestImages/NotNot/mdp.39015080972303-3.png"; //WHY GREY? DUE TO IMAGE LARGE, ZOOM IN
    178185            //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
    179186            //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
    180187            //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
    181188            //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png";
     189            //String default_file =hiresDirectory+"MU/SheetMusic/bc.ark+=13960=t2j72dt1p-10.png";
     190            //String default_file =hiresDirectory+"MU/SheetMusic/bc.ark+=13960=t2j72dt1p-7.png";
     191            String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
    182192            //String default_file =hiresDirectory+"MU/NotSheetMusic/mdp.39015096363935-1.png";
    183             String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
     193            //String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
    184194
    185195            //System.out.println(default_file);
     
    356366            }
    357367            //Better morphology attempt - static
    358             if(CODE_VERSION ==4) {
     368            if(CODE_VERSION == 4) {
    359369
    360370                //Display Original
     
    474484            }
    475485            //Better morphology attempt - dynamic
    476             if(CODE_VERSION ==5) {
     486            if(CODE_VERSION == 5) {
    477487                int hori = original.width();
    478488                int vert = original.height();
     
    553563            }
    554564            //MASK UNDERSTANDING
    555             if(CODE_VERSION ==6) {
     565            if(CODE_VERSION == 6) {
    556566                String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
    557567                Mat mask = new Mat();
     
    579589            }
    580590            //Mask implementation
    581             if(CODE_VERSION ==7) {
     591            if(CODE_VERSION == 7) {
    582592
    583593                //Display Original
     
    664674
    665675            }
    666             //Mask implementation - HIGH RES NUMBER MOD
    667             if(CODE_VERSION ==8) {
     676            //Mask implementations (LARGE AND SMALL) - HIGH RES NUMBER MOD
     677            if(CODE_VERSION == 8) {
    668678
    669679                //Display Original
    670680                imageViewer("original", original1);
    671681
    672                 Mat src = original.clone();
    673682                Mat test = original.clone();
    674                 Mat mask = new Mat();
    675                 Mat dst = new Mat();
    676 
    677                 imageViewer("00 Inverse Binarized Original", src);
    678 
    679                 //Close then Open
    680 
    681 //                Mat firstKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
    682 //                Imgproc.morphologyEx(src, mask, Imgproc.MORPH_CLOSE, firstKernel);
    683 //                imageViewer("01 Closed - mask", mask);
    684 //
    685 //                Mat secondKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
    686 //                Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, secondKernel);
    687 //                imageViewer("02 Open - mask", mask);
    688 
     683
     684
     685                imageViewer("00 Inverse Binarized Original", test);
     686
     687
     688                //************************************
     689                //Large Object Removal
     690                //************************************
     691                Mat srcLOR = original.clone();
     692                Mat maskLOR = new Mat();
     693                Mat dstLOR = new Mat();
    689694
    690695                //denoize
    691696                Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
    692                 Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
    693                 imageViewer("01 Denoize - mask", mask);
     697                Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
     698                imageViewer("01 Denoize - mask", maskLOR);
    694699
    695700                //close up gaps
    696701                Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
    697                 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
    698                 imageViewer("02 gap closer - mask", mask);
     702                Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
     703                imageViewer("02 gap closer - mask", maskLOR);
    699704
    700705                //Isolate large items
    701706                Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
    702                 Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
    703                 imageViewer("03 Isolate Large - mask", mask);
    704                 Core.bitwise_not(mask,mask);
    705 
    706                 //Remove unwanted large items from image
    707                 src.copyTo(dst, mask);
    708                 imageViewer("04 Large Items Removed", dst);
    709 
     707                Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
     708                imageViewer("03 Isolate Large - mask", maskLOR);
     709
     710                //Remove large items from image
     711                Core.bitwise_not(maskLOR,maskLOR);
     712                srcLOR.copyTo(dstLOR, maskLOR);
     713                imageViewer("04 Large Items Removed", dstLOR);
     714
     715                //****************************************
     716                //Small object removal (SOR)
     717                //****************************************
     718
     719                Mat srcSOR = dstLOR.clone();
     720                Mat maskSOR = new Mat();
     721                Mat dstSOR = new Mat();
     722
     723                Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
     724                Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
     725                imageViewer("11 show small - mask", maskSOR);
     726
     727                Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
     728                Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
     729                imageViewer("12 highlight small - mask", maskSOR);
     730
     731/*              Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
     732                Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
     733                imageViewer("13 isolate small - mask", maskSOR);
     734*/
     735
     736                //Remove small items from image
     737                Core.bitwise_not(maskSOR, maskSOR);
     738                srcSOR.copyTo(dstSOR, maskSOR);
     739                imageViewer("14 Small Items Removed", dstSOR);
     740
     741
     742                //****************************************
    710743                //start staff line detection
     744                //****************************************
    711745
    712746                Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
    713                 Imgproc.erode(dst,test,kernelErode);
    714                 imageViewer("11 Erode plus pre", test);
     747                Imgproc.erode(dstSOR,test,kernelErode);
     748                imageViewer("21 Erode plus pre", test);
    715749
    716750                Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
    717751                Imgproc.dilate(test,test,kernelDilate);
    718                 imageViewer("12 Dilate", test);
    719 
    720                 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
    721                 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
    722                 imageViewer("13 Open", test);
     752                imageViewer("22 Dilate", test);
     753
     754                Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
     755                Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
     756                imageViewer("23 Close", test);
     757
    723758
    724759                Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
    725760                Imgproc.erode(test,test,kernelErode02);
    726                 imageViewer("14 Erode (Final)", test);
    727 
    728 
     761                imageViewer("24 Erode (Final)", test);
     762
     763                //********************************************************************************
    729764                //DETECT OUTLINE AND FIND AREA OF THESE LINES.
     765                //********************************************************************************
    730766                ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     767                ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
     768                ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
    731769                Mat hierarchy = new Mat();
    732770
     
    738776                Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    739777
     778                System.out.println(contours.size());
    740779                //Draw contours and record areas
    741                 Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
     780                Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
     781                Mat largeContoursFound = allContoursFound.clone() ;
     782                Mat postContoursFound = allContoursFound.clone();
    742783                int areaCounter = 0;
    743784
     
    748789                //Discuss with david before weekend perhaps?
    749790
    750                 Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
    751 //                   for (int i = 0; i < contours.size(); i++) {
    752 //                    Scalar color = new Scalar(0, i, i);
    753 //                    double area = Imgproc.contourArea(contours.get(i));
    754 //                    Imgproc.drawContours(drawing, contours, i, color, FILLED);
    755 //                    System.out.println("AREA: " + area);
    756 //
    757 //                }
    758                 imageViewer("Contours found", drawing);
     791                Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
     792                for (int i = 0; i < contours.size(); i++) {
     793                    double area = Imgproc.contourArea(contours.get(i));
     794                    if(area > 100) {
     795                        System.out.println("AREA: " + area);
     796                        Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
     797                        //create list of large coutours found
     798                        largeContours.add(contours.get(i));
     799                    }
     800                }
     801                imageViewer("80 All Contours found", allContoursFound);
     802                imageViewer("81 Large Contours Found", largeContoursFound);
     803
     804                //*****************************************************************
     805                //Circles and centres on processed images
     806                //*****************************************************************
     807
     808                //Init arrays
     809                Mat circleOutput = allContoursFound.clone();
     810                MatOfPoint2f[] contoursPoly  = new MatOfPoint2f[largeContours.size()];
     811                Point[] centers = new Point[largeContours.size()];
     812                float[][] radius = new float[largeContours.size()][1];
     813
     814                //Fill arrays
     815                for (int i = 0; i < largeContours.size(); i++) {
     816                    contoursPoly[i] = new MatOfPoint2f();
     817                    Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
     818                    centers[i] = new Point();
     819                    Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
     820
     821                }
     822                //Draw circle for each large contour
     823                for (int i = 0; i < largeContours.size(); i++) {
     824                    Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
     825                }
     826                imageViewer("82 Circles found", circleOutput);
     827
     828                //********************************************************************************
     829                //Centroids - Everything must be to scale
     830                //********************************************************************************
     831
     832                ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
     833                Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
     834                for (int i = 0; i < largeContours.size(); i++) {
     835                    mu.add(i, Imgproc.moments(largeContours.get(i), false));
     836                    Moments p = mu.get(i);
     837                    int x = (int) (p.get_m10() / p.get_m00());
     838                    int y = (int) (p.get_m01() / p.get_m00());
     839                    Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
     840                }
     841                imageViewer("83 Centres found", centreOutput);
     842
     843
     844                //***********************************************
     845                //PostProcessing - Morphology Classifier
     846                //  Use dilation to "Connect the dots"
     847                //  Testing showed the centroids were clustered together
     848                //  Then use area or perimeter as a classifier filter
     849                //REFINEREFINEREIFEN
     850                //REFINEREFINEREIFEN
     851                //REFINEREFINEREIFEN
     852                //REFINEREFINEREIFEN
     853                //REFINEREFINEREIFEN
     854                //REFINEREFINEREIFEN
     855                // FIX UP CLASSIFIER COMPARISON.
     856                // MORPHOLOGIES FUNCTIONS RETURN NULL AS THEIR RETURN VARIABLES. WHY?
     857                //REFINEREFINEREIFEN
     858                //REFINEREFINEREIFEN
     859                //REFINEREFINEREIFEN
     860                //REFINEREFINEREIFEN
     861                //REFINEREFINEREIFEN
     862                //***********************************************
     863
     864                Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
     865                Imgproc.dilate(centreOutput,centreOutput,postDilate);
     866                imageViewer("91 PostDilated", centreOutput);
     867
     868                Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
     869                Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
     870                imageViewer("92 PostClose", centreOutput);
     871
     872                Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
     873                Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
     874                imageViewer("93 PostDenoize", centreOutput);
     875
     876                //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50));
     877                //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline);
     878
     879
     880
     881                //Find area
     882                Mat centreOutputGrey = new Mat();
     883                Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
     884                Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     885
     886                for (int i = 0; i < postContours.size(); i++) {
     887                    double area = Imgproc.contourArea(postContours.get(i));
     888                    if(area > THRESHOLD_AREA_SIZE) {
     889                        System.out.println("POST AREA: " + area);
     890                        Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
     891                        areaCounter++;
     892                    }
     893                }
     894
     895
     896
     897                imageViewer("93 PostEND", postContoursFound);
    759898
    760899                //Classifier Calculation
     
    766905
    767906            }
     907            //ClassifierComparison - Using code version 8
     908            if(CODE_VERSION == 81) {
     909
     910                //Display Original
     911                //imageViewer("original", original1);
     912
     913                Mat test = original.clone();
     914
     915
     916                //imageViewer("00 Inverse Binarized Original", test);
     917
     918
     919                //************************************
     920                //Large Object Removal
     921                //************************************
     922                Mat srcLOR = original.clone();
     923                Mat maskLOR = new Mat();
     924                Mat dstLOR = new Mat();
     925
     926                //denoize
     927                Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
     928                Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
     929                //imageViewer("01 Denoize - mask", maskLOR);
     930
     931                //close up gaps
     932                Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
     933                Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
     934                //imageViewer("02 gap closer - mask", maskLOR);
     935
     936                //Isolate large items
     937                Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
     938                Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
     939                //imageViewer("03 Isolate Large - mask", maskLOR);
     940
     941                //Remove large items from image
     942                Core.bitwise_not(maskLOR,maskLOR);
     943                srcLOR.copyTo(dstLOR, maskLOR);
     944                //imageViewer("04 Large Items Removed", dstLOR);
     945
     946                //****************************************
     947                //Small object removal (SOR)
     948                //****************************************
     949
     950                Mat srcSOR = dstLOR.clone();
     951                Mat maskSOR = new Mat();
     952                Mat dstSOR = new Mat();
     953
     954                Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
     955                Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
     956                //imageViewer("11 show small - mask", maskSOR);
     957
     958                Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
     959                Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
     960                //imageViewer("12 highlight small - mask", maskSOR);
     961
     962/*              Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
     963                Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
     964                imageViewer("13 isolate small - mask", maskSOR);
     965*/
     966
     967                //Remove small items from image
     968                Core.bitwise_not(maskSOR, maskSOR);
     969                srcSOR.copyTo(dstSOR, maskSOR);
     970                //imageViewer("14 Small Items Removed", dstSOR);
     971
     972
     973                //****************************************
     974                //start staff line detection
     975                //****************************************
     976
     977                Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
     978                Imgproc.erode(dstSOR,test,kernelErode);
     979                //imageViewer("21 Erode plus pre", test);
     980
     981                Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
     982                Imgproc.dilate(test,test,kernelDilate);
     983                //imageViewer("22 Dilate", test);
     984
     985                Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
     986                Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
     987                //imageViewer("23 Close", test);
     988
     989
     990                Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
     991                Imgproc.erode(test,test,kernelErode02);
     992                //imageViewer("24 Erode (Final)", test);
     993
     994                //********************************************************************************
     995                //DETECT OUTLINE AND FIND AREA OF THESE LINES.
     996                //********************************************************************************
     997                ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     998                ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
     999                ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
     1000                Mat hierarchy = new Mat();
     1001
     1002                //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
     1003                //(contours)  output array of arrays: Detected contours. Each contour is stored as a vector of points
     1004                //(hierarchy) output array:           Optional output vector, containing information about the image topology.
     1005                //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
     1006
     1007                Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     1008
     1009                System.out.println(contours.size());
     1010                //Draw contours and record areas
     1011                Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
     1012                Mat largeContoursFound = allContoursFound.clone() ;
     1013                Mat postContoursFound = allContoursFound.clone();
     1014                int areaCounter = 0;
     1015
     1016                //Have created a preprocess to remove large objects.
     1017                //Need to now finalized Classifier, re try area detection.
     1018                //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
     1019                //Just use area and periemter to determine if sheet music
     1020                //Discuss with david before weekend perhaps?
     1021
     1022                Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
     1023                for (int i = 0; i < contours.size(); i++) {
     1024                    double area = Imgproc.contourArea(contours.get(i));
     1025                    if(area > 100) {
     1026                        //System.out.println("AREA: " + area);
     1027                        Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
     1028                        //create list of large coutours found
     1029                        largeContours.add(contours.get(i));
     1030                    }
     1031                }
     1032                //imageViewer("80 All Contours found", allContoursFound);
     1033                //imageViewer("81 Large Contours Found", largeContoursFound);
     1034
     1035                //*****************************************************************
     1036                //Circles and centres on processed images
     1037                //*****************************************************************
     1038
     1039                //Init arrays
     1040                Mat circleOutput = allContoursFound.clone();
     1041                MatOfPoint2f[] contoursPoly  = new MatOfPoint2f[largeContours.size()];
     1042                Point[] centers = new Point[largeContours.size()];
     1043                float[][] radius = new float[largeContours.size()][1];
     1044
     1045                //Fill arrays
     1046                for (int i = 0; i < largeContours.size(); i++) {
     1047                    contoursPoly[i] = new MatOfPoint2f();
     1048                    Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
     1049                    centers[i] = new Point();
     1050                    Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
     1051
     1052                }
     1053                //Draw circle for each large contour
     1054                for (int i = 0; i < largeContours.size(); i++) {
     1055                    Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
     1056                }
     1057                //imageViewer("82 Circles found", circleOutput);
     1058
     1059                //********************************************************************************
     1060                //Centroids - Everything must be to scale
     1061                //********************************************************************************
     1062
     1063                ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
     1064                Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
     1065                for (int i = 0; i < largeContours.size(); i++) {
     1066                    mu.add(i, Imgproc.moments(largeContours.get(i), false));
     1067                    Moments p = mu.get(i);
     1068                    int x = (int) (p.get_m10() / p.get_m00());
     1069                    int y = (int) (p.get_m01() / p.get_m00());
     1070                    Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
     1071                }
     1072                //imageViewer("83 Centres found", centreOutput);
     1073
     1074
     1075                //***********************************************
     1076                //PostProcessing - Morphology Classifier
     1077                //  Use dilation to "Connect the dots"
     1078                //  Testing showed the centroids were clustered together
     1079                //  Then use area or perimeter as a classifier filter
     1080                //REFINEREFINEREIFEN
     1081                //REFINEREFINEREIFEN
     1082                //REFINEREFINEREIFEN
     1083                //REFINEREFINEREIFEN
     1084                //REFINEREFINEREIFEN
     1085                //REFINEREFINEREIFEN
     1086                //***********************************************
     1087
     1088                Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
     1089                Imgproc.dilate(centreOutput,centreOutput,postDilate);
     1090                //imageViewer("91 PostDilated", centreOutput);
     1091
     1092                Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
     1093                Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
     1094                //imageViewer("92 PostClose", centreOutput);
     1095
     1096                Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
     1097                Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
     1098                //imageViewer("93 PostDenoize", centreOutput);
     1099
     1100                //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50));
     1101                //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline);
     1102
     1103
     1104
     1105                //Find area
     1106                Mat centreOutputGrey = new Mat();
     1107                Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
     1108                Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     1109
     1110                for (int i = 0; i < postContours.size(); i++) {
     1111                    double area = Imgproc.contourArea(postContours.get(i));
     1112                    if(area > 10000) {
     1113                        //System.out.println("POST AREA: " + area);
     1114                        Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
     1115                        areaCounter++;
     1116                    }
     1117                }
     1118                //imageViewer("93 PostEND", postContoursFound);
     1119
     1120                //Classifier Calculation
     1121                if(areaCounter >= THRESHOLD_AREA_COUNT){
     1122                    //System.out.println("THIS IS SHEET MUSIC");
     1123                    //System.out.println(areaCounter);
     1124                }
     1125
     1126
     1127            }
    7681128
    7691129            //USE stuc element, to rule out large wide and long pieces of black and white.
     
    7791139
    7801140
    781             imshow("LINESFOUND", edgesDetectedRGB);
    782             HighGui.resizeWindow("LINESFOUND", 1000,1000);
     1141            //imshow("LINESFOUND", edgesDetectedRGB);
     1142            //HighGui.resizeWindow("LINESFOUND", 1000,1000);
    7831143           
    7841144            //HighGui.imshow("CLUSTERS FOUND", clustersFoundRGB);
     
    7931153
    7941154            // Wait and Exit
    795             HighGui.waitKey();
     1155            //HighGui.waitKey();
    7961156            System.exit(0);
    7971157        }
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/Makefile

    r33437 r33447  
    3535    less logSingle.txt
    3636   
     37testrun-classifier-single-combo:
     38    ./runClassifer.sh TestImages/test-coo.31924062612282-9.png combo logSingle.txt -singleNew
     39    echo "Displaying javaImageClassifier output"
     40        #Calculate the Accuracy of the classifier
     41    java javaAccuracyCalculator logSingle.txt houghlinesP resultSingle.txt
     42        #Display output
     43    less logSingle.txt
     44   
    3745testrun-classifier-single-morph:
    3846    javac -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar javaClassifierComparison.java;
    39     ./runClassifer.sh TestImages/test-coo.31924062612282-9.png morphology logSingle.txt -singleNew
     47    ./runClassifer.sh /Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/SheetMusic/coo.31924062612282-9.png morphology log-all-hires-new/test/logSingle.txt -singleNew
    4048    echo "Displaying javaImageClassifier output"
    4149        #Calculate the Accuracy of the classifier
    42     java javaAccuracyCalculator logSingle.txt morphology resultSingle.txt
     50    java javaAccuracyCalculator log-all-hires-new/test/logSingle.txt morphology-MU log-all-hires-new/test/resultSingle.txt
    4351        #Display output
    44     less logSingle.txt
     52    less log-all-hires-new/test/logSingle.txt
    4553
    4654
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/configClassifierComparison.properties

    r33439 r33447  
    1313CLUSTER_DISTANCE_MIN            = 2
    1414#MORPHOLOGY
    15 THRESHOLD_AREA_SIZE         = 1000
    16 THRESHOLD_AREA_COUNT            = 4
     15THRESHOLD_AREA_SIZE         = 10000
     16THRESHOLD_AREA_COUNT            = 10
    1717
    1818
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaClassifierComparison.java

    r33439 r33447  
    1 
    21import org.opencv.core.*;
    32import org.opencv.core.Point;
     
    54import org.opencv.imgcodecs.Imgcodecs;
    65import org.opencv.imgproc.Imgproc;
    7 import static org.opencv.imgcodecs.Imgcodecs.imwrite;
     6import org.opencv.imgproc.Moments;
     7//import org.opencv.core.Core.FILLED;
     8//import org.opencv.imgcodecs.Imgcodecs.imwrite;
    89import java.awt.image.BufferedImage;
    910import java.awt.image.DataBufferByte;
     
    200201                        break;
    201202                    case "morphology":
    202                         algorithmResult = Algorithm_Morphology(imageFilename);
     203                        algorithmResult = Algorithm_MorphologyOLD(imageFilename);
    203204                        bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + algorithmResult.getBoolean() + '\t' + "Number of areas:" + '\t' + algorithmResult.getInteger() + '\t' + classifierType + '\n');
    204205                        break;
     
    324325        return returnVariables;
    325326    }
    326     private static Pair Algorithm_Morphology(String filename){
     327    private static Pair Algorithm_MorphologyOLD(String filename){
     328
    327329        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    328330        Boolean isSheetMusic = null;
     
    365367                }
    366368            }
     369
     370
     371
     372
     373
     374
    367375            //Calculates if sheet music or not
    368376            if(areaCounter >= THRESHOLD_AREA_COUNT){
     
    376384        return returnVariables;
    377385    }
    378 
     386    private static Pair Algorithm_Morphology(String filename){
     387
     388        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     389        Boolean isSheetMusic = null;
     390        Pair returnVariables = new Pair();
     391        try{
     392            int FILLED = -1;
     393            //Display Original
     394            //imageViewer("original", original1);
     395            Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
     396            Mat test = original.clone();
     397            //imageViewer("00 Inverse Binarized Original", test);
     398
     399
     400            //************************************
     401            //Large Object Removal
     402            //************************************
     403            Mat srcLOR = original.clone();
     404            Mat maskLOR = new Mat();
     405            Mat dstLOR = new Mat();
     406
     407            //denoize
     408            Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
     409            Imgproc.morphologyEx(srcLOR,maskLOR, Imgproc.MORPH_OPEN, denoize);
     410            //imageViewer("01 Denoize - mask", maskLOR);
     411
     412            //close up gaps
     413            Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
     414            Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_CLOSE, gapCloser);
     415            //imageViewer("02 gap closer - mask", maskLOR);
     416
     417            //Isolate large items
     418            Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
     419            Imgproc.morphologyEx(maskLOR,maskLOR,Imgproc.MORPH_OPEN, isolateLarge);
     420            //imageViewer("03 Isolate Large - mask", maskLOR);
     421
     422            //Remove large items from image
     423            Core.bitwise_not(maskLOR,maskLOR);
     424            srcLOR.copyTo(dstLOR, maskLOR);
     425            //imageViewer("04 Large Items Removed", dstLOR);
     426
     427            //****************************************
     428            //Small object removal (SOR)
     429            //****************************************
     430
     431            Mat srcSOR = dstLOR.clone();
     432            Mat maskSOR = new Mat();
     433            Mat dstSOR = new Mat();
     434
     435            Mat startSOR =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
     436            Imgproc.morphologyEx(srcSOR,maskSOR, Imgproc.MORPH_OPEN, startSOR);
     437            //imageViewer("11 show small - mask", maskSOR);
     438
     439            Mat highlightSmall = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(7,7));
     440            Imgproc.dilate(maskSOR, maskSOR, highlightSmall);
     441            //imageViewer("12 highlight small - mask", maskSOR);
     442
     443/*              Mat isolateSmall =Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
     444                Imgproc.morphologyEx(maskSOR,maskSOR,Imgproc.MORPH_CLOSE, isolateSmall);
     445                imageViewer("13 isolate small - mask", maskSOR);
     446*/
     447
     448            //Remove small items from image
     449            Core.bitwise_not(maskSOR, maskSOR);
     450            srcSOR.copyTo(dstSOR, maskSOR);
     451            //imageViewer("14 Small Items Removed", dstSOR);
     452
     453
     454            //****************************************
     455            //start staff line detection
     456            //****************************************
     457
     458            Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
     459            Imgproc.erode(dstSOR,test,kernelErode);
     460            //imageViewer("21 Erode plus pre", test);
     461
     462            Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
     463            Imgproc.dilate(test,test,kernelDilate);
     464            //imageViewer("22 Dilate", test);
     465
     466            Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
     467            Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
     468            //imageViewer("23 Close", test);
     469
     470
     471            Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
     472            Imgproc.erode(test,test,kernelErode02);
     473            //imageViewer("24 Erode (Final)", test);
     474
     475            //********************************************************************************
     476            //DETECT OUTLINE AND FIND AREA OF THESE LINES.
     477            //********************************************************************************
     478            ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
     479            ArrayList<MatOfPoint> largeContours = new ArrayList<MatOfPoint>();
     480            ArrayList<MatOfPoint> postContours = new ArrayList<MatOfPoint>();
     481            Mat hierarchy = new Mat();
     482
     483            //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
     484            //(contours)  output array of arrays: Detected contours. Each contour is stored as a vector of points
     485            //(hierarchy) output array:           Optional output vector, containing information about the image topology.
     486            //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
     487
     488            Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     489
     490            System.out.println(contours.size());
     491            //Draw contours and record areas
     492            Mat allContoursFound = Mat.zeros(test.size(), CvType.CV_8UC3);
     493            Mat largeContoursFound = allContoursFound.clone() ;
     494            Mat postContoursFound = allContoursFound.clone();
     495            int areaCounter = 0;
     496
     497            //Have created a preprocess to remove large objects.
     498            //Need to now finalized Classifier, re try area detection.
     499            //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
     500            //Just use area and periemter to determine if sheet music
     501            //Discuss with david before weekend perhaps?
     502
     503            Imgproc.drawContours(allContoursFound, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
     504            for (int i = 0; i < contours.size(); i++) {
     505                double area = Imgproc.contourArea(contours.get(i));
     506                if(area > 100) {
     507                    //System.out.println("AREA: " + area);
     508                    Imgproc.drawContours(largeContoursFound, contours, i, new Scalar(255, 0, 0), FILLED);
     509                    //create list of large coutours found
     510                    largeContours.add(contours.get(i));
     511                }
     512            }
     513            //imageViewer("80 All Contours found", allContoursFound);
     514            //imageViewer("81 Large Contours Found", largeContoursFound);
     515
     516            //*****************************************************************
     517            //Circles and centres on processed images
     518            //*****************************************************************
     519
     520            //Init arrays
     521            Mat circleOutput = allContoursFound.clone();
     522            MatOfPoint2f[] contoursPoly  = new MatOfPoint2f[largeContours.size()];
     523            Point[] centers = new Point[largeContours.size()];
     524            float[][] radius = new float[largeContours.size()][1];
     525
     526            //Fill arrays
     527            for (int i = 0; i < largeContours.size(); i++) {
     528                contoursPoly[i] = new MatOfPoint2f();
     529                Imgproc.approxPolyDP(new MatOfPoint2f(largeContours.get(i).toArray()), contoursPoly[i], 1, true);
     530                centers[i] = new Point();
     531                Imgproc.minEnclosingCircle(contoursPoly[i], centers[i], radius[i]);
     532
     533            }
     534            //Draw circle for each large contour
     535            for (int i = 0; i < largeContours.size(); i++) {
     536                Imgproc.circle(circleOutput, centers[i], (int) radius[i][0],new Scalar(255, 0, 0), 1);
     537            }
     538            //imageViewer("82 Circles found", circleOutput);
     539
     540            //********************************************************************************
     541            //Centroids - Everything must be to scale
     542            //********************************************************************************
     543
     544            ArrayList<Moments> mu = new ArrayList<Moments>(largeContours.size());
     545            Mat centreOutput = Mat.zeros(largeContoursFound.size(), CvType.CV_8UC3);
     546            for (int i = 0; i < largeContours.size(); i++) {
     547                mu.add(i, Imgproc.moments(largeContours.get(i), false));
     548                Moments p = mu.get(i);
     549                int x = (int) (p.get_m10() / p.get_m00());
     550                int y = (int) (p.get_m01() / p.get_m00());
     551                Imgproc.circle(centreOutput, new Point(x, y), 4, new Scalar(255, 255, 255), 30);
     552            }
     553            //imageViewer("83 Centres found", centreOutput);
     554
     555
     556            //***********************************************
     557            //PostProcessing - Morphology Classifier
     558            //  Use dilation to "Connect the dots"
     559            //  Testing showed the centroids were clustered together
     560            //  Then use area or perimeter as a classifier filter
     561            //***********************************************
     562
     563            Mat postDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(150,15));
     564            Imgproc.dilate(centreOutput,centreOutput,postDilate);
     565            //imageViewer("91 PostDilated", centreOutput);
     566
     567            Mat postClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
     568            Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_CLOSE, postClose);
     569            //imageViewer("92 PostClose", centreOutput);
     570
     571            Mat postDenoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,100));
     572            Imgproc.morphologyEx(centreOutput,centreOutput, Imgproc.MORPH_OPEN, postDenoize);
     573            //imageViewer("93 PostDenoize", centreOutput);
     574
     575            //Mat postOutline = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(50,50));
     576            //Imgproc.morphologyEx(centreOutput, centreOutput, Imgproc.MORPH_GRADIENT, postOutline);
     577
     578            //Find area
     579            Mat centreOutputGrey = new Mat();
     580            Imgproc.cvtColor(centreOutput, centreOutputGrey, Imgproc.COLOR_RGB2GRAY);
     581            Imgproc.findContours(centreOutputGrey, postContours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
     582
     583            for (int i = 0; i < postContours.size(); i++) {
     584                double area = Imgproc.contourArea(postContours.get(i));
     585                if(area > THRESHOLD_AREA_SIZE) {
     586                    System.out.println("POST AREA: " + area + "AREA COUNTER: " + areaCounter);
     587                    //Imgproc.drawContours(postContoursFound, postContours, i, new Scalar(0, 255, 0), FILLED);
     588                    areaCounter++;
     589                }
     590            }
     591
     592            //imageViewer("93 PostEND", postContoursFound);
     593
     594            //Calculates if sheet music or not
     595            if(areaCounter >= THRESHOLD_AREA_COUNT){
     596                returnVariables = new Pair(true, areaCounter);
     597                //returnVariables.setBoolean(true);
     598                //returnVariables.setInteger(areaCounter);
     599                System.out.println("TEST RETURN VARIABLES: "+ returnVariables.toString());
     600            }
     601
     602        }
     603        catch(Exception e){
     604            System.err.println(e);
     605        }
     606        return returnVariables;
     607    }
    379608    //******************
    380609    //CLASSIFIER FUNCTIONS
Note: See TracChangeset for help on using the changeset viewer.