Search in sources :

Example 11 with Point

use of org.opencv.core.Point 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 12 with Point

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

the class Subdiv2D method locate.

// 
// C++:  int locate(Point2f pt, int& edge, int& vertex)
// 
// javadoc: Subdiv2D::locate(pt, edge, vertex)
public int locate(Point pt, int[] edge, int[] vertex) {
    double[] edge_out = new double[1];
    double[] vertex_out = new double[1];
    int retVal = locate_0(nativeObj, pt.x, pt.y, edge_out, vertex_out);
    if (edge != null)
        edge[0] = (int) edge_out[0];
    if (vertex != null)
        vertex[0] = (int) vertex_out[0];
    return retVal;
}
Also used : Point(org.opencv.core.Point)

Example 13 with Point

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

the class Subdiv2D method edgeOrg.

// 
// C++:  int edgeOrg(int edge, Point2f* orgpt = 0)
// 
// javadoc: Subdiv2D::edgeOrg(edge, orgpt)
public int edgeOrg(int edge, Point orgpt) {
    double[] orgpt_out = new double[2];
    int retVal = edgeOrg_0(nativeObj, edge, orgpt_out);
    if (orgpt != null) {
        orgpt.x = orgpt_out[0];
        orgpt.y = orgpt_out[1];
    }
    return retVal;
}
Also used : Point(org.opencv.core.Point)

Example 14 with Point

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

the class Subdiv2D method edgeDst.

// 
// C++:  int edgeDst(int edge, Point2f* dstpt = 0)
// 
// javadoc: Subdiv2D::edgeDst(edge, dstpt)
public int edgeDst(int edge, Point dstpt) {
    double[] dstpt_out = new double[2];
    int retVal = edgeDst_0(nativeObj, edge, dstpt_out);
    if (dstpt != null) {
        dstpt.x = dstpt_out[0];
        dstpt.y = dstpt_out[1];
    }
    return retVal;
}
Also used : Point(org.opencv.core.Point)

Example 15 with Point

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

the class Lines method vectorExtend.

/**
 * Extends a line as a vector, increasing the Euclidean distance of point 2 from point 1 by moving only point 2.
 * @param line The line to be modified.
 * @param lengthFinal The final desired Euclidean length of the line
 * @param size The size of the working image
 * @return A new Line object of the appropriate length
 */
public static Line vectorExtend(Line line, double lengthFinal, Size size) {
    double scalar = lengthFinal / line.length();
    line.x2 = (int) (MathFTC.clip(scalar * (line.x2 - line.x1) + line.x1, 0, size.width - 1));
    line.y2 = (int) (MathFTC.clip(scalar * (line.y2 - line.y1) + line.y1, 0, size.height - 1));
    line.point2 = new Point(line.x2, line.y2);
    return line;
}
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