unified the coordinate interpretation in RotatedRect (ticket #425)

This commit is contained in:
Vadim Pisarevsky 2010-11-29 18:14:08 +00:00
parent 4a973d4633
commit a937d9d43c
8 changed files with 51 additions and 39 deletions

View File

@ -1765,34 +1765,6 @@ inline RotatedRect::operator CvBox2D() const
CvBox2D box; box.center = center; box.size = size; box.angle = angle; CvBox2D box; box.center = center; box.size = size; box.angle = angle;
return box; return box;
} }
inline void RotatedRect::points(Point2f pt[]) const
{
double _angle = angle*CV_PI/180.;
float a = (float)cos(_angle)*0.5f;
float b = (float)sin(_angle)*0.5f;
pt[0].x = center.x - a*size.height - b*size.width;
pt[0].y = center.y + b*size.height - a*size.width;
pt[1].x = center.x + a*size.height - b*size.width;
pt[1].y = center.y - b*size.height - a*size.width;
pt[2].x = 2*center.x - pt[0].x;
pt[2].y = 2*center.y - pt[0].y;
pt[3].x = 2*center.x - pt[1].x;
pt[3].y = 2*center.y - pt[1].y;
}
inline Rect RotatedRect::boundingRect() const
{
Point2f pt[4];
points(pt);
Rect r(cvFloor(min(min(min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
cvFloor(min(min(min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
cvCeil(max(max(max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
cvCeil(max(max(max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
r.width -= r.x - 1;
r.height -= r.y - 1;
return r;
}
//////////////////////////////// Scalar_ /////////////////////////////// //////////////////////////////// Scalar_ ///////////////////////////////

View File

@ -877,7 +877,7 @@ void ellipse2Poly( Point center, Size axes, int angle,
y = size_b * SinTable[angle]; y = size_b * SinTable[angle];
Point pt; Point pt;
pt.x = cvRound( cx + x * alpha - y * beta ); pt.x = cvRound( cx + x * alpha - y * beta );
pt.y = cvRound( cy - x * beta - y * alpha ); pt.y = cvRound( cy + x * beta + y * alpha );
if( pt != prevPt ) if( pt != prevPt )
pts.push_back(pt); pts.push_back(pt);
} }

View File

@ -3043,6 +3043,37 @@ void normalize( const SparseMat& src, SparseMat& dst, double a, int norm_type )
src.convertTo( dst, -1, scale ); src.convertTo( dst, -1, scale );
} }
////////////////////// RotatedRect //////////////////////
void RotatedRect::points(Point2f pt[]) const
{
double _angle = angle*CV_PI/180.;
float b = (float)cos(_angle)*0.5f;
float a = (float)sin(_angle)*0.5f;
pt[0].x = center.x - a*size.height - b*size.width;
pt[0].y = center.y + b*size.height - a*size.width;
pt[1].x = center.x + a*size.height - b*size.width;
pt[1].y = center.y - b*size.height - a*size.width;
pt[2].x = 2*center.x - pt[0].x;
pt[2].y = 2*center.y - pt[0].y;
pt[3].x = 2*center.x - pt[1].x;
pt[3].y = 2*center.y - pt[1].y;
}
inline Rect RotatedRect::boundingRect() const
{
Point2f pt[4];
points(pt);
Rect r(cvFloor(min(min(min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
cvFloor(min(min(min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
cvCeil(max(max(max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
cvCeil(max(max(max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
r.width -= r.x - 1;
r.height -= r.y - 1;
return r;
}
} }

View File

@ -415,9 +415,9 @@ cvMinAreaRect2( const CvArr* array, CvMemStorage* storage )
icvRotatingCalipers( points, n, CV_CALIPERS_MINAREARECT, (float*)out ); icvRotatingCalipers( points, n, CV_CALIPERS_MINAREARECT, (float*)out );
box.center.x = out[0].x + (out[1].x + out[2].x)*0.5f; box.center.x = out[0].x + (out[1].x + out[2].x)*0.5f;
box.center.y = out[0].y + (out[1].y + out[2].y)*0.5f; box.center.y = out[0].y + (out[1].y + out[2].y)*0.5f;
box.size.height = (float)sqrt((double)out[1].x*out[1].x + (double)out[1].y*out[1].y); box.size.width = (float)sqrt((double)out[1].x*out[1].x + (double)out[1].y*out[1].y);
box.size.width = (float)sqrt((double)out[2].x*out[2].x + (double)out[2].y*out[2].y); box.size.height = (float)sqrt((double)out[2].x*out[2].x + (double)out[2].y*out[2].y);
box.angle = (float)atan2( -(double)out[1].y, (double)out[1].x ); box.angle = (float)atan2( (double)out[1].y, (double)out[1].x );
} }
else if( n == 2 ) else if( n == 2 )
{ {
@ -425,9 +425,9 @@ cvMinAreaRect2( const CvArr* array, CvMemStorage* storage )
box.center.y = (points[0].y + points[1].y)*0.5f; box.center.y = (points[0].y + points[1].y)*0.5f;
double dx = points[1].x - points[0].x; double dx = points[1].x - points[0].x;
double dy = points[1].y - points[0].y; double dy = points[1].y - points[0].y;
box.size.height = (float)sqrt(dx*dx + dy*dy); box.size.width = (float)sqrt(dx*dx + dy*dy);
box.size.width = 0; box.size.height = 0;
box.angle = (float)atan2( -dy, dx ); box.angle = (float)atan2( dy, dx );
} }
else else
{ {

View File

@ -275,7 +275,13 @@ cvCamShift( const void* imgProb, CvRect windowIn,
{ {
box->size.height = (float)length; box->size.height = (float)length;
box->size.width = (float)width; box->size.width = (float)width;
box->angle = (float)(theta*180./CV_PI); box->angle = (float)((CV_PI*0.5+theta)*180./CV_PI);
while(box->angle < 0)
box->angle += 360;
while(box->angle >= 360)
box->angle -= 360;
if(box->angle >= 180)
box->angle -= 180;
box->center = cvPoint2D32f( comp.rect.x + comp.rect.width*0.5f, box->center = cvPoint2D32f( comp.rect.x + comp.rect.width*0.5f,
comp.rect.y + comp.rect.height*0.5f); comp.rect.y + comp.rect.height*0.5f);
} }

View File

@ -130,7 +130,6 @@ int main( int argc, char** argv )
backproj &= mask; backproj &= mask;
RotatedRect trackBox = CamShift(backproj, trackWindow, RotatedRect trackBox = CamShift(backproj, trackWindow,
TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 )); TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
trackBox.angle = 90-trackBox.angle;
if( backprojMode ) if( backprojMode )
cvtColor( backproj, image, CV_GRAY2BGR ); cvtColor( backproj, image, CV_GRAY2BGR );

View File

@ -71,12 +71,16 @@ void processImage(int h, void*)
Mat(contours[i]).convertTo(pointsf, CV_32F); Mat(contours[i]).convertTo(pointsf, CV_32F);
RotatedRect box = fitEllipse(pointsf); RotatedRect box = fitEllipse(pointsf);
box.angle = -box.angle;
if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 ) if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
continue; continue;
drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8); drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8);
ellipse(cimage, box, Scalar(0,0,255), 1, CV_AA); ellipse(cimage, box, Scalar(0,0,255), 1, CV_AA);
ellipse(cimage, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, CV_AA);
Point2f vtx[4];
box.points(vtx);
for( int j = 0; j < 4; j++ )
line(cimage, vtx[j], vtx[(j+1)%4], Scalar(0,255,0), 1, CV_AA);
} }
imshow("result", cimage); imshow("result", cimage);

View File

@ -134,7 +134,7 @@ void CV_TrackBaseTest::generate_object()
double width = box0.size.width*0.5; double width = box0.size.width*0.5;
double height = box0.size.height*0.5; double height = box0.size.height*0.5;
double angle = box0.angle*CV_PI/180.; double angle = box0.angle*CV_PI/180.;
double a = cos(angle), b = sin(angle); double a = sin(angle), b = -cos(angle);
double inv_ww = 1./(width*width), inv_hh = 1./(height*height); double inv_ww = 1./(width*width), inv_hh = 1./(height*height);
img = cvCreateMat( img_size.height, img_size.width, img_type ); img = cvCreateMat( img_size.height, img_size.width, img_type );