opencv纠正轮廓

时间:2017-12-02 18:45:49

标签: android opencv image-processing

inputImage的

enter image description here

ResultImage

enter image description here

我能够过滤图像中最大的轮廓以检测令牌。

我已经应用了扭曲感知,但它只是在轮廓的边缘裁剪图像,没有别的。

我希望将检测到的令牌从图像的其余部分中裁剪出来,在保持比例的同时对其进行去偏斜,这样结果图像应该是直立的。然后我将继续寻找令牌中的blob以检测其中标记的日期。

    private Mat processMat(Mat srcMat) {
    Mat processedMat = new Mat();
    Imgproc.cvtColor(srcMat, processedMat, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(processedMat, processedMat, new Size(5, 5), 5);
    Imgproc.threshold(processedMat, processedMat, 127, 255, Imgproc.THRESH_BINARY);
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(processedMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    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 (!contours.isEmpty()) {
        Imgproc.drawContours(srcMat, contours, maxValIdx, new Scalar(0,255,0),  3);
        Rect rect = Imgproc.boundingRect(contours.get(maxValIdx));
        Log.e("rect", "" + rect);
        int top = srcMat.height();
        int left = srcMat.width();
        int right = 0;
        int bottom = 0;

        if(rect.x < left) {
            left = rect.x;
        }
        if(rect.x+rect.width > right){
            right = rect.x+rect.width;
        }
        if(rect.y < top){
            top = rect.y;
        }
        if(rect.y+rect.height > bottom){
            bottom = rect.y+rect.height;
        }

        Point topLeft = new Point(left, top);
        Point topRight = new Point(right, top);
        Point bottomRight = new Point(right, bottom);
        Point bottomLeft = new Point(left, bottom);

        return warp(srcMat, topLeft, topRight, bottomLeft, bottomRight);
    }
    return null;
}

Mat warp(Mat inputMat, Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) {
    int resultWidth = (int)(topRight.x - topLeft.x);
    int bottomWidth = (int)(bottomRight.x - bottomLeft.x);
    if(bottomWidth > resultWidth)
        resultWidth = bottomWidth;

    int resultHeight = (int)(bottomLeft.y - topLeft.y);
    int bottomHeight = (int)(bottomRight.y - topRight.y);
    if (bottomHeight > resultHeight) {
        resultHeight = bottomHeight;
    }

    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC1);

    List<Point> source = new ArrayList<>();
    source.add(topLeft);
    source.add(topRight);
    source.add(bottomLeft);
    source.add(bottomRight);
    Mat startM = Converters.vector_Point2f_to_Mat(source);

    Point ocvPOut1 = new Point(0, 0);
    Point ocvPOut2 = new Point(resultWidth, 0);
    Point ocvPOut3 = new Point(0, resultHeight);
    Point ocvPOut4 = new Point(resultWidth, resultHeight);
    List<Point> dest = new ArrayList<>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat endM = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight));
    return outputMat;
}

更新1

取代了这个:

        return warp(srcMat, topLeft, topRight, bottomLeft, bottomRight);

有了这个:

        return warp(srcMat, topLeft, topRight, bottomRight, bottomLeft);

结果更新1:

Result Now

更新2

    public Mat warp(Mat inputMat, MatOfPoint selectedContour) {
    MatOfPoint2f new_mat = new MatOfPoint2f(selectedContour.toArray());
    MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
    int contourSize = (int) selectedContour.total();
    Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);

    double[] temp_double;
    temp_double = approxCurve_temp.get(0,0);
    Point p1 = new Point(temp_double[0], temp_double[1]);
    temp_double = approxCurve_temp.get(1,0);
    Point p2 = new Point(temp_double[0], temp_double[1]);
    temp_double = approxCurve_temp.get(2,0);
    Point p3 = new Point(temp_double[0], temp_double[1]);
    temp_double = approxCurve_temp.get(3,0);
    Point p4 = new Point(temp_double[0], temp_double[1]);
    List<Point> source = new ArrayList<Point>();
    source.add(p1);
    source.add(p2);
    source.add(p3);
    source.add(p4);
    Mat startM = Converters.vector_Point2f_to_Mat(source);

    int resultWidth = 846;
    int resultHeight = 2048;

    Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);

    Point ocvPOut1 = new Point(0, 0);
    Point ocvPOut2 = new Point(0, resultHeight);
    Point ocvPOut3 = new Point(resultWidth, resultHeight);
    Point ocvPOut4 = new Point(resultWidth, 0);
    List<Point> dest = new ArrayList<Point>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    Mat endM = Converters.vector_Point2f_to_Mat(dest);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight),
            Imgproc.INTER_CUBIC);
    return outputMat;
}

结果更新2:

我稍微改变了我的warp函数并附加了代码。 然而,所得到的图像以某种方式以错误的方向旋转。你能指导我这是正确的方法吗?

Android设备方向设置为:纵向,输入图像也是纵向。

result_update_2

更新3

我设法通过对角落进行排序来拉直令牌:

    List<Point> source = new ArrayList<Point>();
    source.add(p2);
    source.add(p3);
    source.add(p4);
    source.add(p1);
    Mat startM = Converters.vector_Point2f_to_Mat(source);

结果更新3:

Result Update 3

然而,从左侧裁剪得到的图像,我不知道如何解决这个问题。 如果令牌向右或向左倾斜并且输出图像是直的,我已设法拉直输入图像。但是,如果输入图像已经使令牌居中并且直线上升。它使用相同的代码来旋转令牌:

问题更新3:

Issue Update 3

1 个答案:

答案 0 :(得分:1)

对票证进行纠正的转变接近于仿制票。您可以通过用平行四边形近似轮廓来获得它。您可以找到平行四边形的顶点作为最左侧,最顶部,最右侧和最下面的点。

实际上,你只需要三个顶点(第四个可以从这些顶点重新计算)。也许平行四边形的最小二乘拟合是可能的,我不知道。

另一个选择是考虑一个单应变换,它是从四个点定义的(但计算要复杂得多)。它将考虑到观点。 (您可能会在此处获得一些见解:https://www.codeproject.com/Articles/674433/Perspective-Projection-of-a-Rectangle-Homography。)

为了使图像变直,应用逆变换并检索矩形就足够了。无论如何,您会注意到这个矩形的大小是未知的,因此您可以任意缩放它。最难的问题是找到合适的宽高比。