Index: /other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/.idea/workspace.xml
===================================================================
--- /other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/.idea/workspace.xml	(revision 33443)
+++ /other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/.idea/workspace.xml	(revision 33444)
@@ -15,6 +15,6 @@
         <entry file="file://$PROJECT_DIR$/image-identification-development/src/Main.java">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="874">
-              <caret line="116" column="48" selection-start-line="116" selection-start-column="48" selection-end-line="116" selection-end-column="48" />
+            <state relative-caret-position="3154">
+              <caret line="820" column="49" selection-start-line="820" selection-start-column="49" selection-end-line="820" selection-end-column="49" />
               <folding>
                 <element signature="imports" expanded="true" />
@@ -40,14 +40,18 @@
         <entry file="file://$PROJECT_DIR$/image-identification-development/src/MainMorph.java">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="266">
-              <caret line="407" column="55" selection-start-line="407" selection-start-column="55" selection-end-line="407" selection-end-column="55" />
+            <state relative-caret-position="221">
+              <caret line="182" column="12" selection-start-line="182" selection-start-column="12" selection-end-line="182" selection-end-column="12" />
               <folding>
                 <element signature="imports" expanded="true" />
-                <element signature="class#StartAndEndPoint#0;class#MainMorph#0" />
-                <element signature="method#toBufferedImage#0;class#MainMorph#0" />
-                <element signature="e#11164#13670#1" />
-                <element signature="e#13754#15793#1" />
-                <element signature="e#15861#18508#1" />
-                <element signature="e#21594#25229#1" />
+                <element signature="e#7379#7795#1" />
+                <element signature="e#7580#7900#1" />
+                <element signature="e#11581#14087#1" />
+                <element signature="e#14171#16210#1" />
+                <element signature="e#16278#18925#1" />
+                <element signature="e#19007#24734#1" />
+                <element signature="e#24817#28826#1" />
+                <element signature="e#28892#30082#1" />
+                <element signature="e#30149#34144#1" />
+                <element signature="e#34814#34826#0" expanded="true" />
               </folding>
             </state>
@@ -58,5 +62,5 @@
         <entry file="file://$PROJECT_DIR$/../image-identification-terminal/javaClassifierComparison.java">
           <provider selected="true" editor-type-id="text-editor">
-            <state relative-caret-position="4332">
+            <state relative-caret-position="4598">
               <caret line="356" selection-start-line="356" selection-end-line="356" />
               <folding>
@@ -111,9 +115,5 @@
       <foldersAlwaysOnTop value="true" />
     </navigator>
-    <panes>
-      <pane id="ProjectPane" />
-      <pane id="PackagesPane" />
-      <pane id="Scope" />
-    </panes>
+    <panes />
   </component>
   <component name="PropertiesComponent">
@@ -167,5 +167,4 @@
   <component name="ToolWindowManager">
     <frame x="67" y="25" width="1613" height="1025" extended-state="6" />
-    <editor active="true" />
     <layout>
       <window_info content_ui="combo" id="Project" order="0" weight="0.19783024" />
@@ -175,7 +174,7 @@
       <window_info id="Favorites" order="4" side_tool="true" />
       <window_info anchor="bottom" id="Message" order="0" />
-      <window_info anchor="bottom" id="Find" order="1" />
-      <window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.2494432" />
-      <window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
+      <window_info anchor="bottom" id="Find" order="1" weight="0.32962137" />
+      <window_info anchor="bottom" id="Run" order="2" weight="0.2494432" />
+      <window_info anchor="bottom" id="Debug" order="3" weight="0.3997773" />
       <window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
       <window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@@ -223,6 +222,6 @@
     <entry file="file://$PROJECT_DIR$/image-identification-development/src/Main.java">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="874">
-          <caret line="116" column="48" selection-start-line="116" selection-start-column="48" selection-end-line="116" selection-end-column="48" />
+        <state relative-caret-position="3154">
+          <caret line="820" column="49" selection-start-line="820" selection-start-column="49" selection-end-line="820" selection-end-column="49" />
           <folding>
             <element signature="imports" expanded="true" />
@@ -246,5 +245,5 @@
     <entry file="file://$PROJECT_DIR$/../image-identification-terminal/javaClassifierComparison.java">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="4332">
+        <state relative-caret-position="4598">
           <caret line="356" selection-start-line="356" selection-end-line="356" />
           <folding>
@@ -263,14 +262,18 @@
     <entry file="file://$PROJECT_DIR$/image-identification-development/src/MainMorph.java">
       <provider selected="true" editor-type-id="text-editor">
-        <state relative-caret-position="266">
-          <caret line="407" column="55" selection-start-line="407" selection-start-column="55" selection-end-line="407" selection-end-column="55" />
+        <state relative-caret-position="221">
+          <caret line="182" column="12" selection-start-line="182" selection-start-column="12" selection-end-line="182" selection-end-column="12" />
           <folding>
             <element signature="imports" expanded="true" />
-            <element signature="class#StartAndEndPoint#0;class#MainMorph#0" />
-            <element signature="method#toBufferedImage#0;class#MainMorph#0" />
-            <element signature="e#11164#13670#1" />
-            <element signature="e#13754#15793#1" />
-            <element signature="e#15861#18508#1" />
-            <element signature="e#21594#25229#1" />
+            <element signature="e#7379#7795#1" />
+            <element signature="e#7580#7900#1" />
+            <element signature="e#11581#14087#1" />
+            <element signature="e#14171#16210#1" />
+            <element signature="e#16278#18925#1" />
+            <element signature="e#19007#24734#1" />
+            <element signature="e#24817#28826#1" />
+            <element signature="e#28892#30082#1" />
+            <element signature="e#30149#34144#1" />
+            <element signature="e#34814#34826#0" expanded="true" />
           </folding>
         </state>
Index: /other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java
===================================================================
--- /other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java	(revision 33443)
+++ /other-projects/is-sheet-music-encore/trunk/image-identification-dev-02/image-identification-development/src/MainMorph.java	(revision 33444)
@@ -63,19 +63,12 @@
 import org.opencv.imgcodecs.Imgcodecs;
 import org.opencv.imgproc.Imgproc;
-import org.opencv.imgproc.Moments;
-import org.opencv.osgi.OpenCVNativeLoader;
-import org.opencv.photo.Photo;
-
+
+import static org.opencv.core.Core.FILLED;
 import static org.opencv.core.CvType.CV_8UC3;
 import static org.opencv.highgui.HighGui.imshow;
 import static org.opencv.imgcodecs.Imgcodecs.imwrite;
-import java.awt.image.BufferedImage;
-import java.awt.image.DataBufferByte;
+
 import java.io.File;
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
-import javax.imageio.ImageIO;
 
 //REFERENCES:
@@ -96,4 +89,5 @@
 //http://androiderstuffs.blogspot.com/2016/06/detecting-rectangle-using-opencv-java.html
 //https://stackoverflow.com/questions/23327502/opencv-how-to-draw-minarearect-in-java
+//https://stackoverflow.com/questions/30056910/opencv-java-modify-pixel-values
 
 
@@ -115,4 +109,7 @@
 
 public class MainMorph {
+    //CODE VERSIONS
+    static int CODE_VERSION  = 8;
+    static int IMAGE_VERSION = 3;
     //GLOBAL_CONSTANTS
 
@@ -122,64 +119,33 @@
 
     //
-    static class StartAndEndPoint {
-        //PRIVATES
-        private Point _p1;
-        private Point _p2;
-        //CONSTRUCTOR
-        public StartAndEndPoint(Point p1, Point p2){
-            _p1 = p1;
-            _p2 = p2;
-        }
-        //GETTERS
-        public Point getP1(){
-            return _p1;
-        }
-        public Point getP2(){
-            return  _p2;
-        }
-        //SETTERS
-        public void setP1(Point p1){
-            _p1 = p1;
-        }
-        public void setP2(Point p2){
-            _p2 = p2;
-        }
-
-        //ToString
-        public  String toString(){
-            return "Start: " + _p1 + " End: " + _p2;
-        }
-
-    }
-    private static  BufferedImage toBufferedImage(Mat mat){
-        //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-        //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-        //https://riptutorial.com/opencv/example/21963/converting-an-mat-object-to-an-bufferedimage-object
-        try{
-            int type = BufferedImage.TYPE_3BYTE_BGR;
-            int bufferSize = mat.channels() * mat.cols() * mat.rows();
-            byte[] b = new byte[bufferSize];
-            //get all the pixels
-            mat.get(0, 0, b);
-            BufferedImage image = new BufferedImage(mat.cols(), mat.rows(), type);
-            final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
-            System.arraycopy(b, 0, targetPixels, 0, b.length);
-            return image;
-        }
-        catch(Exception e){
-            System.err.println(e);
-        }
-        return null;
-    }
-
-    private static void showWaitDestroy(String winname, Mat img) {
+
+    private static void imageViewer(String winName, Mat img) {
         try {
-            HighGui.namedWindow(winname, HighGui.WINDOW_AUTOSIZE);
-            imshow(winname, img);
-             HighGui.resizeWindow(winname, 1000, 1000);
-            HighGui.moveWindow(winname, 500, 0);
-            HighGui.waitKey(0);
-
-            HighGui.destroyWindow(winname);
+            //Internal display - Overview - Will Distort High Res images
+            if(IMAGE_VERSION == 1) {
+                HighGui.namedWindow(winName, HighGui.WINDOW_NORMAL);
+                HighGui.resizeWindow(winName, 1000, 1000);
+                imshow(winName, img);
+
+                HighGui.moveWindow(winName, 500, 0);
+                HighGui.waitKey(0);
+
+                HighGui.destroyWindow(winName);
+            }
+            //Internal display - Segmented - Will _NOT_ Distort High Res images
+            if(IMAGE_VERSION == 2) {
+                HighGui.namedWindow(winName, HighGui.WINDOW_AUTOSIZE);
+                HighGui.resizeWindow(winName, 1000, 1000);
+                imshow(winName, img);
+                HighGui.moveWindow(winName, 500, 0);
+                HighGui.waitKey(0);
+
+                HighGui.destroyWindow(winName);
+            }
+            //External display - Save Images for manual viewing
+            if(IMAGE_VERSION == 3) {
+                //save file (testing purposes)
+                imwrite(winName+".jpg", img);
+            }
         }
         catch (Exception e){
@@ -189,14 +155,8 @@
     //MAIN
     public static void main(String[] args) {
-
         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
-
         try {
-
-            ArrayList<StartAndEndPoint> pointArrayList = new ArrayList<>();
-
             //Variables
-            int codeVersion = 4;
-            System.out.println("Running code version: " + codeVersion);
+            System.out.println("Running code version: " + CODE_VERSION);
             Mat edgesDetected = new Mat();
             Mat mid = new Mat();
@@ -218,5 +178,8 @@
             //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t2q53nq6w-6.png";
             //String default_file =hiresDirectory+"BK/NotSheetMusic/aeu.ark+=13960=t9z03w65z-4.png";
-            String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
+            //String default_file =hiresDirectory+"MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
+            //String default_file =hiresDirectory+"MU/SheetMusic/emu.010001066823-9.png";
+            //String default_file =hiresDirectory+"MU/NotSheetMusic/mdp.39015096363935-1.png";
+            String default_file =hiresDirectory+"MU/SheetMusic/coo.31924062612282-9.png";
 
             //System.out.println(default_file);
@@ -236,4 +199,5 @@
             // Load an image
             Mat original1 = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
+            System.out.println("Width: " + original1.width() + " Height: " + original1.height());
             Mat original = original1.clone();
             
@@ -251,5 +215,5 @@
 
             //dynamic morphology??
-            if(codeVersion == 1) {
+            if(CODE_VERSION == 1) {
                 int hori = original.width();
                 int vert = original.height();
@@ -261,5 +225,5 @@
 
                 Mat test = original.clone();
-                showWaitDestroy("Original", test);
+                imageViewer("Original", test);
 
                 System.out.println("hori: " + hori + '\t' + "vert: " + vert);
@@ -268,67 +232,67 @@
                 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX, (sizeY/100)));
                 Imgproc.erode(test,test,kernelErode);
-                showWaitDestroy("01 Erode", test);
+                imageViewer("01 Erode", test);
 
                 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX,(sizeY/10)));
                 Imgproc.dilate(test, test, kernelDialate);
-                showWaitDestroy("02 Dialate", test);
+                imageViewer("02 Dialate", test);
 
                 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/5)));
                 Imgproc.erode(test,test,kernelErodeAgain);
-                showWaitDestroy(" 03 Erode Again", test);
+                imageViewer(" 03 Erode Again", test);
 
                 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10)*3,(sizeY/10)*3));
                 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
-                showWaitDestroy("04 Close", test);
+                imageViewer("04 Close", test);
 
                 Imgproc.adaptiveThreshold(test, test,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
-                showWaitDestroy("05 Binarized", test);
+                imageViewer("05 Binarized", test);
 
                 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/20)));
                 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
-                showWaitDestroy(" 06 Open", test);
+                imageViewer(" 06 Open", test);
 
                 Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/5),(sizeY/100)));
                 Imgproc.dilate(test, test, kernelDialateAgain);
-                showWaitDestroy("07 Dialate", test);
+                imageViewer("07 Dialate", test);
 
 
                 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((sizeX/10),(sizeY/2)));
                 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
-                showWaitDestroy(" 08 Close Again (Final)", test);
+                imageViewer(" 08 Close Again (Final)", test);
             }
             //Successful hardcode for morhpology
-            if (codeVersion == 2) {
+            if (CODE_VERSION == 2) {
 
                 //MAKE SURE BLACK & WHITE
                 Mat test = original.clone();
-                showWaitDestroy("00 Binarized Original", test);
+                imageViewer("00 Binarized Original", test);
 
                 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
                 Imgproc.erode(test,test,kernelErode);
-                showWaitDestroy("01 Erode", test);
+                imageViewer("01 Erode", test);
 
                 Mat kernelDialate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
                 Imgproc.dilate(test, test, kernelDialate);
-                showWaitDestroy("02 Dialate", test);
+                imageViewer("02 Dialate", test);
 
                 Mat kernelErodeAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,20));
                 Imgproc.erode(test,test,kernelErodeAgain);
-                showWaitDestroy(" 03 Erode Again", test);
+                imageViewer(" 03 Erode Again", test);
 
                 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(35,20));
                 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelClose);
-                showWaitDestroy("04 Close", test);
+                imageViewer("04 Close", test);
 
 //                Imgproc.threshold(test,test, 127, 255, Imgproc.THRESH_BINARY);
-//                showWaitDestroy("05 Binarized", test);
+//                imageViewer("05 Binarized", test);
 
                 Mat kernelOpen = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
                 Imgproc.morphologyEx(test,test,Imgproc.MORPH_OPEN, kernelOpen);
-                showWaitDestroy(" 06 Open", test);
+                imageViewer(" 06 Open", test);
 
 //                Mat kernelDialateAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,10));
 //                Imgproc.dilate(test, test, kernelDialateAgain);
-//                showWaitDestroy("07 Dialate", test);
+//                imageViewer("07 Dialate", test);
 
                 //FIGURE OUT FLOOD FILL!!
@@ -338,9 +302,9 @@
                 Mat kernelCloseAgain = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,50));
                 Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, kernelCloseAgain);
-                showWaitDestroy(" 08 Close Again (Final)", test);
+                imageViewer(" 08 Close Again (Final)", test);
 
             }
             //Tutorial/Demo Code
-            if (codeVersion == 3) {
+            if (CODE_VERSION == 3) {
                 Mat horizontal = original.clone();
                 Mat vertical = original.clone();
@@ -353,5 +317,5 @@
                 Imgproc.dilate(horizontal, horizontal, horizontalStructure);
                 // Show extracted horizontal lines
-                showWaitDestroy("horizontal", horizontal);
+                imageViewer("horizontal", horizontal);
                 // Specify size on vertical axis
                 int vertical_size = vertical.rows() / 30;
@@ -362,8 +326,8 @@
                 Imgproc.dilate(vertical, vertical, verticalStructure);
                 // Show extracted vertical lines
-                showWaitDestroy("vertical", vertical);
+                imageViewer("vertical", vertical);
                 // Inverse vertical image
                 Core.bitwise_not(vertical, vertical);
-                showWaitDestroy("vertical_bit", vertical);
+                imageViewer("vertical_bit", vertical);
                 // Extract edges and smooth image according to the logic
                 // 1. extract edges
@@ -375,9 +339,9 @@
                 Mat edges = new Mat();
                 Imgproc.adaptiveThreshold(vertical, edges, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 3, -2);
-                showWaitDestroy("edges", edges);
+                imageViewer("edges", edges);
                 // Step 2
                 Mat kernel = Mat.ones(2, 2, CvType.CV_8UC1);
                 Imgproc.dilate(edges, edges, kernel);
-                showWaitDestroy("dilate", edges);
+                imageViewer("dilate", edges);
                 // Step 3
                 Mat smooth = new Mat();
@@ -388,16 +352,18 @@
                 smooth.copyTo(vertical, edges);
                 // Show final result
-                showWaitDestroy("smooth - final", vertical);
+                imageViewer("smooth - final", vertical);
                 System.exit(0);
             }
             //Better morphology attempt - static
-            if(codeVersion ==4) {
+            if(CODE_VERSION ==4) {
 
                 //Display Original
-                showWaitDestroy("original", original1);
+                imageViewer("original", original1);
 
                 Mat test = original.clone();
-
-                showWaitDestroy("00 Inverse Binarized Original", test);
+                Mat pre = original.clone();
+                Mat dst = new Mat();
+
+                imageViewer("00 Inverse Binarized Original", test);
 
                 //remove large items of no interest pre proc
@@ -411,15 +377,45 @@
                 //denoize
                 Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
-                Imgproc.morphologyEx(test,test, Imgproc.MORPH_OPEN, denoize);
-                showWaitDestroy("Denoize", test);
+                Imgproc.morphologyEx(pre,pre, Imgproc.MORPH_OPEN, denoize);
+                imageViewer("Denoize - PRE", pre);
 
                 //close up gaps
-                Mat gapcloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
-                Imgproc.morphologyEx(test,test,Imgproc.MORPH_CLOSE, gapcloser);
-                showWaitDestroy("gap closer", test);
-
-                Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 50));
-                Imgproc.erode(test,test, kernelRemoveLarge);
-                showWaitDestroy("Remove Large", test);
+                Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
+                Imgproc.morphologyEx(pre,pre,Imgproc.MORPH_CLOSE, gapCloser);
+                imageViewer("gap closer - PRE", pre);
+
+                Mat kernelHighlightLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10, 10));
+                Imgproc.erode(pre,pre, kernelHighlightLarge);
+                imageViewer("Highlight Large - PRE", pre);
+
+                //change white pixels to red
+                ArrayList<MatOfPoint> contoursPre = new ArrayList<MatOfPoint>();
+                Mat hierarchyPre = new Mat();
+
+                //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
+                //(contours)  output array of arrays: Detected contours. Each contour is stored as a vector of points
+                //(hierarchy) output array:           Optional output vector, containing information about the image topology.
+                //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
+
+                Imgproc.findContours(pre, contoursPre, hierarchyPre, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+                //Draw contours and record areas
+
+                Mat drawingPre = Mat.zeros(test.size(), CvType.CV_8UC3);
+                Mat mask = new Mat(drawingPre.size(), CV_8UC3,new Scalar(0));
+                Scalar colorPre = new Scalar(255, 255, 255);
+                Imgproc.drawContours(mask, contoursPre, -1, new Scalar(255, 255, 255), FILLED);
+                Imgproc.fillPoly(drawingPre, contoursPre,colorPre);
+                imageViewer("DRAWINGPRE", drawingPre);
+
+
+                //FIIIIIIIIIIIIIIIIIIIIIIIIIIXXXXXXXXXXXX
+                //Remove from main Mat
+                Core.bitwise_not(mask,mask);
+                imageViewer("MASK", mask);
+                imageViewer("TEST", test);
+
+                drawingPre.copyTo(test, mask);
+                imageViewer("COMBINE", test);
 
                 //start staff line detection
@@ -427,17 +423,17 @@
                 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
                 Imgproc.erode(test,test,kernelErode);
-                showWaitDestroy("01 Erode", test);
+                imageViewer("01 Erode plus pre", test);
 
                 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
                 Imgproc.dilate(test,test,kernelDilate);
-                showWaitDestroy("02 Dilate", test);
+                imageViewer("02 Dilate", test);
 
                 Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
                 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
-                showWaitDestroy("03 Open", test);
+                imageViewer("03 Open", test);
 
                 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
                 Imgproc.erode(test,test,kernelErode02);
-                showWaitDestroy("04 Erode (Final)", test);
+                imageViewer("04 Erode (Final)", test);
 
 
@@ -475,8 +471,8 @@
 
                 //Show in a window
-                showWaitDestroy("Contours", drawing);
+                imageViewer("Contours", drawing);
             }
             //Better morphology attempt - dynamic
-            if(codeVersion ==5) {
+            if(CODE_VERSION ==5) {
                 int hori = original.width();
                 int vert = original.height();
@@ -494,30 +490,30 @@
                 System.out.println(hori + " " + vert + " "  + sizeX1 + " " + sizeY1);
                 //Display Original
-                showWaitDestroy("original", original1);
+                imageViewer("original", original1);
 
                 Mat test = original.clone();
-                showWaitDestroy("00 Inverse Binarized Original", test);
+                imageViewer("00 Inverse Binarized Original", test);
 
                 //Remove very large and wide black spaces (8th of the page)
                 //Mat kernelRemoveLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(hori/8, vert/8));
                 //Imgproc.erode(test,test, kernelRemoveLarge);
-                //showWaitDestroy("Remove Large", test);
+                //imageViewer("Remove Large", test);
 
                 //Eliminate things that are not long and thin
                 Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10,sizeY1)); //new Size(10,1));
                 Imgproc.erode(test,test,kernelErode);
-                showWaitDestroy("01 Erode", test);
+                imageViewer("01 Erode", test);
 
                 Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX10*2,sizeY1*3)); //new Size(20,3));
                 Imgproc.dilate(test,test,kernelDilate);
-                showWaitDestroy("02 Dilate", test);
+                imageViewer("02 Dilate", test);
 
                 Mat kernelClose = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*4,sizeY1*4)); //new Size(4,4));
                 Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelClose);
-                showWaitDestroy("03 Open", test);
+                imageViewer("03 Open", test);
 
                 Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(sizeX1*8,sizeX1*8)); //new Size(8,8));
                 Imgproc.erode(test,test,kernelErode02);
-                showWaitDestroy("04 Erode (Final)", test);
+                imageViewer("04 Erode (Final)", test);
 
 
@@ -554,7 +550,220 @@
 
                 //Show in a window
-                showWaitDestroy("Contours", drawing);
-            }
-            //ADD PRE PROC
+                imageViewer("Contours", drawing);
+            }
+            //MASK UNDERSTANDING
+            if(CODE_VERSION ==6) {
+                String path ="/Scratch/cpb16/is-sheet-music-encore/hires-download-images/MU/NotSheetMusic/aeu.ark+=13960=t0dv28v9r-1.png";
+                Mat mask = new Mat();
+                Mat dst = new Mat();
+                //Get source image and binerize
+                Mat src = Imgcodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE);
+                Imgproc.adaptiveThreshold(original1, src,255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,Imgproc.THRESH_BINARY_INV, 15, THRESHOLD_C);
+                imageViewer("src", src);
+
+                //Find unwanted material, then invert it so mask removes not keeps.
+                Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(100,1));
+                Imgproc.erode(src,mask,kernelErode);
+                Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(110,10));
+                Imgproc.dilate(mask, mask, kernelDilate);
+                Core.bitwise_not(mask,mask);
+                imageViewer("mask", mask);
+
+                //Copy source image to new Mat, with mask in use
+                src.copyTo(dst, mask);
+                imageViewer("dst", dst);
+
+
+
+
+            }
+            //Mask implementation
+            if(CODE_VERSION ==7) {
+
+                //Display Original
+                imageViewer("original", original1);
+
+                Mat src = original.clone();
+                Mat test = original.clone();
+                Mat mask = new Mat();
+                Mat dst = new Mat();
+
+                imageViewer("00 Inverse Binarized Original", src);
+
+                //denoize
+                Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
+                Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
+                imageViewer("01 Denoize - mask", mask);
+
+                //close up gaps
+                Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
+                Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
+                imageViewer("02 gap closer - mask", mask);
+
+                //Isolate large items
+                Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
+                Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
+                imageViewer("03 Isolate Large - mask", mask);
+                Core.bitwise_not(mask,mask);
+
+                //Remove unwanted large items from image
+                src.copyTo(dst, mask);
+                imageViewer("04 Large Items Removed", dst);
+
+                //start staff line detection
+
+                Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,1));
+                Imgproc.erode(dst,test,kernelErode);
+                imageViewer("11 Erode plus pre", test);
+
+                Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20,3));
+                Imgproc.dilate(test,test,kernelDilate);
+                imageViewer("12 Dilate", test);
+
+                Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(4,4));
+                Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
+                imageViewer("13 Open", test);
+
+                Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8,8));
+                Imgproc.erode(test,test,kernelErode02);
+                imageViewer("14 Erode (Final)", test);
+
+
+                //DETECT OUTLINE AND FIND AREA OF THESE LINES.
+                ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
+                Mat hierarchy = new Mat();
+
+                //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
+                //(contours)  output array of arrays: Detected contours. Each contour is stored as a vector of points
+                //(hierarchy) output array:           Optional output vector, containing information about the image topology.
+                //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
+
+                Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+                //Draw contours and record areas
+                Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
+                int areaCounter = 0;
+
+
+                Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), FILLED);
+//                for (int i = 0; i < contours.size(); i++) {
+//                    Scalar color = new Scalar(0, i, i);
+//                    double area = Imgproc.contourArea(contours.get(i));
+//                    Imgproc.drawContours(drawing, contours, i, color, FILLED);
+//                    System.out.println("AREA: " + area);
+//
+//                }
+                imageViewer("Contours found", drawing);
+
+                //Classifier Calculation
+                if(areaCounter >= THRESHOLD_AREA_COUNT){
+                    System.out.println("THIS IS SHEET MUSIC");
+                    System.out.println(areaCounter);
+                }
+
+
+            }
+            //Mask implementation - HIGH RES NUMBER MOD
+            if(CODE_VERSION ==8) {
+
+                //Display Original
+                imageViewer("original", original1);
+
+                Mat src = original.clone();
+                Mat test = original.clone();
+                Mat mask = new Mat();
+                Mat dst = new Mat();
+
+                imageViewer("00 Inverse Binarized Original", src);
+
+                //Close then Open
+
+//                Mat firstKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
+//                Imgproc.morphologyEx(src, mask, Imgproc.MORPH_CLOSE, firstKernel);
+//                imageViewer("01 Closed - mask", mask);
+//
+//                Mat secondKernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,10));
+//                Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, secondKernel);
+//                imageViewer("02 Open - mask", mask);
+
+
+                //denoize
+                Mat denoize = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
+                Imgproc.morphologyEx(src,mask, Imgproc.MORPH_OPEN, denoize);
+                imageViewer("01 Denoize - mask", mask);
+
+                //close up gaps
+                Mat gapCloser = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
+                Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_CLOSE, gapCloser);
+                imageViewer("02 gap closer - mask", mask);
+
+                //Isolate large items
+                Mat isolateLarge = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(8, 8));
+                Imgproc.morphologyEx(mask,mask,Imgproc.MORPH_OPEN, isolateLarge);
+                imageViewer("03 Isolate Large - mask", mask);
+                Core.bitwise_not(mask,mask);
+
+                //Remove unwanted large items from image
+                src.copyTo(dst, mask);
+                imageViewer("04 Large Items Removed", dst);
+
+                //start staff line detection
+
+                Mat kernelErode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(15,2)); //10,2
+                Imgproc.erode(dst,test,kernelErode);
+                imageViewer("11 Erode plus pre", test);
+
+                Mat kernelDilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //20,3
+                Imgproc.dilate(test,test,kernelDilate);
+                imageViewer("12 Dilate", test);
+
+                Mat kernelOpening = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //4,4
+                Imgproc.morphologyEx(test, test, Imgproc.MORPH_CLOSE, kernelOpening);
+                imageViewer("13 Open", test);
+
+                Mat kernelErode02 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(10,4)); //10,1
+                Imgproc.erode(test,test,kernelErode02);
+                imageViewer("14 Erode (Final)", test);
+
+
+                //DETECT OUTLINE AND FIND AREA OF THESE LINES.
+                ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
+                Mat hierarchy = new Mat();
+
+                //PARAMETERS: input image, output array of arrays, output array, contour retrieval mode, contour approximation method.
+                //(contours)  output array of arrays: Detected contours. Each contour is stored as a vector of points
+                //(hierarchy) output array:           Optional output vector, containing information about the image topology.
+                //https://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga17ed9f5d79ae97bd4c7cf18403e1689a
+
+                Imgproc.findContours(test, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+
+                //Draw contours and record areas
+                Mat drawing = Mat.zeros(test.size(), CvType.CV_8UC3);
+                int areaCounter = 0;
+
+                //Have created a preprocess to remove large objects.
+                //Need to now finalized Classifier, re try area detection.
+                //Paths to take - rectangle boxes around detected contours over threshold (area or perimeter)
+                //Just use area and periemter to determine if sheet music
+                //Discuss with david before weekend perhaps?
+
+                Imgproc.drawContours(drawing, contours, -1, new Scalar(0, 255, 0), 1); //USES LINE_8
+//                   for (int i = 0; i < contours.size(); i++) {
+//                    Scalar color = new Scalar(0, i, i);
+//                    double area = Imgproc.contourArea(contours.get(i));
+//                    Imgproc.drawContours(drawing, contours, i, color, FILLED);
+//                    System.out.println("AREA: " + area);
+//
+//                }
+                imageViewer("Contours found", drawing);
+
+                //Classifier Calculation
+                if(areaCounter >= THRESHOLD_AREA_COUNT){
+                    System.out.println("THIS IS SHEET MUSIC");
+                    System.out.println(areaCounter);
+                }
+
+
+            }
 
             //USE stuc element, to rule out large wide and long pieces of black and white.
