implement erode to get rid of unwanted colors
[3501/OpenCVShowImage] / IsolateImage.java
index 0ee66c3ba9bdc752c9d7f80ac1675d2bc3ffc1ab..d2462da93c164557e00b634c794f757d7ea03117 100644 (file)
@@ -1,83 +1,46 @@
-import java.awt.Graphics;
-import java.awt.Panel;
-import java.awt.image.BufferedImage;
-import java.io.IOException;
-import java.net.URL;
-
-import javax.imageio.ImageIO;
-import javax.swing.JFrame;
-
 import org.opencv.core.Core;
 import org.opencv.core.Mat;
+import org.opencv.core.Point;
 import org.opencv.core.Scalar;
+import org.opencv.core.Size;
 import org.opencv.imgcodecs.Imgcodecs;
+import org.opencv.imgproc.Imgproc;
 
-public class IsolateImage extends Panel {
-       BufferedImage image;
+public class IsolateImage {
 
        public static Mat matOriginal;
        public static Mat filtered;
+       public static Mat kernel;
 
-       public static final Scalar LOWER_BOUNDS = new Scalar(103, 164, 169);
-       public static final Scalar UPPER_BOUNDS = new Scalar(125, 229, 255);
+       public static final Scalar LOWER_BOUNDS = new Scalar(169, 164, 103);
+       public static final Scalar UPPER_BOUNDS = new Scalar(255, 229, 140);
+       // public static final Scalar LOWER_BOUNDS = new Scalar(255, 0, 0);
+       // public static final Scalar UPPER_BOUNDS = new Scalar(0, 255, 255);
 
        static {
                System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        }
 
-       public IsolateImage() {
-               try {
-                       // image file
-                       // name is
-                       // testpicture.png
-                       URL input = this.getClass().getResource(("imgs/testpicture.png"));
-                       // this will read the image to save
-                       // into a BufferedImage variable
-                       image = ImageIO.read(input);
-               } catch (IOException ie) {
-                       // this is to spill
-                       // out a message
-                       // saying
-                       // something
-                       // doesn't work
-                       System.out.println("Error: " + ie.getMessage());
-               }
-       }
-
-       public void paint(Graphics g) {
-               // This is to actually draw the image out,
-               // necessary.
-
-               // Input of a bufferedimage, x, y, then
-               // an observer in this case null.
-               g.drawImage(image, 0, 0, null);
-       }
-
        public static void main(String args[]) throws Exception {
 
                matOriginal = new Mat();
                filtered = new Mat();
 
-               // Creates a window to
-               // display the image
-               JFrame frame = new JFrame("Display image");
-
-               // Gets the image from ShowImage() method
-               Panel panel = new IsolateImage();
-
-               // Adds the image to the new window
-               // panel
-               frame.getContentPane().add(panel);
+               kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
 
-               // sets the size of the panel
-               frame.setSize(500, 500);
-               // makes it visible
-               frame.setVisible(true);
+               processImage();
        }
 
        public static void processImage() {
                matOriginal = Imgcodecs.imread("imgs/testpicture.png"); // convert image
                                                                                                                                // into matrix
 
+               filtered = matOriginal;
+
+               Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered);
+               Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 2);
+
+               Imgcodecs.imwrite("output.png", filtered);
+
        }
 }