Search in sources :

Example 21 with Size

use of org.opencv.core.Size 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 22 with Size

use of org.opencv.core.Size 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 23 with Size

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

the class HSVColorFilter method process.

@Override
public void process(Mat input, Mat mask) {
    Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2HSV_FULL);
    Imgproc.GaussianBlur(input, input, new Size(3, 3), 0);
    Scalar lower = new Scalar(perfect.val[0] - range.val[0], perfect.val[1] - range.val[1], perfect.val[2] - range.val[2]);
    Scalar upper = new Scalar(perfect.val[0] + range.val[0], perfect.val[1] + range.val[1], perfect.val[2] + range.val[2]);
    Core.inRange(input, lower, upper, mask);
    input.release();
}
Also used : Size(org.opencv.core.Size) Scalar(org.opencv.core.Scalar)

Example 24 with Size

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

the class LeviColorFilter method leviBlueFilter.

public void leviBlueFilter(Mat input, Mat mask, double threshold) {
    List<Mat> channels = new ArrayList<>();
    Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
    Imgproc.GaussianBlur(input, input, new Size(3, 3), 0);
    Core.split(input, channels);
    Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
    for (int i = 0; i < channels.size(); i++) {
        channels.get(i).release();
    }
}
Also used : Mat(org.opencv.core.Mat) Size(org.opencv.core.Size) ArrayList(java.util.ArrayList)

Example 25 with Size

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

the class LeviColorFilter method leviRedFilter.

public void leviRedFilter(Mat input, Mat mask, double threshold) {
    Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
    Imgproc.GaussianBlur(input, input, new Size(3, 3), 0);
    Core.split(input, channels);
    Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
    for (int i = 0; i < channels.size(); i++) {
        channels.get(i).release();
    }
}
Also used : Size(org.opencv.core.Size)

Aggregations

Size (org.opencv.core.Size)28 Mat (org.opencv.core.Mat)17 Scalar (org.opencv.core.Scalar)9 ArrayList (java.util.ArrayList)8 Rect (org.opencv.core.Rect)7 Point (org.opencv.core.Point)6 MatOfPoint (org.opencv.core.MatOfPoint)4 SurfaceTexture (android.graphics.SurfaceTexture)2 Camera (android.hardware.Camera)2 Line (com.disnodeteam.dogecv.math.Line)2 File (java.io.File)2 MatOfPoint2f (org.opencv.core.MatOfPoint2f)2 DrawerLayout (android.support.v4.widget.DrawerLayout)1 ActionBarDrawerToggle (android.support.v7.app.ActionBarDrawerToggle)1 SurfaceView (android.view.SurfaceView)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 RadioButton (android.widget.RadioButton)1 TextView (android.widget.TextView)1