2

So I'm trying to place an image into a scene using opencv. I can rotate it about its centre (z axis) using GetRotationMatrix2d and warpaffine. I'm wondering to I need to use a perspective transform? Or can I just create a 3d rotation matrix and plug it into warpaffine? And is there any getRotationMatrix equivalent for 3d? Thanks!

Edit: I found this post but it does not work for me.

My code is below And I have gotten it to rotate, although I'm not sure what focal length I should be using? everytime I rotate about a different angle I need to play around with the focal length to get it right. Say if I rotate my image 45' it looks like the pic below but that's clearly warped off in some way... enter image description here

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);
  }
OneTwo
  • 1,445
  • 5
  • 22
  • 45

1 Answers1

1

Does translation work? Try the following code instead.

Mat T = (Mat_<double>(4, 4) <<
         1, 0, 0, 0,
         0, 1, 0, 0,
         0, 0, 1, 0,
         dx, dy, dz, 1);
Tony Luo
  • 110
  • 1
  • 9