0

我想使用 opencv执行图像变形。我已经检测到图像的 4 个角((ex)4000x4000)并getPerspectiveTransform在 opencv 中使用了变换矩阵。现在,我想在图像中心扭曲图像。所以我用warpPerspective. 我输入的源图像大小为 450x450,我使用为整个图像计算的变换矩阵。

但是屏幕上什么也没有显示。任何帮助表示赞赏。

这是示例代码和示例图像。

这是源图像。 这是源图像。

检测到此图像 检测到此图像

这是我想要从图像中得到的翘曲区域。(代码中的 src_crop)

在此处输入图像描述

这是结果。

在此处输入图像描述

        source[0] = Point2f(lt.x, lt.y);
        source[1] = Point2f(rt.x, rt.y);
        source[2] = Point2f(rb.x, rb.y);
        source[3] = Point2f(lb.x, lb.y);

        dst[0] = Point2f(6, 10);
        dst[1] = Point2f(3899, 7);
        dst[2] = Point2f(3901, 3899);
        dst[3] = Point2f(9, 3902);

        Mat transformMatrix ;
        transformMatrix = getPerspectiveTransform(source, dst);



        Mat dstFrame ;
        warpPerspective(src_crop, dstFrame, transformMatrix, Size(450, 450));
4

1 回答 1

1

您的转换失败,因为您为getPerspectiveTransform方法使用了错误的值。您似乎混淆了如何创建输出图像以及如何从该图像的数据中填充目标角。

此外,链接数组中的右上角(左上、右上、左下、右下)很重要,你似乎把它弄混了。

此示例将向您展示如何连接正确的点并将其输出到一个空的输出图像中:

// Assuming your source image is called 'sourceImage' and you have the corner points you need:

// Create vectors to store the corners
vector<Point2f> originalCorners;
vector<Point2f> destinationCorners;

// Put the Sudoku corners in your originalCorners
originalCorners.clear();
originalCorners.push_back(Point2f(lt.x, lt.y);
originalCorners.push_back(Point2f(rt.x, rt.y);
originalCorners.push_back(Point2f(lb.x, lb.y);
originalCorners.push_back(Point2f(rb.x, rb.y);

// Output image size of 450x450
int ouputImageWidth = 450;
int outputImageHeight = 450;

// Create an empty image (450x450) to output your transformation result in
Mat transformedOutputImage(ouputImageWidth, outputImageHeight, sourceImage.type());

// Now put the corners of the output image so the warp knows where to warp to
destinationCorners.clear();
destinationCorners.push_back(Point2f(0, 0));
destinationCorners.push_back(Point2f(ouputImageWidth, 0));
destinationCorners.push_back(Point2f(0, outputImageHeight));
destinationCorners.push_back(Point2f(ouputImageWidth, outputImageHeight));

// Now we have all corners sorted, so we can create the warp matrix
Mat warpMatrix = getPerspectiveTransform(originalCorners, destinationCorners);

// And now we can warp the Sudoku in the new image
warpPerspective(sourceImage, transformedOutputImage, warpMatrix, Size(ouputImageWidth, ouputImageHeight));

现在,这假设您知道要变形的图像部分的角点。如果您不知道如何获得中间方块的积分,我建议您看看这些优秀的答案

但是,只要您有四个角点,此方法就可以使用。您甚至可以通过手动查找中间方形角点并插入它们来尝试。

于 2017-04-05T12:46:41.677 回答