使用 opencv 进行图像变形

Image Warping using opencv

我想使用 opencv 执行图像变形。 我已经检测到图像的 4 个角((ex)4000x4000)并在 opencv 中使用 getPerspectiveTransform 获得了变换矩阵。 现在,我想在图像中心扭曲图像。 所以我用了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));

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

此外,重要的是 link 阵列中的右角(左上角、右上角、左下角、右下角),您似乎混淆了。

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

// 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));

现在,假设您知道要变形的图像部分的角点。如果你不知道如何获取中间方格的点数,我建议你看看these excellent answers

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