在OpenCV中为warpPerspective设置图像中心的原点

时间:2012-02-06 19:06:44

标签: opencv perspective

我遇到OpenCVs warpPerspective函数的问题。我正在使用OpenCV 2.3.1。我的目标很简单:我希望能够围绕每个轴旋转图像,但原点位于图像的中心,而不是左上角。我的问题可能更多是关于数学而不是编程: - )

我的代码如下所示:

Mat transformationMatrix, xRotation(3, 3, CV_32F, Scalar(0)), yRotation(3, 3, CV_32F, Scalar(0)), zRotation(3, 3, CV_32F, Scalar(0)), offset(3, 3, CV_32F, Scalar(0));
Mat transformedImage(baseImage.rows, baseImage.cols, CV_8UC3);

float xRad = 0.0*((float)M_PI/(float)180);
xRotation.at<float>(0,0) = 1;
xRotation.at<float>(1,1) = cos(xRad);
xRotation.at<float>(1,2) = -sin(xRad);
xRotation.at<float>(2,1) = sin(xRad);
xRotation.at<float>(2,2) = cos(xRad);

float yRad = 0.0*((float)M_PI/(float)180);
yRotation.at<float>(0,0) = cos(yRad);
yRotation.at<float>(0,2) = sin(yRad);
yRotation.at<float>(1,1) = 1;
yRotation.at<float>(2,0) = -sin(yRad);
yRotation.at<float>(2,2) = cos(yRad);

float zRad = 10.0*((float)M_PI/(float)180);
zRotation.at<float>(0,0) = cos(zRad);
zRotation.at<float>(0,1) = -sin(zRad);
zRotation.at<float>(1,0) = sin(zRad);
zRotation.at<float>(1,1) = cos(zRad);
zRotation.at<float>(2,2) = 1;

offset.at<float>(0,0) = 1;
offset.at<float>(1,1) = 1;
offset.at<float>(2,2) = 1;
offset.at<float>(0,2) = image.cols/2;
offset.at<float>(1,2) = image.rows/2;

transformationMatrix = xRotation*yRotation*zRotation*offset;

warpPerspective(image, transformedImage, transformationMatrix, Size(transformedImage.rows, transformedImage.cols));
imshow("Output", transformedImage);

图像是预先填充的cv :: Mat。在这里,我只是试图将图像旋转10度,但稍后我需要进行更复杂的变换。我引入的偏移移动图像使中心位于0,0,这意味着图像被正确旋转。然而,它有一个令人遗憾的副作用,就是扔掉最终在图像外面的四分之三的图像。理想情况下,我应该能够将旋转后的图像转换回视图,但我无法完全理解这一点。据我所知,这必须以某种方式在同一个转换矩阵中完成。

我应该注意到我已经看到了这个问题:Specify an origin to warpPerspective() function in OpenCV 2.x,但我希望没有重新实现warpPerspective就可以做到这一点。我也见过OpenCV warpperspective,但没有一个指针对我有帮助。这个问题:How to use cv::warpPerspective for the perspective transformation?与我的问题无关。

提前感谢任何提示: - )

0 个答案:

没有答案