Cvbox2D(RotatedRect)的解析与应用

       

Cvbox2D(RotatedRect)的解析与应用 

kezunhai@gmail.com

http://blog.csdn.net/kezunhai

         在Opencv中,通过函数调用或传参数通常会遇到CvBox2D类型的结构体。如对对给定的 2D 点集或某一轮廓点集,进行椭圆拟合或寻找最小面积的包围矩形,使用函数:

		CvBox2D box = cvFitEllipse2( contours);
		CvBox2D boxRect = cvMinAreaRect2(contours);
其中CvBox2D的定义如下:

typedef struct CvBox2D
{
    CvPoint2D32f center;  /* Center of the box.                          */
    CvSize2D32f  size;    /* Box width and length.                       */
    float angle;          /* Angle between the horizontal axis           */
                          /* and the first side (i.e. length) in degrees */
}
CvBox2D;
注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角 ,如下图:

通过对opencv底层实现的查看,可知函数 cvBoxPoints(box[count], point); 是用于求取盒子的顶点的函数,该函数cvBoxPoints的定义如下:

void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] ) 
{ 
	double angle = box.angle*CV_PI/180. 
	float a = (float)cos(angle)*0.5f; 
	float b = (float)sin(angle)*0.5f; 

	pt[0].x = box.center.x - a*box.size.height - b*box.size.width; 
	pt[0].y = box.center.y + b*box.size.height - a*box.size.width; 
	pt[1].x = box.center.x + a*box.size.height - b*box.size.width; 
	pt[1].y = box.center.y - b*box.size.height - a*box.size.width; 
	pt[2].x = 2*box.center.x - pt[0].x; 
	pt[2].y = 2*box.center.y - pt[0].y; 
	pt[3].x = 2*box.center.x - pt[1].x; 
	pt[3].y = 2*box.center.y - pt[1].y; 
}

简单证明此函数的计算公式。计算x可由下面三个方程求得:
pt[1].x - pt[0].x = width*sin(angle) 
pt[2].x - pt[1].x = height*cos(angle) 
pt[2].x - pt[0].x = 2(box.center.x - pt[0].x)
y可同理求得。
    由下面函数可以讲求得的点画出来:
void DrawBox(CvBox2D box,IplImage* img) 
{ 
	CvPoint2D32f point[4]; 
	int i; 
	for ( i=0; i<4; i++) 
	{ 
		point[i].x = 0; 
		point[i].y = 0; 
	} 
	cvBoxPoints(box, point); //计算二维盒子顶点 
	CvPoint pt[4]; 
	for ( i=0; i<4; i++) 
	{ 
		pt[i].x = (int)point[i].x; 
		pt[i].y = (int)point[i].y; 
	} 
	cvLine( img, pt[0], pt[1],CV_RGB(255,0,0), 2, 8, 0 ); 
	cvLine( img, pt[1], pt[2],CV_RGB(255,0,0), 2, 8, 0 ); 
	cvLine( img, pt[2], pt[3],CV_RGB(255,0,0), 2, 8, 0 ); 
	cvLine( img, pt[3], pt[0],CV_RGB(255,0,0), 2, 8, 0 ); 
} 
  
    另外,由于Opencv的升级,在2.x以上的版本中,可以使用RotatedRect类来代替CvBox2D的使用,而且RotatedRect功能更强大,使用更方便,其定义如下:
class CV_EXPORTS RotatedRect
{
public:
    //! various constructors
    RotatedRect();
    RotatedRect(const Point2f& center, const Size2f& size, float angle);
    RotatedRect(const CvBox2D& box);

    //! returns 4 vertices of the rectangle
    void points(Point2f pts[]) const;
    //! returns the minimal up-right rectangle containing the rotated rectangle
    Rect boundingRect() const;
    //! conversion to the old-style CvBox2D structure
    operator CvBox2D() const;

    Point2f center; //< the rectangle mass center
    Size2f size;    //< width and height of the rectangle
    float angle;    //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
};
通过调用boundingRect()函数就可以直接获得外接矩形,而通过调用Points()函数就可以取得四个顶点。

参考资料:

作者:kezunhai出处:http://blog.csdn.net/kezunhai欢迎转载或分享,但请务必声明文章出处。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值