Euler angles determination for rectangle perspective

asked 2013-07-01 13:21:38 -0500

bboy gravatar image

Hi everyone!

I've a problem since last week, on a program for real-time detection based on Canny + find contours whose goal is to detect a rectangle. This part of my program works well!

Now I would like to determine in real-time the three Euler angles in order to have the orientation of my rectangle. For that, I create a "virtual" rectangle with four points and calculate the transformation matrix with the four corners of my detected rectangle. In order to first validate my calculation of these angmes, I've tried different methods:

-I create four points who represents a rectangle -I create four points who will represent my detected rectangle -I use getPerspectiveTransform or findHomography to obtain transformation matrix (both do the same matrix) -I use RCDecomp3x3 to get my three rotation matrices -I calculate my three angles from Qx,Qy and Qz, these three rotation matrices.

I've also tried to calculate global rotation matrix QxQyQz and calculate the angles from it (with atan2 formulas). I've also tried with 5 points (the center of both rectangles and their corners).

My problem is that my values for X and Y are false, because X is always 0 and Y is 0 or 180. Only the Z value changes!

Do you know where is my problem?

Thanks a lot!

Here is my program, I can change the points of the detected rectangle to test every angle.

Mat image = Mat::ones(600,400,CV_32F);
Mat dst2;

//A reference rectangle
vector<Point2f> source(5);
source[0] = Point(100,200);
source[1] = Point(100,100);
source[2] = Point(200,100);
source[3] = Point(200,200);
source[4] = Point(150,150);
line(image,source[0],source[1],Scalar(0,0,0));
line(image,source[1],source[2],Scalar(0,0,0));
line(image,source[2],source[3],Scalar(0,0,0));
line(image,source[3],source[0],Scalar(0,0,0));
circle(image,source[4],2,Scalar(0,0,0));

//My detected rectangle
vector<Point2f> dst(5);
dst[0] = Point(100,200);    
dst[1] = Point(180,180);      
dst[2] = Point(280,180);    
dst[3] = Point(200,200);        
dst[4] = Point(190,190);
circle(image,dst[4],2,Scalar(0,0,0));
line(image,dst[0],dst[1],Scalar(0,0,0));
line(image,dst[1],dst[2],Scalar(0,0,0));
line(image,dst[2],dst[3],Scalar(0,0,0));
line(image,dst[3],dst[0],Scalar(0,0,0));

Mat mtxr,mtxq,qx,qy,qz;
Mat matrice = findHomography(source,dst);

RQDecomp3x3(matrice,mtxr,mtxq,qx,qy,qz);
cout<<"Angle x = "<<180*(acos(qx.at<double>(1,1)))/3.14159<<endl;
cout<<"Angle y = "<<180*(acos(qy.at<double>(0,0)))/3.14159<<endl;
cout<<"Angle z = "<<180*(acos(qz.at<double>(0,0)))/3.14159<<endl;

//A second example rectangle
perspectiveTransform(source,dst,matrice);
vector<Point2f> source2(4);
source2[0] = Point(100,500);
source2[1] = Point(100,300);
source2[2] = Point(300,300);
source2[3] = Point(300,500);
line ...
(more)
edit retag flag offensive close merge delete