Opencv - 选择矩形并从android中的图像应用转换?

时间:2016-07-07 09:44:21

标签: android opencv detection rectangles

我想从图像中检测纸张。我应用了中位数模糊,Canny,扩张,阈值等。 find的算法。我能找到工作表,但不知道如何裁剪矩形和应用转换

enter image description here

这是我的代码: -

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++;
        }
    }

我想只选择白色的纸张。请帮助我

提前致谢

0 个答案:

没有答案