Search in sources :

Example 6 with Point

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

the class TemplateMatching method getROI.

private static Rect getROI(Point p, Mat src, Mat currentTemplate) {
    int x = (int) (p.x * 2 - currentTemplate.cols() / 4);
    x = Math.max(0, x);
    int y = (int) (p.y * 2 - currentTemplate.rows() / 4);
    y = Math.max(0, y);
    int w = (int) (currentTemplate.cols() * 1.5);
    int h = (int) (currentTemplate.rows() * 1.5);
    if (x + w >= src.cols()) {
        w = src.cols() - x - 1;
    }
    if (y + h >= src.rows()) {
        h = src.rows() - y - 1;
    }
    return new Rect(x, y, w, h);
}
Also used : Rect(org.opencv.core.Rect) Point(org.opencv.core.Point)

Example 7 with Point

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

the class TemplateMatching method getBestMatched.

public static Pair<Point, Double> getBestMatched(Mat tmResult, int matchMethod, float threshold) {
    TimingLogger logger = new TimingLogger(LOG_TAG, "best_matched_point");
    // FIXME: 2017/11/26 正交化?
    // Core.normalize(tmResult, tmResult, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Core.MinMaxLocResult mmr = Core.minMaxLoc(tmResult);
    logger.addSplit("minMaxLoc");
    double value;
    Point pos;
    if (matchMethod == Imgproc.TM_SQDIFF || matchMethod == Imgproc.TM_SQDIFF_NORMED) {
        pos = mmr.minLoc;
        value = -mmr.minVal;
    } else {
        pos = mmr.maxLoc;
        value = mmr.maxVal;
    }
    logger.addSplit("value:" + value);
    logger.dumpToLog();
    return new Pair<>(pos, value);
}
Also used : TimingLogger(android.util.TimingLogger) Point(org.opencv.core.Point) Core(org.opencv.core.Core) Pair(android.util.Pair)

Example 8 with Point

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

the class Images method findImage.

public Point findImage(ImageWrapper image, ImageWrapper template, float weakThreshold, float threshold, Rect rect, int maxLevel) {
    if (image == null)
        throw new NullPointerException("image = null");
    if (template == null)
        throw new NullPointerException("template = null");
    Mat src = image.getMat();
    if (rect != null) {
        src = new Mat(src, rect);
    }
    org.opencv.core.Point point = TemplateMatching.fastTemplateMatching(src, template.getMat(), TemplateMatching.MATCHING_METHOD_DEFAULT, weakThreshold, threshold, maxLevel);
    if (point != null) {
        if (rect != null) {
            point.x += rect.x;
            point.y += rect.y;
        }
        point.x = mScreenMetrics.scaleX((int) point.x);
        point.y = mScreenMetrics.scaleX((int) point.y);
    }
    return point;
}
Also used : Mat(org.opencv.core.Mat) Point(org.opencv.core.Point)

Example 9 with Point

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

the class CryptoboxDetector method processFrame.

@Override
public Mat processFrame(Mat rgba, Mat gray) {
    downScaleFactor = 0.5;
    Size initSize = rgba.size();
    newSize = new Size(initSize.width * downScaleFactor, initSize.height * downScaleFactor);
    rgba.copyTo(workingMat);
    avgPoints = new ArrayList<>();
    Imgproc.resize(workingMat, workingMat, newSize);
    if (rotateMat) {
        Mat tempBefore = workingMat.t();
        // mRgba.t() is the transpose
        Core.flip(tempBefore, workingMat, 1);
        tempBefore.release();
    }
    switch(detectionMode) {
        case RED:
            Mat redMask = workingMat.clone();
            colorFilterRed.process(redMask, mask);
            redMask.release();
            break;
        case BLUE:
            Mat blueMask = workingMat.clone();
            colorFilterBlue.process(blueMask, mask);
            blueMask.release();
            break;
    }
    // display = new Mat(mask.height(), mask.width(), CvType.CV_8UC1);
    ArrayList<Line> lines = (ArrayList<Line>) Lines.getOpenCvLines(mask, 1, 55);
    lines = (ArrayList<Line>) Lines.linearExtend(lines, 4, newSize);
    // lines = Lines.mergeLines(lines, 13, 300, 6);
    // lines = Lines.mergeLines(lines, 6, 2000, 4);
    List<Line> linesVertical = new ArrayList<Line>();
    for (Line line : lines) {
        if (Lines.getAngularDistance(line, new Line(new Point(0, 0), new Point(100, 0))) > 45) {
            linesVertical.add(line);
        }
    }
    Collections.sort(linesVertical, new Comparator<Line>() {

        @Override
        public int compare(Line line1, Line line2) {
            if (line1.center().x > line2.center().x) {
                return 1;
            } else if (line1.center().x < line2.center().x) {
                return -1;
            } else {
                return 0;
            }
        }
    });
    if (linesVertical.size() == 0) {
        CryptoBoxDetected = false;
        ColumnDetected = false;
        return rgba;
    }
    Line left = linesVertical.get(0);
    Line right = linesVertical.get(linesVertical.size() - 1);
    double perpDistance = Lines.getPerpindicularDistance(left, right);
    double collumnLength = Lines.getPerpindicularDistance(left, right) / 6;
    List<List<Line>> groupings = new ArrayList<List<Line>>();
    int j = 0;
    while (j < linesVertical.size()) {
        List<Line> group = new ArrayList<Line>();
        group.add(linesVertical.get(j));
        int i = j + 1;
        while (i < linesVertical.size() && Lines.getPerpindicularDistance(linesVertical.get(j), linesVertical.get(i)) < collumnLength) {
            group.add(linesVertical.get(i));
            i++;
        }
        groupings.add(group);
        j = i;
    }
    for (int i = 0; i < groupings.size() - 1; i++) {
        Point center = new Line(Lines.getMeanPoint(groupings.get(i)), Lines.getMeanPoint(groupings.get(i + 1))).center();
        int y = (int) MathFTC.clip(0.6 * center.y, 0, mask.height());
        double max = 1.4 * center.y;
        if (center.y < 125) {
            y = 1;
            max = 250;
        }
        int count = 0;
        while (y < mask.height() && y < max && count < 10) {
            if (mask.get(y, (int) center.x)[0] > 0) {
                count++;
            // Imgproc.circle(original, new Point(2*center.x, 2*y), 10, new Scalar(255,255,255), 6);
            } else {
            // Imgproc.circle(original, new Point(2*center.x, 2*y), 10, new Scalar(30,30,200), 6);
            }
            y += 10;
        }
        if (count >= 10) {
            List<Line> appendee = groupings.get(i);
            appendee.addAll(groupings.get(i + 1));
            groupings.set(i, appendee);
            groupings.remove(i + 1);
            i -= 1;
        }
    }
    for (int i = 0; i < groupings.size(); i++) {
        Point center = Lines.getMeanPoint(groupings.get(i));
        int y = (int) MathFTC.clip(0.2 * center.y, 0, mask.height());
        double max = 1.8 * center.y;
        if (center.y < 50) {
            y = 1;
            max = (int) 0.8 * mask.height();
        }
        int minX = (int) MathFTC.clip(center.x - 5, 0, mask.width());
        int maxX = (int) MathFTC.clip(center.x + 5, 0, mask.width());
        int count = 0;
        while (y < mask.height() && y < max && count < 10) {
            if (mask.get(y, (int) center.x)[0] > 0 || mask.get(y, minX)[0] > 0 || mask.get(y, maxX)[0] > 0) {
                count++;
            // Imgproc.circle(rgba, new Point(2*center.x, 2*y), 10, new Scalar(255,255,255), 6);
            } else {
            // Imgproc.circle(rgba, new Point(2*center.x, 2*y), 10, new Scalar(30,30,200), 6);
            }
            y += 4;
        }
        if (count <= 9) {
            groupings.remove(i);
            i -= 1;
        }
    }
    if (groupings.size() > 4) {
        Collections.sort(groupings, new Comparator<List<Line>>() {

            @Override
            public int compare(List<Line> g1, List<Line> g2) {
                if (Lines.stdDevX(g1) > Lines.stdDevX(g2)) {
                    return 1;
                } else if (Lines.stdDevX(g1) < Lines.stdDevX(g2)) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        groupings = groupings.subList(0, 4);
    }
    List<Line> columns = new ArrayList<Line>();
    for (int i = 0; i < groupings.size(); i++) {
        Point center = Lines.getMeanPoint(groupings.get(i));
        double angle = Lines.getMeanAngle(groupings.get(i));
        columns.add(Lines.constructLine(Lines.getMeanPoint(groupings.get(i)), Lines.getMeanAngle(groupings.get(i)), 400));
    }
    for (int i = 0; i < groupings.size(); i++) {
        groupings.set(i, Lines.resize(groupings.get(i), 1 / downScaleFactor));
    }
    for (int i = 0; i < groupings.size(); i++) {
        // Imgproc.circle(original, Lines.getMeanPoint(groupings.get(i)), 50, new Scalar(40,200,70), 4);
        for (Line line : groupings.get(i)) {
        // Imgproc.line(rgba, line.point1, line.point2, new Scalar(50,200,55), 4);
        // Imgproc.circle(rgba, line.center(), 20, new Scalar(80,60,190),4);
        // Imgproc.putText(rgba, Integer.toString(i), line.center(), Core.FONT_HERSHEY_PLAIN, 7, new Scalar(10,240,230),3);
        }
    }
    for (Line line : columns) {
        line.resize(1 / downScaleFactor);
        Imgproc.line(rgba, line.point1, line.point2, new Scalar(20, 165, 240), 20);
    }
    if (columns.size() < 3) {
        trackables = new ArrayList<>();
        CryptoBoxDetected = false;
        ColumnDetected = false;
        return rgba;
    }
    for (int i = 0; i < columns.size() - 1; i++) {
        Line conec = Lines.getPerpindicularConnector(columns.get(i), columns.get(i + 1), rgba.size());
        // Imgproc.line(rgba, conec.point1, conec.point2, new Scalar(210, 30, 40), 7);
        Point centerPoint = conec.center();
        if (i < 3) {
            if (trackables.size() == 0) {
                for (int l = 0; l < trackableMemory; l++) {
                    trackables.add(new ArrayList<Point>());
                }
            }
            if (trackables.size() <= i) {
                trackables.add(new ArrayList<Point>());
            }
            if (trackables.get(i).size() < trackableMemory) {
                trackables.get(i).add(centerPoint);
            } else {
                Collections.rotate(trackables.get(i), -1);
                trackables.get(i).set(trackableMemory - 1, centerPoint);
            }
            for (int k = 0; k < trackables.get(i).size(); k++) {
            // Imgproc.circle(rgba, trackables.get(i).get(k),4,new Scalar(255,255,255),3);
            }
        }
        Point avgPoint = Points.getMeanPoint(trackables.get(i));
        Imgproc.putText(rgba, "Col #" + i, new Point(avgPoint.x, avgPoint.y - 15), 0, 1.5, new Scalar(0, 255, 255), 2);
        // DogeLogger.LogVar("Col-"+i, avgPoint.toString());
        Imgproc.circle(rgba, avgPoint, 15, new Scalar(0, 255, 0), 6);
        avgPoints.add(avgPoint);
        CryptoBoxPositions[i] = (int) avgPoint.x;
    }
    if (avgPoints.size() == 3) {
        CryptoBoxDetected = true;
    }
    ColumnDetected = true;
    Point newFull = Points.getMeanPoint(avgPoints);
    Line newFullLine = new Line(newFull, fullAvgPoint);
    if (newFullLine.length() > 75) {
        trackables = new ArrayList<>();
        Log.d("DogeCV", "RESETTING TRACKABLE!");
    }
    fullAvgPoint = newFull;
    // Imgproc.cvtColor(white, white, Imgproc.COLOR_RGB2HSV);
    Imgproc.putText(rgba, "DogeCV 1.1 Crypto: " + newSize.toString() + " - " + speed.toString() + " - " + detectionMode.toString(), new Point(5, 30), 0, 1.2, new Scalar(0, 255, 255), 2);
    return rgba;
}
Also used : Mat(org.opencv.core.Mat) Size(org.opencv.core.Size) ArrayList(java.util.ArrayList) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint) Scalar(org.opencv.core.Scalar) Line(com.disnodeteam.dogecv.math.Line) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with Point

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

the class CryptoboxDetector method drawSlot.

public Point drawSlot(int slot, List<Rect> boxes) {
    // Get the pillar to the left
    Rect leftColumn = boxes.get(slot);
    // Get the pillar to the right
    Rect rightColumn = boxes.get(slot + 1);
    // Get the X Coord
    int leftX = leftColumn.x;
    // Get the X Coord
    int rightX = rightColumn.x;
    // Calculate the point between the two
    int drawX = ((rightX - leftX) / 2) + leftX;
    // Calculate Y Coord. We wont use this in our bot's opetation, buts its nice for drawing
    int drawY = leftColumn.height + leftColumn.y;
    return new Point(drawX, drawY);
}
Also used : Rect(org.opencv.core.Rect) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint) Point(org.opencv.core.Point) MatOfPoint(org.opencv.core.MatOfPoint)

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