import java.util.ArrayList;
-import java.util.Iterator;
+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.Rect;
import org.opencv.core.Scalar;
-import org.opencv.core.Size;
-import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class IsolateImage {
- public static Mat matOriginal;
- public static Mat filtered;
- public static Mat kernel;
- public static Mat matHierarchy;
-
- public static final Scalar LOWER_BOUNDS = new Scalar(80, 80, 80);
- public static final Scalar UPPER_BOUNDS = new Scalar(140, 180, 140);
- public static final Scalar BLACK = new Scalar(0, 0, 0);
- public static final Scalar GRAY = new Scalar(100, 100, 100);
-
- static {
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
+ public enum DisplayMode {
+ DISP_MODE_RAW, DISP_MODE_THRESH, DISP_MODE_TARGETS, DISPL_MODE_TARGETS_PLUS;
}
- public static void main(String args[]) throws Exception {
-
- matOriginal = new Mat();
- filtered = new Mat();
- matHierarchy = new Mat();
-
- kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3));
-
- findContours();
- }
-
- public static void findContours() {
-
- ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
- int frameCount = 0;
- int THE_BEST_MAGIC_NUMBER = 300;
-
- contours.clear();
- matOriginal = Imgcodecs.imread("imgs/goal2.png"); // convert image
- // into matrix
-
- filtered = matOriginal;
-
- Imgproc.cvtColor(matOriginal, filtered, Imgproc.COLOR_BGR2GRAY);
- // takes out unnecessary colors
-
- Imgproc.GaussianBlur(matOriginal, filtered, new Size(5, 5), Core.BORDER_DEFAULT);
-
- // bing bing bong bong
- // bing bing bong bong
-
- // converts image to grayscale, blurs it, and detects edges
-
- Imgproc.Canny(matOriginal, filtered, 35, 125);
-
- Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
-
- for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) {
- MatOfPoint matOfPoint = iterator.next();
- Rect rec = Imgproc.boundingRect(matOfPoint);
- if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) {
+ public class TargetInfo {
+ double centroid_x;
+ double centroid_y;
+ double width;
+ double height;
- iterator.remove();
- }
-
- }
- Rect largestRec = null;
- double largestRecArea = 0;
-
- for (MatOfPoint mop : contours) {
- double recArea = Imgproc.contourArea(mop);
- if (largestRecArea < recArea) {
- largestRec = Imgproc.boundingRect(mop);
- largestRecArea = recArea;
- }
- }
- Imgproc.rectangle(matOriginal, largestRec.br(), largestRec.tl(), GRAY, 1);
-
- Imgcodecs.imwrite("output.png", filtered);
-
- }
-
- public static double distanceToCamera(double knownWidth, int focalLength, double perWidth) {
- return (knownWidth * ((double) (focalLength))) / perWidth;
+ Vector<Point> points;
}
- public static void processImage() {
-
- ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
- int frameCount = 0;
- int THE_BEST_MAGIC_NUMBER = 300;
- while (frameCount < 1) {
- contours.clear();
- matOriginal = Imgcodecs.imread("imgs/goal2.png"); // convert image
- // into matrix
+ 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) {
- filtered = matOriginal;
+ Mat input = new Mat();
+ input.create(h, w, CvType.CV_8UC4);
- Imgproc.GaussianBlur(matOriginal, filtered, new Size(25, 25), Core.BORDER_DEFAULT);
+ Mat hsv = new Mat();
+ Imgproc.cvtColor(input, hsv, Imgproc.COLOR_RGBA2RGB);
+ Imgproc.cvtColor(hsv, hsv, Imgproc.COLOR_RGB2HSV);
- Core.inRange(matOriginal, LOWER_BOUNDS, UPPER_BOUNDS, filtered);
- Imgproc.erode(matOriginal, filtered, kernel, new Point(-1, -1), 5);
- Imgproc.dilate(matOriginal, filtered, kernel, new Point(-1, -1), 0);
- Imgproc.findContours(filtered, contours, matHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
+ Mat thresh = new Mat();
+ Core.inRange(hsv, new Scalar(h_min, s_min, v_min), new Scalar(h_max, s_max, v_max), thresh);
- for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) {
- MatOfPoint matOfPoint = iterator.next();
- Rect rec = Imgproc.boundingRect(matOfPoint);
- if (rec.height < THE_BEST_MAGIC_NUMBER || rec.width < THE_BEST_MAGIC_NUMBER) {
+ 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);
- iterator.remove();
- }
+ for (int i = 0; i < contours.size(); i++) {
+ MatOfPoint contour = contours.get(i);
+ convex_contour.clear();
+ Imgproc.convexHull(contour, convex_contour.get(i), false);
- }
-
- for (MatOfPoint mop : contours) {
- Rect rec = Imgproc.boundingRect(mop);
- Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), GRAY, 1);
- System.out.println(rec.toString());
- }
-
- frameCount++;
}
- Imgcodecs.imwrite("output.png", filtered);
-
- }
-
- /**
- * @param angle
- * a nonnormalized angle
- */
-
- public static double normalize360(double angle) {
- while (angle >= 360.0) {
- angle -= 360.0;
- }
- while (angle < 0.0) {
- angle += 360.0;
- }
- return angle;
}
}