Oncamera Find largest rectangle, it all rectangle one by One

asked 2019-06-25 04:40:47 -0500

Jayapriya gravatar image

updated 2019-07-01 22:24:24 -0500

supra56 gravatar image

android detect all Rectangle one by one .But i need to detect the rectangle at time i oncamera frame. My code:

 private Mat findRectangle(Mat src) {
    Mat blurred = src.clone();
    Imgproc.medianBlur(src, blurred, 9);

    Mat gray0 = new Mat(blurred.size(), CvType.CV_8UC1), gray = new Mat();

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    List<Mat> blurredChannel = new ArrayList<Mat>();
    blurredChannel.add(blurred);
    List<Mat> gray0Channel = new ArrayList<Mat>();
    gray0Channel.add(gray0);

    MatOfPoint2f approxCurve;

    double maxArea = 0;
    int maxId = -1;

    for (int c = 0; c < 3; c++) {
        int ch[] = {c, 0};
        Core.mixChannels(blurredChannel, gray0Channel, new MatOfInt(ch));

        int thresholdLevel = 1;
        for (int t = 0; t < thresholdLevel; t++) {
            if (t == 0) {
                Canny(gray0, gray, 10, 20, 3, true); // true ?
                Imgproc.dilate(gray, gray, new Mat(), new Point(-1, -1), 1); // 1
            } else {
                Imgproc.adaptiveThreshold(gray0, gray, thresholdLevel,
                        Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
                        Imgproc.THRESH_BINARY,
                        (src.width() + src.height()) / 200, t);
            }

            Imgproc.findContours(gray, contours, new Mat(),
                    Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

            for (MatOfPoint contour : contours) {
                MatOfPoint2f temp = new MatOfPoint2f(contour.toArray());

                double area = Imgproc.contourArea(contour);
                approxCurve = new MatOfPoint2f();
                Imgproc.approxPolyDP(temp, approxCurve, Imgproc.arcLength(temp, true) * 0.02, true);

                if (approxCurve.total() == 4 && area >= maxArea) {
                    double maxCosine = 0;

                    List<Point> curves = approxCurve.toList();
                    for (int j = 2; j < 5; j++) {

                        double cosine = Math.abs(angle(curves.get(j % 4), curves.get(j - 2), curves.get(j - 1)));
                        maxCosine = Math.max(maxCosine, cosine);
                    }

                    if (maxCosine < 0.3) {
                        maxArea = area;
                        maxId = contours.indexOf(contour);
                    }
                }
            }
        }
    }
    double maxVal = 0;

    int maxValIdx = 0;
    for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++)
    {
        double contourArea = Imgproc.contourArea(contours.get(contourIdx));
        if (maxVal < contourArea)
        {
            maxVal = contourArea;
            maxValIdx = contourIdx;
        }
    }

  if (maxId >= 0) {
        Imgproc.drawContours(src, contours, maxId, new Scalar(0, 255, 0, .16), 4);
    }

    return src;
}
edit retag flag offensive close merge delete

Comments

and the problem is where, exactly ?

berak gravatar imageberak ( 2019-07-02 02:38:12 -0500 )edit