List of usage examples for org.opencv.core Point Point
public Point(double x, double y)
From source file:org.openpnp.vision.FluentCv.java
License:Open Source License
public FluentCv convertCirclesToPoints(List<Point> points) { for (int i = 0; i < mat.cols(); i++) { double[] circle = mat.get(0, i); double x = circle[0]; double y = circle[1]; points.add(new Point(x, y)); }//from w w w. j a va2s . com return this; }
From source file:org.openpnp.vision.FluentCv.java
License:Open Source License
/** * Draw circles from the current Mat contained onto the Mat specified in baseTag using the * specified color, optionally storing the results in tag. The current Mat is replaced with the * Mat from baseTag with the circles drawn on top of it. * /*from w ww. j a v a2 s . c om*/ * @param baseTag * @param color * @param tag * @return */ public FluentCv drawCircles(String baseTag, Color color, String... tag) { Color centerColor = new HslColor(color).getComplementary(); Mat mat = get(baseTag); if (mat == null) { mat = new Mat(); } for (int i = 0; i < this.mat.cols(); i++) { double[] circle = this.mat.get(0, i); double x = circle[0]; double y = circle[1]; double radius = circle[2]; Core.circle(mat, new Point(x, y), (int) radius, colorToScalar(color), 2); Core.circle(mat, new Point(x, y), 1, colorToScalar(centerColor), 2); } return store(mat, tag); }
From source file:org.openpnp.vision.FluentCv.java
License:Open Source License
/** * Filter circles as returned from e.g. houghCircles to only those that are within maxDistance * of the best fitting line./*from w ww . ja v a 2 s. com*/ * * @param tag * @return */ public FluentCv filterCirclesToLine(double maxDistance, String... tag) { if (this.mat.cols() < 2) { return store(this.mat, tag); } List<Point> points = new ArrayList<>(); // collect the circles into a list of points for (int i = 0; i < this.mat.cols(); i++) { float[] circle = new float[3]; this.mat.get(0, i, circle); float x = circle[0]; float y = circle[1]; points.add(new Point(x, y)); } Point[] line = Ransac.ransac(points, 100, maxDistance); Point a = line[0]; Point b = line[1]; // filter the points by distance from the resulting line List<float[]> results = new ArrayList<>(); for (int i = 0; i < this.mat.cols(); i++) { float[] circle = new float[3]; this.mat.get(0, i, circle); Point p = new Point(circle[0], circle[1]); if (pointToLineDistance(a, b, p) <= maxDistance) { results.add(circle); } } // It really seems like there must be a better way to do this, but after hours // and hours of trying I can't find one. How the hell do you append an element // of 3 channels to a Mat?! Mat r = new Mat(1, results.size(), CvType.CV_32FC3); for (int i = 0; i < results.size(); i++) { r.put(0, i, results.get(i)); } return store(r, tag); }
From source file:org.sahyagiri.rpi.opencv.DetectCar.java
License:Mozilla Public License
public final Point findCentreOfMass(final Mat inputRGB, final Scalar carColorThresholdHSVLow, final Scalar carColorThresholdHSVHigh) { Mat hsvImage = new Mat(); Imgproc.cvtColor(inputRGB, hsvImage, Imgproc.COLOR_RGB2HSV); Mat outputHSV = new Mat(); Core.inRange(hsvImage, carColorThresholdHSVLow, carColorThresholdHSVHigh, outputHSV); Imgproc.morphologyEx(outputHSV, outputHSV, Imgproc.MORPH_ERODE, new Mat()); Imgproc.GaussianBlur(outputHSV, outputHSV, new Size(3, 3), 3); Imgproc.threshold(outputHSV, outputHSV, 50, 255, Imgproc.THRESH_BINARY); ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(outputHSV, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); MatOfPoint contourPoints = combineContourPoints(contours); Moments moments = Imgproc.moments(contourPoints); int x = (int) (moments.get_m10() / moments.get_m00()); int y = (int) (moments.get_m01() / moments.get_m00()); final Point centreOfMass = new Point(x, y); return centreOfMass; }
From source file:org.technowolves.vision.TowerTracker.java
License:Open Source License
/** * /*from w w w .j a v a2s . co m*/ * reads an image from a live image capture and outputs information to the SmartDashboard or a file */ public static void processImage() { ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); double x, y, targetX, targetY, distance, azimuth; // frame counter int FrameCount = 0; long before = System.currentTimeMillis(); // only run for the specified time while (FrameCount < 100) { contours.clear(); // capture from the axis camera videoCapture.read(matOriginal); // captures from a static file for testing // matOriginal = Imgcodecs.imread("someFile.png"); Imgproc.cvtColor(matOriginal, matHSV, Imgproc.COLOR_BGR2HSV); Core.inRange(matHSV, LOWER_BOUNDS, UPPER_BOUNDS, matThresh); Imgproc.findContours(matThresh, contours, matHeirarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // make sure the contours that are detected are at least 20x20 // pixels with an area of 400 and an aspect ration greater then 1 for (Iterator<MatOfPoint> iterator = contours.iterator(); iterator.hasNext();) { MatOfPoint matOfPoint = (MatOfPoint) iterator.next(); Rect rec = Imgproc.boundingRect(matOfPoint); if (rec.height < 25 || rec.width < 25) { iterator.remove(); continue; } float aspect = (float) rec.width / (float) rec.height; if (aspect < 1.0) iterator.remove(); } for (MatOfPoint mop : contours) { Rect rec = Imgproc.boundingRect(mop); Imgproc.rectangle(matOriginal, rec.br(), rec.tl(), BLACK); } // if there is only 1 target, then we have found the target we want if (contours.size() == 1) { Rect rec = Imgproc.boundingRect(contours.get(0)); // "fun" math brought to you by miss daisy (team 341)! y = rec.br().y + rec.height / 2; y = -((2 * (y / matOriginal.height())) - 1); distance = (TOP_TARGET_HEIGHT - TOP_CAMERA_HEIGHT) / Math.tan((y * VERTICAL_FOV / 2.0 + CAMERA_ANGLE) * Math.PI / 180); // angle to target...would not rely on this targetX = rec.tl().x + rec.width / 2; targetX = (2 * (targetX / matOriginal.width())) - 1; azimuth = normalize360(targetX * HORIZONTAL_FOV / 2.0 + 0); // drawing info on target Point center = new Point(rec.br().x - rec.width / 2 - 15, rec.br().y - rec.height / 2); Point centerw = new Point(rec.br().x - rec.width / 2 - 15, rec.br().y - rec.height / 2 - 20); Imgproc.putText(matOriginal, "" + (int) distance, center, Core.FONT_HERSHEY_PLAIN, 1, BLACK); Imgproc.putText(matOriginal, "" + (int) azimuth, centerw, Core.FONT_HERSHEY_PLAIN, 1, BLACK); } // output an image for debugging Imgcodecs.imwrite("output.png", matOriginal); FrameCount++; } shouldRun = false; }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.CameraCalibration.java
License:Open Source License
/** * Draws checkerboard corners on an image. * // www . j av a 2 s . c o m * @param image the image to process * @param addToCalibration if true, add this image to the corner list */ public void process(Mat image, boolean addToCalibration) { boolean patternFound = Calib3d.findChessboardCorners(image, boardSize, boardCorners, Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_NORMALIZE_IMAGE | Calib3d.CALIB_CB_FAST_CHECK); if (patternFound) { // Refine corner positions to be more accurate Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY); Imgproc.cornerSubPix(grayImage, boardCorners, new Size(6, 6), new Size(-1, -1), new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.1)); if (addToCalibration) { calibrationCorners.add(boardCorners); } } image.copyTo(boardImage); Calib3d.drawChessboardCorners(boardImage, boardSize, boardCorners, patternFound); if (!addToCalibration) { debugImage("Board", boardImage); } Imgproc.undistort(image, undistortImage, cameraMatrix, distCoeffs); undistortImage.copyTo(image); Imgproc.putText(image, "Error: " + error, new Point(20, 20), Core.FONT_HERSHEY_PLAIN, 1.5, new Scalar(0, 255, 0)); }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.Utils.java
License:Open Source License
public static void drawText(Mat image, String text, double x, double y) { Imgproc.putText(image, text, new Point(x, y), Core.FONT_HERSHEY_PLAIN, TEXT_SIZE, TEXT_COLOR); }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.Utils.java
License:Open Source License
public static void drawText(Mat image, String text, double x, double y, double textSize) { Imgproc.putText(image, text, new Point(x, y), Core.FONT_HERSHEY_PLAIN, textSize, TEXT_COLOR); }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.Utils.java
License:Open Source License
public static void drawText(Mat image, String text, double x, double y, Scalar textColor) { Imgproc.putText(image, text, new Point(x, y), Core.FONT_HERSHEY_PLAIN, TEXT_SIZE, textColor); }
From source file:org.usfirst.frc.team2084.CMonster2016.vision.Utils.java
License:Open Source License
public static void drawText(Mat image, String text, double x, double y, double textSize, Scalar textColor) { Imgproc.putText(image, text, new Point(x, y), Core.FONT_HERSHEY_PLAIN, textSize, textColor); }