所以我尝试使用 opencv 将图像放入场景中。我可以使用 GetRotationMatrix2d 和 warpaffine 围绕它的中心(z 轴)旋转它。我想知道我需要使用透视变换吗?或者我可以只创建一个 3d 旋转矩阵并将其插入 warpaffine 中吗?是否有任何 getRotationMatrix 等效于 3d? 谢谢!
编辑:我找到了 this post但它对我不起作用。
我的代码在下面 我已经让它旋转了,虽然我不确定我应该使用什么焦距?每次我旋转不同的角度时,我都需要调整焦距以使其正确。假设我将我的图像旋转 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);
关于c++ - 如何使用 opencv 围绕 y 轴旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22947987/