Question

I am writing an AR application on iOS and am using CATransform3D transforms on a UIView.

Through OpenCV, I can get a perspective matrix through the call cvGetPerspectiveTransform(). This returns a 3x3 matrix.

I can't remember where I found it, but I had sample code that did the following:

//the opencv transform matrix is put into map_matrix
cvGetPerspectiveTransform (src_pnt, dst_pnt, map_matrix);

CATransform3D t = CATransform3DIdentity;

// map over the CATransform3D 4x4 matrix from the opencv 3x3 matrix
t.m11 = cvmGet(map_matrix,0,0); 
t.m12 = cvmGet(map_matrix,1,0); 
t.m14 = cvmGet(map_matrix,2,0); 
t.m21 = cvmGet(map_matrix,0,1); 
t.m22 = cvmGet(map_matrix,1,1); 
t.m24 = cvmGet(map_matrix,2,1); 
t.m41 = cvmGet(map_matrix,0,2); 
t.m42 = cvmGet(map_matrix,1,2); 
t.m44 = cvmGet(map_matrix,2,2); 

myView.layer.transform = t;

However, applying this form causes my view to go crazy and jump all over the place. With a little experimenting, I found that only these three mappings work somewhat:

t.m14 = cvmGet(map_matrix,2,0); //left and right transform
t.m24 = cvmGet(map_matrix,2,1); //up and down transform
t.m21 = cvmGet(map_matrix,0,1); //some kind of skew?

I've been reading up on 3d graphics and the transform math, but it's quite a daunting task.

What would be the correct mapping of the 4x4 matrix from the 3x3 matrix?

Thank you!

Was it helpful?

Solution

There may be different problems:

First, you should try the inverse of your matrix (in case the transform you obtained is actually from destination to source). Use mat.inv(), or swap the parameters sent to getPerspectiveMatrix.

If it does not solve your problem, you must decompose the 3x3 matrix into rotations and translations and combine it to form a 4x4 matrix. An approximate solution is to delete the last row, and the third column (which correspond to 3D rotation) - but this is only a rough approximation.

Please check also this very good post about creating perspective transform matrices Opencv virtually camera rotating/translating for bird's eye view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top