[英]How can I rotate an image about the yaxis with opencv?
因此,我尝试使用opencv将图像放置到场景中。 我可以使用GetRotationMatrix2d和warpaffine将其绕其中心(z轴)旋转。 我想知道我是否需要使用透视变换? 还是可以只创建一个3D旋转矩阵并将其插入warpaffine中? 还有3d的任何getRotationMatrix吗? 谢谢!
编辑:我发现了这篇文章,但对我不起作用。
我的代码在下面,尽管我不确定应该使用什么焦距,但我已经旋转了它。 每次我旋转一个不同的角度时,我都需要焦距调整才能正确。 假设我将图片旋转为45',如下图所示,但显然以某种方式变形了...
void rotateImage(const Mat &input, Mat &output, double alpha, double beta, double gamma, double dx, double dy, double dz, double f)
{
alpha = (alpha - 90.)*CV_PI/180.;
beta = (beta - 90.)*CV_PI/180.;
gamma = (gamma - 90.)*CV_PI/180.;
// get width and height for ease of use in matrices
double w = (double)input.cols;
double h = (double)input.rows;
// Projection 2D -> 3D matrix
Mat A1 = (Mat_<double>(4,3) <<
1, 0, -w/2,
0, 1, -h/2,
0, 0, 0,
0, 0, 1);
// Rotation matrices around the X, Y, and Z axis
Mat RX = (Mat_<double>(4, 4) <<
1, 0, 0, 0,
0, cos(alpha), -sin(alpha), 0,
0, sin(alpha), cos(alpha), 0,
0, 0, 0, 1);
Mat RY = (Mat_<double>(4, 4) <<
cos(beta), 0, -sin(beta), 0,
0, 1, 0, 0,
sin(beta), 0, cos(beta), 0,
0, 0, 0, 1);
Mat RZ = (Mat_<double>(4, 4) <<
cos(gamma), -sin(gamma), 0, 0,
sin(gamma), cos(gamma), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Composed rotation matrix with (RX, RY, RZ)
Mat R = RX * RY * RZ;
// Translation matrix
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, dx,
0, 1, 0, dy,
0, 0, 1, dz,
0, 0, 0, 1);
// 3D -> 2D matrix
Mat A2 = (Mat_<double>(3,4) <<
f, 0, w/2, 0,
0, f, h/2, 0,
0, 0, 1, 0);
// Final transformation matrix
Mat trans = A2 * (T * (R * A1));
// Apply matrix transformation
warpPerspective(input, output, trans, input.size(), INTER_LANCZOS4);
}
翻译有效吗? 请尝试以下代码。
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
dx, dy, dz, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.