| 1 | import java.util.ArrayList; |
| 2 | import java.util.List; |
| 3 | import java.util.Vector; |
| 4 | |
| 5 | import org.opencv.core.Core; |
| 6 | import org.opencv.core.CvType; |
| 7 | import org.opencv.core.Mat; |
| 8 | import org.opencv.core.MatOfInt; |
| 9 | import org.opencv.core.MatOfPoint; |
| 10 | import org.opencv.core.Point; |
| 11 | import org.opencv.core.Scalar; |
| 12 | import org.opencv.imgproc.Imgproc; |
| 13 | |
| 14 | public class IsolateImage { |
| 15 | |
| 16 | public enum DisplayMode { |
| 17 | DISP_MODE_RAW, DISP_MODE_THRESH, DISP_MODE_TARGETS, DISPL_MODE_TARGETS_PLUS; |
| 18 | } |
| 19 | |
| 20 | public class TargetInfo { |
| 21 | double centroid_x; |
| 22 | double centroid_y; |
| 23 | double width; |
| 24 | double height; |
| 25 | |
| 26 | Vector<Point> points; |
| 27 | } |
| 28 | |
| 29 | public Vector<TargetInfo> processImpl(int w, int h, int texOut, DisplayMode mode, int h_min, int h_max, int s_min, |
| 30 | int s_max, int v_min, int v_max) { |
| 31 | |
| 32 | Mat input = new Mat(); |
| 33 | input.create(h, w, CvType.CV_8UC4); |
| 34 | |
| 35 | Mat hsv = new Mat(); |
| 36 | Imgproc.cvtColor(input, hsv, Imgproc.COLOR_RGBA2RGB); |
| 37 | Imgproc.cvtColor(hsv, hsv, Imgproc.COLOR_RGB2HSV); |
| 38 | |
| 39 | Mat thresh = new Mat(); |
| 40 | Core.inRange(hsv, new Scalar(h_min, s_min, v_min), new Scalar(h_max, s_max, v_max), thresh); |
| 41 | |
| 42 | Mat contour_input = new Mat(); |
| 43 | contour_input = thresh.clone(); |
| 44 | List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); |
| 45 | Vector<MatOfInt> convex_contour = new Vector<MatOfInt>(); |
| 46 | Vector<Point> poly = new Vector<Point>(); |
| 47 | Vector<TargetInfo> targets = new Vector<TargetInfo>(); |
| 48 | Vector<TargetInfo> rejected_targets = new Vector<TargetInfo>(); |
| 49 | Imgproc.findContours(contour_input, contours, contour_input, Imgproc.RETR_EXTERNAL, |
| 50 | Imgproc.CHAIN_APPROX_TC89_KCOS); |
| 51 | |
| 52 | for (int i = 0; i < contours.size(); i++) { |
| 53 | MatOfPoint contour = contours.get(i); |
| 54 | convex_contour.clear(); |
| 55 | Imgproc.convexHull(contour, convex_contour.get(i), false); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | } |
| 60 | } |