單應矩陣計算旋轉角和平移量
Q:
have used this code as a basis to detect my rectangular target in a scene.I use ORB and Flann Matcher.I have been able to draw the bounding box of the detected target in my scene successfully using the findHomography() and perspectiveTransform() functions.
The reference image (img_object in the above code) is a straight view of only the rectangular target.Now the target in my scene image may be tilted forwards or backwards.I want to find out the angle by which it has been tilted.I have read various posts and
came to the conclusion that the homography returned by findHomography() can be decomposed to the rotation matrix and translation vector. I have used code from https:/gist.github.com/inspirit/740979 recommended by this link translated to C++.This is the Zhang
SVD decomposition code got from the camera calibration module of OpenCV.I got the complete explanation of this decomposition code from O'Reilly's Learning OpenCV book.
I also used solvePnP() on the the keypoints returned by the matcher to cross check the rotation matrix and the translation vector returned from the homography decomposition but they do not seem to the same.
I have already the measurements of the tilts of all my scene images.i found 2 ways to retrieve the angles from the rotation matrix to check how well they match my values.
Given a 3×3 rotation matrix
R =
[ r_{11} & r_{12} & r_{13} ]
[ r_{21} & r_{22} & r_{23} ]
[ r_{31} & r_{32} & r_{33} ]
The 3 Euler angles are
theta_{x} = atan2(r_{32}, r_{33})
theta_{y} = atan2(-r_{31}, sqrt{r_{32}^2 + r_{33}^2})
theta_{z} = atan2(r_{21}, r_{11})
The axis,angle representation - Being R a general rotation matrix, its corresponding rotation axis u and rotation angle θ can be retrieved from:
cos(θ) = ( trace(R) − 1) / 2
[u]× = (R − R⊤) / 2 sin(θ)
I calculated the angles using both the methods for the rotation matrices obtained from the homography decomposition and the solvepnp().All the angles are different and give very unexpected values.
Is there a hole in my understanding?I do not understand where my calculations are wrong.Are there any alternatives i can use?
A:
Why do you expect them to be the same? They are not the same thing at all.
The Euler angles are three angles of rotation about one axis at a time, starting from the world frame.
Rodriguez's formula gives components of one vector in the world frame, and an angle of rotation about that vector.
acually since am using both the formulas on the same rotation matrix,shouldn't one of the 3 euler angle match the angle gotten from the Rodriguez formula? I am new to these concepts so please correct
me if am wrong. – user2958957 Feb 14 '14 at 8:16
No. The angle in Rodriguez's formula is the total rotation, Euler angles are three ordered rotations about three separate axis that, together, accomplish the same total rotation. Suggestion,
get yourself four pencils and some tape, bind three of them at 90 deg angles, then model the whole problem in your hands. – Francesco Callari Feb 14 '14 at 14:17
thanks for clarifying :) I have been also looking for alternative ways for homography decomposition and used Jav_Rock's answer in this for the homography returned by findHomography() but rotation angles
are always 0. Is there a difference when am estimating a homography between 2 images using matched points and using only 4 precise points to compute the homography? – user2958957
Feb 17 '14 at 5:41
Q:
I have already done the comparison of 2 images of same scene which are taken by one camera with different view angles(say left and right) using SURF in emgucv (C#). And it gave me a 3x3 homography matrix for 2D transformation. But now I want to make those
2 images in 3D environment (using DirectX). To do that I need to calculate relative location and orientation of 2nd image(right) to the 1st image(left) in 3D form. How can I calculate Rotation and Translate matrices for 2nd image?
I need also z value for 2nd image.
I read something called 'Homograhy decomposition'. Is it the way?
Is there anybody who familiar with homography decomposition and is there any algorithm which it implement?
Thanks in advance for any help.
A:
Homography only works for planar scenes (ie: all of your points are coplanar). If that is the case then the homography is a projective transformation and it can be decomposed into its components.
But if your scene isn't coplanar (which I think is the case from your description) then it's going to take a bit more work. Instead of a homography you need to calculate the fundamental matrix (which emgucv will do for you). The fundamental matrix is a combination
of the camera intrinsic matrix (K), the relative rotation (R) and translation (t) between the two views. Recovering the rotation and translation is pretty straight forward if you know K. It looks like emgucv has methods for camera calibration. I am not familiar
with their particular method but these generally involve taking several images of a scene with know geometry.
Thanks jlewis42 for pay attention on this matter. – mili Feb 17 '12 at 3:21
But I calculate fundamental matrix as u said(using random generate points and project it using homography) and also I calculate thecamera intrinsic matrix using chess board method of
EmguCV. But I can not find any method to get R and T directly from fundamentalmatrix. After that I calculate the essential matrix as describe in here and get the R and T as describe in here so It did not give me anacceptable
answer. Where could be the error? – mili Feb 17 '12 at 3:44 1
Unfortunately I can't answer that without knowing how the result is wrong. Here's a couple of things to look into: Are you sure all of your pointcorrespondences are valid? Check whether
the fundamental matrix is correct using epipolar geometry(en.wikipedia.org/wiki/Epipolar_geometry). Basically if you multiply a point in the left image by the fundamental matrix it will give you theequation
of a line in the right image (in ax + by + c = 0 form). The corresponding point in the right image will lie on that line. Also try recombining K, R and t to see if you get back the same F – jlewis42 Feb 17 '12 at 17:22
Thanks jlewis42 I will take a look at these things and trying to fix the errors. again thanks. – mili Feb 29 '12 at 1:06
To figure out camera motion (exact rotation and translation up to a scaling factor) you need
Calculate fundamental matrix F, for example, using eight-point algorithm
Calculate Essential matrix E = A’FA, where A is intrinsic camera matrix
Decompose E which is by definition Tx * R via SVD into E=ULV’
Create a special 3x3 matrix
0 -1 0
W = 1 0 0
0 0 1
that helps to run decomposition:
R = UW-1VT, Tx = ULWUT, where
0 -tx ty
Tx = tz 0 -tx
-ty tx 0
Since E can have an arbitrary sign and W can be replace by Winv we have 4 distinct solution and have to select the one which produces most points in front of the camera.
It's been a while since you asked this question. By now, there are some good references on this problem.
One of them is "invitation to 3D image" by Ma, chapter 5 of it is free here http://vision.ucla.edu//MASKS/chapters.html
Also, Vision Toolbox of Peter Corke includes the tools to perform this. However, he does not explain much math of the decomposition