# Euler angles determination for rectangle perspective

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 = Point(100,200);
source = Point(100,100);
source = Point(200,100);
source = Point(200,200);
source = Point(150,150);
line(image,source,source,Scalar(0,0,0));
line(image,source,source,Scalar(0,0,0));
line(image,source,source,Scalar(0,0,0));
line(image,source,source,Scalar(0,0,0));
circle(image,source,2,Scalar(0,0,0));

//My detected rectangle
vector<Point2f> dst(5);
dst = Point(100,200);
dst = Point(180,180);
dst = Point(280,180);
dst = Point(200,200);
dst = Point(190,190);
circle(image,dst,2,Scalar(0,0,0));
line(image,dst,dst,Scalar(0,0,0));
line(image,dst,dst,Scalar(0,0,0));
line(image,dst,dst,Scalar(0,0,0));
line(image,dst,dst,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 = Point(100,500);
source2 = Point(100,300);
source2 = Point(300,300);
source2 = Point(300,500);
line ...
edit retag close merge delete