Manage to translate most Team 254 code into java
[3501/OpenCVShowImage] / IsolateImage.java
index fb96b49b2888f8deea618cd4c935cd74ccac1680..fd1cb0c8199693e28bcaffacabdd9181e38b8019 100644 (file)
@@ -1,74 +1,60 @@
-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 java.util.ArrayList;
+import java.util.List;
+import java.util.Vector;
 
 import org.opencv.core.Core;
+import org.opencv.core.CvType;
 import org.opencv.core.Mat;
+import org.opencv.core.MatOfInt;
+import org.opencv.core.MatOfPoint;
+import org.opencv.core.Point;
 import org.opencv.core.Scalar;
+import org.opencv.imgproc.Imgproc;
 
-public class IsolateImage extends Panel {
-       BufferedImage image;
-
-       public static Mat matOriginal;
+public class IsolateImage {
 
-       public static final Scalar LOWER_BOUNDS = new Scalar(103, 164, 169);
-       public static final Scalar UPPER_BOUNDS = new Scalar(125, 229, 255);
-
-       static {
-               System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
+       public enum DisplayMode {
+               DISP_MODE_RAW, DISP_MODE_THRESH, DISP_MODE_TARGETS, DISPL_MODE_TARGETS_PLUS;
        }
 
-       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 class TargetInfo {
+               double centroid_x;
+               double centroid_y;
+               double width;
+               double height;
+
+               Vector<Point> points;
        }
 
-       public void paint(Graphics g) {
-               // This is to actually draw the image out,
-               // necessary.
+       public Vector<TargetInfo> processImpl(int w, int h, int texOut, DisplayMode mode, int h_min, int h_max, int s_min,
+                       int s_max, int v_min, int v_max) {
 
-               // Input of a bufferedimage, x, y, then
-               // an observer in this case null.
-               g.drawImage(image, 0, 0, null);
-       }
+               Mat input = new Mat();
+               input.create(h, w, CvType.CV_8UC4);
 
-       public static void main(String args[]) throws Exception {
+               Mat hsv = new Mat();
+               Imgproc.cvtColor(input, hsv, Imgproc.COLOR_RGBA2RGB);
+               Imgproc.cvtColor(hsv, hsv, Imgproc.COLOR_RGB2HSV);
 
-               matOriginal = new Mat();
+               Mat thresh = new Mat();
+               Core.inRange(hsv, new Scalar(h_min, s_min, v_min), new Scalar(h_max, s_max, v_max), thresh);
 
-               // Creates a window to
-               // display the image
-               JFrame frame = new JFrame("Display image");
+               Mat contour_input = new Mat();
+               contour_input = thresh.clone();
+               List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
+               Vector<MatOfInt> convex_contour = new Vector<MatOfInt>();
+               Vector<Point> poly = new Vector<Point>();
+               Vector<TargetInfo> targets = new Vector<TargetInfo>();
+               Vector<TargetInfo> rejected_targets = new Vector<TargetInfo>();
+               Imgproc.findContours(contour_input, contours, contour_input, Imgproc.RETR_EXTERNAL,
+                               Imgproc.CHAIN_APPROX_TC89_KCOS);
 
-               // Gets the image from ShowImage() method
-               Panel panel = new IsolateImage();
+               for (int i = 0; i < contours.size(); i++) {
+                       MatOfPoint contour = contours.get(i);
+                       convex_contour.clear();
+                       Imgproc.convexHull(contour, convex_contour.get(i), false);
 
-               // Adds the image to the new window
-               // panel
-               frame.getContentPane().add(panel);
+               }
 
-               // sets the size of the panel
-               frame.setSize(500, 500);
-               // makes it visible
-               frame.setVisible(true);
        }
 }