get translation and rotation matrix of an object

1,403 views
Skip to first unread message

Rafa Ortega

unread,
Jan 8, 2012, 10:53:38 AM1/8/12
to android-opencv
Hi all!!

I am working with detected squares, I have 4 points in the frame that represent the square vertex, but I want to use them with OpenGL drawing. So I need to know the translation and rotation matrix. I imagine it has something to do with the functions cvFindExtrinsicCameraParams2 and Rodrigues that i've seen in C++, but I don't know how to make them work in Android since I don't see them anywhere.

Can anybody send me some light?? 

Thanks in advance =)

Andrey Kamaev

unread,
Jan 8, 2012, 1:51:46 PM1/8/12
to android...@googlegroups.com
cvFindExtrinsicCameraParams2 is the old C name of the function. In C++ and Java API it is named solvePnP
Here is a usage example from Java API tests:

Mat intrinsics = Mat.eye(3, 3, CvType.CV_32F);
intrinsics.put(0, 0, 400);
intrinsics.put(1, 1, 400);
intrinsics.put(0, 2, 640 / 2);
intrinsics.put(1, 2, 480 / 2);

List<Point3> points3d = new ArrayList<Point3>();
List<Point> points2d = new ArrayList<Point>();
int minPnpPointsNum = 4;

for (int i = 0; i < minPnpPointsNum; i++) {
    double x = Math.random() * 100 - 50;
    double y = Math.random() * 100 - 50;
    points2d.add(new Point(x, y));
    points3d.add(new Point3(0, y, x));
}

Mat rvec = new Mat();
Mat tvec = new Mat();
Calib3d.solvePnP(points3d, points2d, intrinsics, new Mat(), rvec, tvec);

Mat truth_rvec = new Mat(3, 1, CvType.CV_64F);
truth_rvec.put(0, 0, 0, Math.PI / 2, 0);

Mat truth_tvec = new Mat(3, 1, CvType.CV_64F);
truth_tvec.put(0, 0, -320, -240, 400);

assertMatEqual(truth_rvec, rvec, EPS);
assertMatEqual(truth_tvec, tvec, EPS);

/Andrey

Rafa Ortega

unread,
Jan 9, 2012, 12:05:42 PM1/9/12
to android...@googlegroups.com
Thanks a lot Andrey! This is usefull to me, by the way I have one question about the code you send, why you use that intrinsics matrix as the cameraMatrix? Is it some kind of approach that will work always or do I have to calculate the cameraMatrix somehow? If i have to... how do I get the cameraMatrix?

Thanks again.
Rafa.

2012/1/8 Andrey Kamaev <andrey...@itseez.com>

Andrey Kamaev

unread,
Jan 10, 2012, 6:30:21 AM1/10/12
to android...@googlegroups.com
Ideally you should calibrate your camera. Without the calibration you will have noticeable errors in estimated object pose.

I have not enough experience to say how good is the matrix from this test code. Any way you need to do some corrections for your case, at least adjust the camera matrix for your resolution.
There is an OpenCV sample named select3dobj.cpp demonstrating calibration and pose estimation. Probably you'll find it useful.

/Andrey
Reply all
Reply to author
Forward
0 new messages