Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Crop rectangle and apply transformation from image?

Hello all,

i want to detect paper sheet from image.i applied medianBlur, Canny ,dilate,threshold,Etc. algorithms to find.i am able to find sheet but dont know how to crop rectangle and apply transformation

image description

this is my code:-

    Mat blurred = new Mat();
    Imgproc.medianBlur(src, blurred, 9);

    // Set up images to use.
    Mat gray0 = new Mat(blurred.size(), CvType.CV_8U);
    Mat gray = new Mat();

    // For Core.mixChannels.
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    List<MatOfPoint2f> rectangles = new ArrayList<MatOfPoint2f>();

    List<Mat> sources = new ArrayList<Mat>();
    sources.add(blurred);
    List<Mat> destinations = new ArrayList<Mat>();
    destinations.add(gray0);

    // To filter rectangles by their areas.
    int srcArea = src.rows() * src.cols();

    // Find squares in every color plane of the image.
    for (int c = 0; c < 3; c++) {
        int[] ch = {c, 0};
        MatOfInt fromTo = new MatOfInt(ch);

        Core.mixChannels(sources, destinations, fromTo);

        // Try several threshold levels.
        for (int l = 0; l < N; l++) {
            if (l == 0) {
                // HACK: Use Canny instead of zero threshold level.
                // Canny helps to catch squares with gradient shading.
                // NOTE: No kernel size parameters on Java API.
                Imgproc.Canny(gray0, gray, 0, CANNY_THRESHOLD);

                // Dilate Canny output to remove potential holes between edge segments.
                Imgproc.dilate(gray, gray, Mat.ones(new Size(3, 3), 0));
            } else {
                int threshold = (l + 1) * 255 / N;
                Imgproc.threshold(gray0, gray, threshold, 255, Imgproc.THRESH_BINARY);
            }

            // Find contours and store them all as a list.
            Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
            int i=0;
            for (MatOfPoint contour : contours) {
                MatOfPoint2f contourFloat = GeomUtils.toMatOfPointFloat(contour);
                double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02;

                // Approximate polygonal curves.
                MatOfPoint2f approx = new MatOfPoint2f();
                Imgproc.approxPolyDP(contourFloat, approx, arcLen, true);

                if (isRectangle(approx, srcArea)) {
                    Imgproc.drawContours(src, contours, i, new Scalar(255, 0, 0), 3);
                    //rectangles.add(approx);
                    /*Rect rect = Imgproc.boundingRect(contour);
                    Log.e("Rectangle Finder:-" + i, "Height:-" + rect.height + ", Width:-" + rect.width + " and Area:-" + rect.area() + "\nX:-" + rect.x + ",Y:-" + rect.y);*/
                }
                i++;
            }
        }

i want to select only white papersheet.please help me

Thanks in advance