angle from cvFitEllipse2

asked 2013-11-28 01:24:42 -0500

vandelfi gravatar image

Hi,

I'm doing some head tracking and I'm trying to estimate the roll angel by using openCV to fit an ellipse to the head contour. That all works fine. I would like to use the angle of the box to calculate the roll angle, i.e. the angle between the y axis of the image and the long axis of the ellipse.

It says that the box angle is the angle between the horizonal axis and the first side (i.e length), but the results that I get are somewhat strange.

  • The angel is 180° in case my head is upward.
  • When I tilt to the right the angle decreases to around 135° normally and then jumps suddenly to around 315°.
  • When I tilt to the left the angle rises normally until I get to 315° and jumps/changes suddenly to 135°

Here's short video of this: cvBox2D angle

Any idea what's going on and how I could calculate the roll angle properly?

Here is my code, but I would guess that what's happening is from the angle calculations done in cvFitEllipse2:

  CvBox2D box;
  CvPoint center;
  CvSize size;
  int count = largest_contour->total;

  CvMat* points_f = cvCreateMat( 1, count, CV_32FC2 );
  CvMat points_i = cvMat( 1, count, CV_32SC2, points_f->data.ptr );
  cvCvtSeqToArray(largest_contour, points_f->data.ptr, CV_WHOLE_SEQ );
  cvConvert( &points_i, points_f );

  box = cvFitEllipse2(points_f);
  center = cvPointFrom32f(box.center);
  size.width = cvRound(box.size.width*0.5);
  size.height = cvRound(box.size.height*0.5);

  cvEllipse(dst,center,size,box.angle,0,360,cvScalar(0,255,0),2,8,0);
edit retag flag offensive close merge delete