允许用户输入图像和变换矩阵并查看应用的变换的 OpenCV 程序

OpenCV Program that Allows User to Enter Image and Transformation Matrix and See Transformation Applied

所以,我 运行 遇到的第一个问题是 OpenCV 将其原点定义为左上角而不是 window 的中心。这是一个问题,因为我只想将 t运行sformation 矩阵应用于图像(例如关于 x 轴的反射)并看到它“就地”应用,因此它保持在同一个位置,但仍然反映。

我解决这个问题的方法是首先 t运行slate 图像到 OpenCV 的原点,将我的 t运行sformation 矩阵应用到图像,然后 t运行slate 它回来回到原来的位置。

这工作正常,但是,图像的任何部分在任何时候离开屏幕都是永久性的 deleted/cropped。我不知道如何防止这种情况发生,我想可能有一个标志或其他东西,我尝试使用 BORDER_WRAP 而不是 BORDER_CONSTANT 这几乎解决了我的问题,但不完全是现在我完全坚持下一步要去哪里

这是我目前的情况:

int main()
{
// Read in and display input image
Mat src = imread("myImage.png");

imshow("Input Image", src);

// Translate the image to the origin
Mat M = (Mat_<double>(2, 3) << 1, 0, -(src.rows / 2), 0, 1, -(src.cols / 2));
Size dsize = Size(src.rows, src.cols);
warpAffine(src, src, M, dsize, INTER_LINEAR, BORDER_CONSTANT, Scalar());

// Apply the affine transformation
Mat M2 = (Mat_<double>(2, 3) << 1, 0, 0, 0, -1, 0);
dsize = Size(src.rows, src.cols);
warpAffine(src, src, M2, dsize, INTER_LINEAR, BORDER_CONSTANT, Scalar());

// Translate the resulting image back to its original location and display
Mat M3 = (Mat_<double>(2, 3) << 1, 0, (src.rows / 2), 0, 1, (src.cols / 2));
dsize = Size(src.rows, src.cols);
warpAffine(src, src, M3, dsize, INTER_LINEAR, BORDER_CONSTANT, Scalar());

// This is an extremely cropped version of the input image because
// it gets cropped when translated to the origin
imshow("Output Image", src);

waitKey();

return 0;
}

新代码:

// Read in and display input image
Mat src = imread("umichLogo.png");
imshow("Input Image", src);

Mat M = (Mat_<double>(3, 3) << 1, 0, -(src.rows / 2), 0, 1, -(src.cols / 2), 0, 0, 1);
Mat M2 = (Mat_<double>(3, 3) << 1, 0, 0, 0, -1, 0, 0, 0, 1);
Mat M3 = (Mat_<double>(3, 3) << 1, 0, (src.rows / 2), 0, 1, (src.cols / 2), 0, 0, 1);

Mat Composition = M3 * (M2 * M);
Size dsize = Size(src.rows, src.cols);
warpPerspective(src, src, Composition, dsize, INTER_LINEAR, BORDER_CONSTANT, Scalar());

imshow("Output Image", src);
waitKey();

return 0;

为避免不希望的裁剪,仅(同时)变换一次。

Mat M = (Mat_<double>(3, 3) << 1, 0, -(src.rows / 2), 0, 1, -(src.cols / 2), 0,0,1);
Mat M2 = (Mat_<double>(3, 3) << 1, 0, 0, 0, -1, 0, 0,0,1);
Mat M3 = (Mat_<double>(3, 3) << 1, 0, (src.rows / 2), 0, 1, (src.cols / 2), 0,0,1);

Mat Composition = M3 * (M2 * M);
Size dsize = Size(src.rows, src.cols);
warpPerspective(src, src, Composition, dsize, INTER_LINEAR, BORDER_CONSTANT, Scalar());

我发现,rows 和 cols 是错误的(不仅是大小,还有 M 和 M3)。 固定代码是:

Mat M = (Mat_<double>(3, 3) << 1, 0, -(src.cols / 2), 0, 1, -(src.rows / 2), 0,0,1);
Mat M2 = (Mat_<double>(3, 3) << 1, 0, 0, 0, -1, 0, 0,0,1);
Mat M3 = (Mat_<double>(3, 3) << 1, 0, (src.cols / 2), 0, 1, (src.rows / 2), 0,0,1);
Mat Comp = M3 * (M2 * M);
warpPerspective(src, src, Comp, src.size(), INTER_LINEAR, BORDER_CONSTANT, Scalar());