Search in sources :

Example 41 with Point

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

the class ColorFinder method checksPath.

private boolean checksPath(ImageWrapper image, Point startingPoint, int threshold, Rect rect, int[] points) {
    for (int i = 0; i < points.length; i += 3) {
        int x = points[i];
        int y = points[i + 1];
        int color = points[i + 2];
        ColorDetector colorDetector = new ColorDetector.DifferenceDetector(color, threshold);
        int c = image.pixel((int) (x + startingPoint.x), (int) (y + startingPoint.y));
        if (!colorDetector.detectsColor(Color.red(c), Color.green(c), Color.blue(c))) {
            return false;
        }
    }
    return true;
}
Also used : Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

Example 42 with Point

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

the class TemplateMatching method pyrUp.

private static void pyrUp(Point p, int level) {
    for (int i = 0; i < level; i++) {
        p.x *= 2;
        p.y *= 2;
    }
}
Also used : Point(org.opencv.core.Point)

Example 43 with Point

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

the class GlyphDetector 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();
    }
    Imgproc.putText(workingMat, newSize.toString() + " - " + speed.toString(), new Point(5, 15), 0, 0.5, new Scalar(0, 255, 0), 1);
    Imgproc.cvtColor(workingMat, processed, Imgproc.COLOR_RGB2GRAY);
    switch(speed) {
        case VERY_FAST:
            Imgproc.blur(processed, processed, new Size(2, 2));
            Imgproc.bilateralFilter(processed.clone(), processed, 11, 17, 17);
            Imgproc.Canny(processed, edges, 15, 45.0);
            structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(3, 3));
            Imgproc.morphologyEx(edges, edges, Imgproc.MORPH_CLOSE, structure);
            break;
        case FAST:
            Imgproc.blur(processed, processed, new Size(3, 3));
            Imgproc.bilateralFilter(processed.clone(), processed, 11, 17, 17);
            Imgproc.Canny(processed, edges, 15, 45.0);
            structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(6, 6));
            Imgproc.morphologyEx(edges, edges, Imgproc.MORPH_CLOSE, structure);
            break;
        case BALANCED:
            Imgproc.blur(processed, processed, new Size(4, 4));
            Imgproc.bilateralFilter(processed.clone(), processed, 11, 17, 17);
            Imgproc.Canny(processed, edges, 15, 45.0);
            structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(7, 7));
            Imgproc.morphologyEx(edges, edges, Imgproc.MORPH_CLOSE, structure);
            break;
        case SLOW:
            Imgproc.blur(processed, processed, new Size(6, 6));
            Imgproc.bilateralFilter(processed.clone(), processed, 11, 17, 17);
            Imgproc.Canny(processed, edges, 15, 45.0);
            structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(10, 10));
            Imgproc.morphologyEx(edges, edges, Imgproc.MORPH_CLOSE, structure);
            break;
        case VERY_SLOW:
            Imgproc.blur(processed, processed, new Size(7, 7));
            Imgproc.bilateralFilter(processed.clone(), processed, 11, 17, 17);
            Imgproc.Canny(processed, edges, 15, 45.0);
            structure = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_RECT, new Size(15, 15));
            Imgproc.morphologyEx(edges, edges, Imgproc.MORPH_CLOSE, structure);
            break;
    }
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(edges, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    hierarchy.release();
    double chosenScore = 0;
    Rect chosenRect = null;
    Collections.sort(contours, new Comparator<MatOfPoint>() {

        @Override
        public int compare(MatOfPoint matOfPoint, MatOfPoint t1) {
            if (Imgproc.contourArea(matOfPoint) > Imgproc.contourArea(t1)) {
                return -1;
            } else if (Imgproc.contourArea(matOfPoint) < Imgproc.contourArea(t1)) {
                return 1;
            } else {
                return 0;
            }
        }
    });
    // Remove First Index which is usually a large square filling the entire screen,
    contours.remove(0);
    for (MatOfPoint c : contours) {
        if (Imgproc.contourArea(c) > 1000) {
            Rect rect = Imgproc.boundingRect(c);
            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));
            double cubeRatio = Math.max(Math.abs(h / w), Math.abs(w / h));
            double score = 100;
            double diffrenceFromPerfect = Math.abs(1 - cubeRatio);
            double scoreRatioPunishment = 1 - diffrenceFromPerfect;
            double scoreRatio = scoreRatioPunishment * scoreRatioWeight;
            score *= scoreRatio;
            double distanceFromCenterX = (newSize.width / 2) - centerPoint.x;
            double distanceFromCenterY = newSize.height - centerPoint.y;
            distanceFromCenterX = Math.abs(distanceFromCenterX / newSize.width);
            distanceFromCenterY = Math.abs(distanceFromCenterY / newSize.height);
            double scoreDistanceFromCenterXPunishment = 1 - distanceFromCenterX;
            double scoreDistanceFromCenterYPunishment = 1 - distanceFromCenterY;
            double scoreDistanceFromCenterX = scoreDistanceFromCenterXPunishment * scoreDistanceXWeight;
            double scoreDistanceFromCenterY = scoreDistanceFromCenterYPunishment * scoreDistanceYWeight;
            score *= scoreDistanceFromCenterX;
            score *= scoreDistanceFromCenterY;
            double minArea = GetMinArea(contours);
            double maxArea = GetMaxArea(contours);
            double area = Imgproc.contourArea(c);
            double normalizedArea = (area - minArea) / (maxArea - minArea);
            double scoreAreaPunishment = normalizedArea;
            double scoreArea = scoreAreaPunishment * scoreAreaWeight;
            score *= scoreArea;
            if (chosenRect == null) {
                chosenRect = rect;
                chosenScore = score;
            }
            if (score > chosenScore) {
                chosenRect = rect;
                chosenScore = score;
            }
            if (debugDrawRects) {
                Imgproc.rectangle(workingMat, new Point(x, y), new Point((x + w), (y + h)), new Scalar(0, 255, 255), 1);
            }
            if (debugDrawRects) {
                String toPrint = String.format("Score: %.2f", score);
                Imgproc.putText(workingMat, toPrint, new Point(x + 5, y + 5), 0, 0.5, new Scalar(0, 255, 255));
            }
        }
    }
    if (chosenRect != null && chosenScore > minScore) {
        double x = chosenRect.x;
        double y = chosenRect.y;
        double w = chosenRect.width;
        double h = chosenRect.height;
        Imgproc.rectangle(workingMat, new Point(x, y), new Point((x + w), (y + h)), new Scalar(0, 255, 0), 3);
        chosenGlyphPosition = new Point((x + (w / 2)), (y + (h / 2)));
        chosenGlyphOffset = newSize.width - (x + (w / 2));
        foundRect = false;
    } else {
        foundRect = true;
    }
    Imgproc.resize(workingMat, workingMat, initSize);
    return workingMat;
}
Also used : Mat(org.opencv.core.Mat) Rect(org.opencv.core.Rect) Size(org.opencv.core.Size) 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 44 with Point

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

the class JewelDetector 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 redConvert = workingMat.clone();
    Mat blueConvert = workingMat.clone();
    colorFilterRed.process(redConvert, maskRed);
    colorFilterBlue.process(blueConvert, maskBlue);
    List<MatOfPoint> contoursRed = new ArrayList<>();
    Imgproc.findContours(maskRed, contoursRed, hiarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.drawContours(workingMat, contoursRed, -1, new Scalar(230, 70, 70), 2);
    Rect chosenRedRect = null;
    double chosenRedScore = Integer.MAX_VALUE;
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for (MatOfPoint c : contoursRed) {
        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 < chosenRedScore && finalDiffrence < maxDiffrence && area > minArea) {
            chosenRedScore = finalDiffrence;
            chosenRedRect = rect;
        }
        if (debugContours && area > 100) {
            Imgproc.circle(workingMat, centerPoint, 3, new Scalar(0, 255, 255), 3);
            Imgproc.putText(workingMat, "Area: " + area, centerPoint, 0, 0.5, new Scalar(0, 255, 255));
        }
    }
    List<MatOfPoint> contoursBlue = new ArrayList<>();
    Imgproc.findContours(maskBlue, contoursBlue, hiarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
    Imgproc.drawContours(workingMat, contoursBlue, -1, new Scalar(70, 130, 230), 2);
    Rect chosenBlueRect = null;
    double chosenBlueScore = Integer.MAX_VALUE;
    for (MatOfPoint c : contoursBlue) {
        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 - 1);
        double finalDiffrence = (ratioDiffrence * ratioWeight) + (areaDiffrence * areaWeight);
        // Think of diffrence as score. 0 = perfect
        if (finalDiffrence < chosenBlueScore && finalDiffrence < maxDiffrence && area > minArea) {
            chosenBlueScore = finalDiffrence;
            chosenBlueRect = rect;
        }
        if (debugContours && area > 100) {
            Imgproc.circle(workingMat, centerPoint, 3, new Scalar(0, 255, 255), 3);
            Imgproc.putText(workingMat, "Area: " + area, centerPoint, 0, 0.5, new Scalar(0, 255, 255));
        }
    }
    if (chosenRedRect != null) {
        Imgproc.rectangle(workingMat, new Point(chosenRedRect.x, chosenRedRect.y), new Point(chosenRedRect.x + chosenRedRect.width, chosenRedRect.y + chosenRedRect.height), new Scalar(255, 0, 0), 2);
        Imgproc.putText(workingMat, "Red: " + String.format("%.2f", chosenRedScore), new Point(chosenRedRect.x - 5, chosenRedRect.y - 10), Core.FONT_HERSHEY_PLAIN, 1.3, new Scalar(255, 0, 0), 2);
    }
    if (chosenBlueRect != null) {
        Imgproc.rectangle(workingMat, new Point(chosenBlueRect.x, chosenBlueRect.y), new Point(chosenBlueRect.x + chosenBlueRect.width, chosenBlueRect.y + chosenBlueRect.height), new Scalar(0, 0, 255), 2);
        Imgproc.putText(workingMat, "Blue: " + String.format("%.2f", chosenBlueScore), new Point(chosenBlueRect.x - 5, chosenBlueRect.y - 10), Core.FONT_HERSHEY_PLAIN, 1.3, new Scalar(0, 0, 255), 2);
    }
    if (chosenBlueRect != null && chosenRedRect != null) {
        if (chosenBlueRect.x < chosenRedRect.x) {
            currentOrder = JewelOrder.BLUE_RED;
            lastOrder = currentOrder;
        } else {
            currentOrder = JewelOrder.RED_BLUE;
            lastOrder = currentOrder;
        }
    } else {
        currentOrder = JewelOrder.UNKNOWN;
    }
    Imgproc.putText(workingMat, "Result: " + lastOrder.toString(), new Point(10, newSize.height - 30), 0, 1, new Scalar(255, 255, 0), 1);
    Imgproc.putText(workingMat, "Current Track: " + currentOrder.toString(), new Point(10, newSize.height - 10), 0, 0.5, new Scalar(255, 255, 255), 1);
    Imgproc.resize(workingMat, workingMat, initSize);
    redConvert.release();
    blueConvert.release();
    Imgproc.putText(workingMat, "DogeCV 1.1 Jewel: " + 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 45 with Point

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

the class Line method resize.

public void resize(double scale) {
    this.x1 = scale * x1;
    this.y1 = scale * y1;
    this.x2 = scale * x2;
    this.y2 = scale * y2;
    this.point1 = new Point(x1, y1);
    this.point2 = new Point(x2, y2);
}
Also used : Point(org.opencv.core.Point)

Aggregations

Point (org.opencv.core.Point)56 MatOfPoint (org.opencv.core.MatOfPoint)30 Mat (org.opencv.core.Mat)23 ArrayList (java.util.ArrayList)11 Scalar (org.opencv.core.Scalar)9 Line (com.disnodeteam.dogecv.math.Line)7 Rect (org.opencv.core.Rect)7 Size (org.opencv.core.Size)7 MatOfPoint2f (org.opencv.core.MatOfPoint2f)4 TimingLogger (android.util.TimingLogger)2 Ponto (br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Ponto)2 KeyPoint (org.opencv.core.KeyPoint)2 MatOfKeyPoint (org.opencv.core.MatOfKeyPoint)2 Pair (android.util.Pair)1 Corner (br.edu.ifspsaocarlos.sdm.kifurecorder.processing.cornerDetector.Corner)1 Rectangle (java.awt.Rectangle)1 List (java.util.List)1 ImageNotFoundException (org.getopentest.exceptions.ImageNotFoundException)1 Core (org.opencv.core.Core)1 MinMaxLocResult (org.opencv.core.Core.MinMaxLocResult)1