Manage to translate most Team 254 code into java
[3501/OpenCVShowImage] / IsolateImage.java
CommitLineData
c724deb3 1import java.util.ArrayList;
f72f3818
E
2import java.util.List;
3import java.util.Vector;
c724deb3 4
0e9212ae 5import org.opencv.core.Core;
f72f3818 6import org.opencv.core.CvType;
fb24aa85 7import org.opencv.core.Mat;
f72f3818 8import org.opencv.core.MatOfInt;
c724deb3 9import org.opencv.core.MatOfPoint;
a5abe89b 10import org.opencv.core.Point;
0e9212ae 11import org.opencv.core.Scalar;
a5abe89b 12import org.opencv.imgproc.Imgproc;
0e9212ae 13
bf95a16f 14public class IsolateImage {
0e9212ae 15
f72f3818
E
16 public enum DisplayMode {
17 DISP_MODE_RAW, DISP_MODE_THRESH, DISP_MODE_TARGETS, DISPL_MODE_TARGETS_PLUS;
0e9212ae
E
18 }
19
f72f3818
E
20 public class TargetInfo {
21 double centroid_x;
22 double centroid_y;
23 double width;
24 double height;
0e1260e3 25
f72f3818 26 Vector<Point> points;
c621c378
E
27 }
28
f72f3818
E
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) {
877f5b61 31
f72f3818
E
32 Mat input = new Mat();
33 input.create(h, w, CvType.CV_8UC4);
877f5b61 34
f72f3818
E
35 Mat hsv = new Mat();
36 Imgproc.cvtColor(input, hsv, Imgproc.COLOR_RGBA2RGB);
37 Imgproc.cvtColor(hsv, hsv, Imgproc.COLOR_RGB2HSV);
877f5b61 38
f72f3818
E
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);
877f5b61 41
f72f3818
E
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);
c621c378 51
f72f3818
E
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);
877f5b61 56
877f5b61
E
57 }
58
7a245e8b 59 }
0e9212ae 60}