您的转换失败,因为您为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));
现在,这假设您知道要变形的图像部分的角点。如果您不知道如何获得中间方块的积分,我建议您看看这些优秀的答案。
但是,只要您有四个角点,此方法就可以使用。您甚至可以通过手动查找中间方形角点并插入它们来尝试。