5

Given a 3 x 3 rotation matrix,R, and a 3 x 1 translation matrix,T, I am wondering how to multiply the T and R matrices to an image?

Lets say the Iplimage img is 640 x 480.

What I want to do is R*(T*img).

I was thinking of using cvGemm, but that didn't work.

JasonMArcher
  • 12,386
  • 20
  • 54
  • 51
SeriousTyro
  • 725
  • 3
  • 9
  • 14
  • 2
    Does the answer help you, if not let me know, i'll try to explain better, if yes: thanks to accept it ! Julien – jmartel Aug 12 '11 at 13:23

1 Answers1

7

The function you are searching for is probably warpPerspective() : this is a use case...

// 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 axis
        Mat R = (Mat_<double>(4, 4) <<
            1,          0,           0, 0,
            0, cos(alpha), -sin(alpha), 0,
            0, sin(alpha),  cos(alpha), 0,
            0,          0,           0, 1);

// Translation matrix on the Z axis 
        Mat T = (Mat_<double>(4, 4) <<
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, dist,
            0, 0, 0, 1);

// Camera Intrisecs matrix 3D -> 2D
        Mat A2 = (Mat_<double>(3,4) <<
            f, 0, w/2, 0,
            0, f, h/2, 0,
            0, 0,   1, 0);

Mat transfo = A2 * (T * (R * A1));

Mat source;
Mat destination;

warpPerspective(source, destination, transfo, source.size(), INTER_CUBIC | WARP_INVERSE_MAP);

I hope it could help you,

Julien

PS : I gave the example with a projection from 2D to 3D but you can use directly transfo = T* R;

jmartel
  • 2,693
  • 2
  • 20
  • 27
  • I have followed this link http://jepsonsblog.blogspot.in/2012/11/rotation-in-3d-using-opencvs.html but I dont know what the inputs I need to give. I tried with few numbers but couldnt succeed. My intention is to rotate a given image along y-axis. – 2vision2 Aug 27 '13 at 14:56
  • 2
    The A1 matrix is wrong here, it needs to have another 1 in the third row. Se this answer: https://stackoverflow.com/questions/17087446/how-to-calculate-perspective-transform-for-opencv-from-rotation-angles –  Jun 11 '17 at 21:21
  • Er, sorry... I guess that your way could work as well as long as you move the camera (or image) back from z=0 with a big z-translation... –  Jun 11 '17 at 21:38