Search in sources :

Example 1 with Scalar

use of org.opencv.core.Scalar in project Frankenstein by olir.

the class LR2VR180 method configure.

@Override
public Mat configure(Mat sourceFrame) {
    float aspect = ((float) sourceFrame.cols()) / (float) sourceFrame.rows();
    int vcut = 0;
    System.out.println("aspect (A): " + aspect);
    if (aspect > 2f)
        // not a half sbs
        aspect = aspect / 2f;
    if (aspect < 1.4f) {
        if (aspect < 1.3f)
            aspect *= 1.3f;
        vcut = 1;
    }
    System.out.println("aspect (B): " + aspect);
    // aspect heuristic
    vrVerticalSpan = 0.7f - (aspect - 1.33333f) / 1.7f;
    if (vrVerticalSpan < 0.4f)
        vrVerticalSpan = 0.4f;
    System.out.println("VerticalSpan: " + vrVerticalSpan + "  vcut: " + vcut);
    borderW = (int) ((1.0f - factor) * (float) (sourceFrame.cols() >> 1) * 0.25f) + vcut * (int) (sourceFrame.cols() * 0.50);
    borderH = (int) (((1.0f - factor) + (1.0f / vrVerticalSpan - 1.0) * convert3DMode) * (float) sourceFrame.rows() * 0.5f);
    borderH = ((borderH + (2 << (ALIGMENT_POT - 1)) - 1) >> ALIGMENT_POT) << ALIGMENT_POT;
    System.out.println("borderW: " + borderW + "  borderH: " + borderH);
    newFrame = sourceFrame.clone();
    Imgproc.resize(sourceFrame, newFrame, new Size((double) sourceFrame.cols() + 4 * borderW, (double) sourceFrame.rows() + 2 * borderH));
    newFrame.setTo(new Scalar(0, 0, 0));
    smallWidth = sourceFrame.cols() >> 1;
    smallHeight = sourceFrame.rows();
    System.out.println("aspect: " + (((float) sourceFrame.cols()) / 2f / (float) sourceFrame.rows()) + " ==> " + (((float) newFrame.cols()) / 2f / (float) newFrame.rows()));
    if (coneCorrection) {
        for (int i = 0; i < STEPS_PER_DIRECTION; i++) {
            bufferFrame[i] = sourceFrame.clone();
            Imgproc.resize(sourceFrame, bufferFrame[i], new Size((double) sourceFrame.cols() + 4 * borderW, (double) sourceFrame.rows() + 2 * borderH));
            bufferFrame[i].setTo(new Scalar(0, 0, 0));
        }
        double[] weight = new double[STEPS_PER_DIRECTION];
        double sum = 0.0;
        for (int i = 0; i < STEPS_PER_DIRECTION; i++) {
            weight[i] = 1.0 + ((double) i) / (double) (STEPS_PER_DIRECTION - 1);
            sum += weight[i];
        }
        int hsumSrc = 0;
        int hsumDest = 0;
        for (int i = 0; i < STEPS_PER_DIRECTION; i++) {
            weight[i] = sum / ((double) STEPS_PER_DIRECTION) / weight[i];
            srcStepOffset[i] = hsumSrc;
            srcStepHeight[i] = (int) ((((double) (smallHeight >> 1)) / (double) STEPS_PER_DIRECTION));
            hsumSrc += srcStepHeight[i];
            destStepOffset[i] = hsumDest;
            destStepHeight[i] = (int) (weight[i] * (((double) (smallHeight >> 1)) / (double) STEPS_PER_DIRECTION));
            hsumDest += destStepHeight[i];
        }
    // srcStepHeight[STEPS_PER_DIRECTION - 1] += (smallHeight >> 1) - hsumSrc;
    // destStepHeight[STEPS_PER_DIRECTION - 1] += (smallHeight >> 1) - hsumDest;
    }
    return newFrame;
}
Also used : Size(org.opencv.core.Size) Scalar(org.opencv.core.Scalar)

Example 2 with Scalar

use of org.opencv.core.Scalar in project Frankenstein by olir.

the class StereoDistanceFilter method process.

@Override
public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
    if (newFrame == null || newFrame.cols() != sourceFrame.cols() || newFrame.rows() != sourceFrame.rows()) {
        newFrame = sourceFrame.clone();
        perspectiveMultiplier = sourceFrame.cols() / 320;
        if (perspectiveMultiplier < 1)
            perspectiveMultiplier = 1;
    }
    newFrame.setTo(new Scalar(0, 0, 0));
    Rect roi = new Rect(val(0, perspective(), 0, -1), 0, (sourceFrame.cols() >> 1) - Math.abs(perspective()), sourceFrame.rows());
    sourceFrame.submat(new Rect((Math.abs(perspective()) >> 1), 0, (sourceFrame.cols() >> 1) - Math.abs(perspective()), sourceFrame.rows())).copyTo(new Mat(newFrame, roi));
    roi = new Rect(val(sourceFrame.cols() >> 1, -perspective(), sourceFrame.cols() >> 1, -1), 0, (sourceFrame.cols() >> 1) - Math.abs(perspective()), sourceFrame.rows());
    sourceFrame.submat(new Rect((sourceFrame.cols() >> 1) + (Math.abs(perspective()) >> 1), 0, (sourceFrame.cols() >> 1) - Math.abs(perspective()), sourceFrame.rows())).copyTo(new Mat(newFrame, roi));
    return newFrame;
}
Also used : Mat(org.opencv.core.Mat) Rect(org.opencv.core.Rect) Scalar(org.opencv.core.Scalar)

Example 3 with Scalar

use of org.opencv.core.Scalar in project Frankenstein by olir.

the class SlideShowInput method process.

@Override
public Mat process(Mat sourceFrame, int frameId, FilterContext context) {
    int sid = (frameId - 1) / (fps * fpSlide);
    if (sid < slides.size()) {
        Slide s = slides.get(sid);
        File[] f = s.getFiles();
        Mat img = Imgcodecs.imread(f[0].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
        img.convertTo(img, CvType.CV_8UC3);
        Imgproc.resize(img, tmpFrame, new Size((double) smallWidth, (double) smallHeight));
        Rect roi = new Rect(0, 0, smallWidth, smallHeight);
        tmpFrame.copyTo(new Mat(newFrame, roi));
        if (mode3D && f.length > 1) {
            img = Imgcodecs.imread(f[1].getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_COLOR);
            img.convertTo(img, CvType.CV_8UC3);
            Imgproc.resize(img, tmpFrame, new Size((double) smallWidth, (double) smallHeight));
        }
        roi = new Rect(smallWidth, 0, smallWidth, smallHeight);
        tmpFrame.copyTo(new Mat(newFrame, roi));
    } else
        newFrame.setTo(new Scalar(0, 0, 0, 0));
    return newFrame;
}
Also used : Mat(org.opencv.core.Mat) Rect(org.opencv.core.Rect) Size(org.opencv.core.Size) File(java.io.File) Scalar(org.opencv.core.Scalar)

Example 4 with Scalar

use of org.opencv.core.Scalar 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 5 with Scalar

use of org.opencv.core.Scalar 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)

Aggregations

Scalar (org.opencv.core.Scalar)15 Mat (org.opencv.core.Mat)9 Size (org.opencv.core.Size)9 Rect (org.opencv.core.Rect)7 MatOfPoint (org.opencv.core.MatOfPoint)6 Point (org.opencv.core.Point)6 ArrayList (java.util.ArrayList)4 MatOfPoint2f (org.opencv.core.MatOfPoint2f)2 Line (com.disnodeteam.dogecv.math.Line)1 File (java.io.File)1 List (java.util.List)1