Search in sources :

Example 1 with MatOfPoint

use of org.opencv.core.MatOfPoint in project Auto.js by hyb1996.

the class ColorFinder method findColor.

public Point findColor(ImageWrapper image, int color, int threshold, Rect rect) {
    MatOfPoint matOfPoint = findColorInner(image, color, threshold, rect);
    if (matOfPoint == null) {
        return null;
    }
    Point point = matOfPoint.toArray()[0];
    if (rect != null) {
        point.x = mScreenMetrics.scaleX((int) (point.x + rect.x));
        point.y = mScreenMetrics.scaleX((int) (point.y + rect.y));
    }
    return point;
}
Also used : MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

Example 2 with MatOfPoint

use of org.opencv.core.MatOfPoint in project Auto.js by hyb1996.

the class ColorFinder method findColorInner.

private MatOfPoint findColorInner(ImageWrapper image, int color, int threshold, Rect rect) {
    Mat bi = new Mat();
    Scalar lowerBound = new Scalar(Color.red(color) - threshold, Color.green(color) - threshold, Color.blue(color) - threshold, 255);
    Scalar upperBound = new Scalar(Color.red(color) + threshold, Color.green(color) + threshold, Color.blue(color) + threshold, 255);
    if (rect != null) {
        Core.inRange(new Mat(image.getMat(), rect), lowerBound, upperBound, bi);
    } else {
        Core.inRange(image.getMat(), lowerBound, upperBound, bi);
    }
    Mat nonZeroPos = new Mat();
    Core.findNonZero(bi, nonZeroPos);
    if (nonZeroPos.rows() == 0 || nonZeroPos.cols() == 0) {
        return null;
    }
    return new MatOfPoint(nonZeroPos);
}
Also used : Mat(org.opencv.core.Mat) MatOfPoint(org.opencv.core.MatOfPoint) Scalar(org.opencv.core.Scalar)

Example 3 with MatOfPoint

use of org.opencv.core.MatOfPoint in project Auto.js by hyb1996.

the class ColorFinder method findAllPointsForColor.

public Point[] findAllPointsForColor(ImageWrapper image, int color, int threshold, Rect rect) {
    MatOfPoint matOfPoint = findColorInner(image, color, threshold, rect);
    if (matOfPoint == null) {
        return new Point[0];
    }
    Point[] points = matOfPoint.toArray();
    if (rect != null) {
        for (int i = 0; i < points.length; i++) {
            points[i].x = mScreenMetrics.scaleX((int) (points[i].x + rect.x));
            points[i].y = mScreenMetrics.scaleX((int) (points[i].y + rect.y));
        }
    }
    return points;
}
Also used : MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

Example 4 with MatOfPoint

use of org.opencv.core.MatOfPoint in project Relic_Main by TeamOverdrive.

the class GenericDetector method processFrame.

@Override
public Mat processFrame(Mat rgba, Mat gray) {
    Size initSize = rgba.size();
    newSize = new Size(initSize.width * downScaleFactor, initSize.height * downScaleFactor);
    rgba.copyTo(workingMat);
    Imgproc.resize(workingMat, workingMat, newSize);
    if (rotateMat) {
        Mat tempBefore = workingMat.t();
        // mRgba.t() is the transpose
        Core.flip(tempBefore, workingMat, -1);
        tempBefore.release();
    }
    Mat preConvert = workingMat.clone();
    colorFilter.process(preConvert, mask);
    if (stretch) {
        structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, stretchKernal);
        Imgproc.morphologyEx(mask, mask, Imgproc.MORPH_CLOSE, structure);
    }
    List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(mask, contours, hiarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.drawContours(workingMat, contours, -1, new Scalar(230, 70, 70), 2);
    Rect chosenRect = null;
    double chosenScore = Integer.MAX_VALUE;
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for (MatOfPoint c : contours) {
        MatOfPoint2f contour2f = new MatOfPoint2f(c.toArray());
        // Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true) * 0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
        // Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint(approxCurve.toArray());
        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);
        // You can find this by printing the area of each found rect, then looking and finding what u deem to be perfect.
        // Run this with the bot, on a balance board, with jewels in their desired location. Since jewels should mostly be
        // in the same position, this hack could work nicely.
        double area = Imgproc.contourArea(c);
        double areaDiffrence = 0;
        switch(detectionMode) {
            case MAX_AREA:
                areaDiffrence = -area * areaWeight;
                break;
            case PERFECT_AREA:
                areaDiffrence = Math.abs(perfectArea - area);
                break;
        }
        // Just declaring vars to make my life eassy
        double x = rect.x;
        double y = rect.y;
        double w = rect.width;
        double h = rect.height;
        Point centerPoint = new Point(x + (w / 2), y + (h / 2));
        // Get the ratio. We use max in case h and w get swapped??? it happens when u account for rotation
        double cubeRatio = Math.max(Math.abs(h / w), Math.abs(w / h));
        double ratioDiffrence = Math.abs(cubeRatio - perfectRatio);
        double finalDiffrence = (ratioDiffrence * ratioWeight) + (areaDiffrence * areaWeight);
        // Think of diffrence as score. 0 = perfect
        if (finalDiffrence < chosenScore && finalDiffrence < maxDiffrence && area > minArea) {
            chosenScore = finalDiffrence;
            chosenRect = rect;
        }
        if (debugContours && area > 100) {
            Imgproc.circle(workingMat, centerPoint, 3, new Scalar(0, 255, 255), 3);
            Imgproc.putText(workingMat, "Area: " + String.format("%.1f", area), centerPoint, 0, 0.5, new Scalar(0, 255, 255));
        }
    }
    if (chosenRect != null) {
        Imgproc.rectangle(workingMat, new Point(chosenRect.x, chosenRect.y), new Point(chosenRect.x + chosenRect.width, chosenRect.y + chosenRect.height), new Scalar(0, 255, 0), 3);
        Imgproc.putText(workingMat, "Result: " + String.format("%.2f", chosenScore), new Point(chosenRect.x - 5, chosenRect.y - 10), Core.FONT_HERSHEY_PLAIN, 1.3, new Scalar(0, 255, 0), 2);
        Point centerPoint = new Point(chosenRect.x + (chosenRect.width / 2), chosenRect.y + (chosenRect.height / 2));
        resultRect = chosenRect;
        resultLocation = centerPoint;
        resultFound = true;
    } else {
        resultFound = false;
        resultRect = null;
        resultLocation = null;
    }
    Imgproc.resize(workingMat, workingMat, initSize);
    preConvert.release();
    Imgproc.putText(workingMat, "DogeCV v1.1 Generic: " + newSize.toString() + " - " + speed.toString() + " - " + detectionMode.toString(), new Point(5, 30), 0, 1.2, new Scalar(0, 255, 255), 2);
    return workingMat;
}
Also used : Mat(org.opencv.core.Mat) Rect(org.opencv.core.Rect) Size(org.opencv.core.Size) MatOfPoint2f(org.opencv.core.MatOfPoint2f) ArrayList(java.util.ArrayList) MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint) Scalar(org.opencv.core.Scalar)

Example 5 with MatOfPoint

use of org.opencv.core.MatOfPoint in project Relic_Main by TeamOverdrive.

the class Converters method vector_vector_Point_to_Mat.

// vector_vector_Point
public static Mat vector_vector_Point_to_Mat(List<MatOfPoint> pts, List<Mat> mats) {
    Mat res;
    int lCount = (pts != null) ? pts.size() : 0;
    if (lCount > 0) {
        for (MatOfPoint vpt : pts) mats.add(vpt);
        res = vector_Mat_to_Mat(mats);
    } else {
        res = new Mat();
    }
    return res;
}
Also used : Mat(org.opencv.core.Mat) MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfKeyPoint(org.opencv.core.MatOfKeyPoint) KeyPoint(org.opencv.core.KeyPoint) MatOfPoint(org.opencv.core.MatOfPoint)

Aggregations

MatOfPoint (org.opencv.core.MatOfPoint)15 Point (org.opencv.core.Point)12 Mat (org.opencv.core.Mat)9 ArrayList (java.util.ArrayList)6 MatOfPoint2f (org.opencv.core.MatOfPoint2f)4 Scalar (org.opencv.core.Scalar)4 Rect (org.opencv.core.Rect)3 Size (org.opencv.core.Size)3 KeyPoint (org.opencv.core.KeyPoint)1 MatOfKeyPoint (org.opencv.core.MatOfKeyPoint)1