因此,我正在尝试使用opencv将图像放置到场景中。我可以使用GetRotationMatrix2d和warpaffine将其绕其中心(z轴)旋转。我想知道我是否需要使用透视变换?还是可以只创建一个3D旋转矩阵并将其插入warpaffine中?还有3d的任何getRotationMatrix吗?
谢谢!

编辑:我找到了this post,但对我不起作用。

我的代码在下面,尽管我不确定应该使用什么焦距,但我已经旋转了它。每次我旋转不同的 Angular 时,我都需要调整焦距以使其正确。假设我将图片旋转为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/

10-12 00:30
查看更多