Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have a problem in getting the edge contours of image with resolution 1836 x3264

image description


This is my code for wrapping this image

 Mat img = Imgcodecs.imread(getImg().getPath());
        if (img.empty()) {
            Log.e("Error:12", "Empty Image");
        }
 Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
   Imgproc.medianBlur(gray, gray, 21);
        Imgproc.adaptiveThreshold(gray, blurred, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 21, 2);
 Imgproc.erode(blurred, blurred, new Mat(), new Point(-1, -1), 10);
        Imgproc.dilate(blurred, blurred, new Mat(), new Point(-1, -1), 1);
        Imgproc.Canny(blurred, edged, 1, 255, 3, false);
   Mat clone = edged.clone();
        Mat hierarchy = new Mat();
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(clone, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        List<Point> curves = new ArrayList<>();
        hierarchy.release();
        Mat corners = new Mat(4, 1, CvType.CV_32FC2);


        for (int idx = 0; idx < contours.size(); idx++) {
            MatOfPoint contour = contours.get(idx);
            MatOfPoint2f contour_points = new MatOfPoint2f(contour.toArray());

            RotatedRect minRect = Imgproc.minAreaRect(contour_points);
            Point[] rect_points = new Point[4];
            minRect.points(rect_points);
            if (minRect.size.height >img .width() / 2) {
                List<Point> srcPoints = new ArrayList<Point>(4);

                srcPoints.add(rect_points[2]);
                srcPoints.add(rect_points[3]);
                srcPoints.add(rect_points[0]);
                srcPoints.add(rect_points[1]);

                corners = Converters.vector_Point_to_Mat(
                        srcPoints, CvType.CV_32F);

            }
            Imgproc.drawContours(img , contours, idx, new Scalar(0, 255, 0, .8), 2);

        }


        Imgcodecs.imwrite("asdas.jpg", (img );
        showImage(img);

Because i want to detect the 4 sides of the document and wrap it , but the result that i want is incorrect. Please help me to detect the contours of large size image.

Thank you in advance