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

asked 2018-04-19 22:25:34 -0600

mar_y_ow_777 gravatar image

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

edit retag flag offensive close merge delete

Comments

I'm curious to know who asked you this work.

http://answers.opencv.org/question/94...@sturkmen

LBerger gravatar imageLBerger ( 2018-04-20 03:33:36 -0600 )edit