利用cvMinAreaRect2求取轮廓最小外接矩形

转自:http://blog.csdn.net/mine1024/article/details/6044856

对给定的 2D 点集,寻找最小面积的包围矩形,使用函数:

CvBox2D cvMinAreaRect2( const CvArr * points, CvMemStorage * storage = NULL );

points
点序列或点集数组 storage 可选的临时存储仓 函数 cvMinAreaRect2 通过建立凸外形并且旋转外形以寻找给定 2D 点集的最小面积的包围矩形。

其中返回的2D盒子定义如下:

1 typedef struct CvBox2D
2 {
3 CvPoint2D32f center; /* 盒子的中心 */
4 CvSize2D32f size; /* 盒子的长和宽 */
5 float angle; /* 水平轴与第一个边的夹角,用弧度表示 */
6 } CvBox2D;
注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角。 如下图

2010-11-25 9-05-42

可用函数 cvBoxPoints(box[count], point); 寻找盒子的顶点

1 void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[ 4 ] )
2 {
3 double angle = box . angle * CV_PI / 180 .
4 float a = ( float )cos(angle) * 0 . 5f;
5 float b = ( float )sin(angle) * 0 . 5f;
6
7 pt[ 0 ] . x = box . center . x - a * box . size . height - b * box . size . width;
8 pt[ 0 ] . y = box . center . y + b * box . size . height - a * box . size . width;
9 pt[ 1 ] . x = box . center . x + a * box . size . height - b * box . size . width;
10 pt[ 1 ] . y = box . center . y - b * box . size . height - a * box . size . width;
11 pt[ 2 ] . x = 2 * box . center . x - pt[ 0 ] . x;
12 pt[ 2 ] . y = 2 * box . center . y - pt[ 0 ] . y;
13 pt[ 3 ] . x = 2 * box . center . x - pt[ 1 ] . x;
14 pt[ 3 ] . y = 2 * box . center . y - pt[ 1 ] . y;
15 }
简单证明此函数的计算公式:
计算 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 略。

写了个函数绘制CvBox2D

1 void DrawBox(CvBox2D box,IplImage * img)
2 {
3 CvPoint2D32f point[ 4 ];
4 int i;
5 for ( i = 0 ; i < 4; i + + )
6 {
7 point[i] . x = 0 ;
8 point[i] . y = 0 ;
9 }
10 cvBoxPoints(box, point); // 计算二维盒子顶点
11 CvPoint pt[ 4 ];
12 for ( i = 0 ; i < 4; i + + )
13 {
14 pt[i] . x = ( int )point[i] . x;
15 pt[i] . y = ( int )point[i] . y;
16 }
17 cvLine( img, pt[ 0 ], pt[ 1 ],CV_RGB( 255 , 0 , 0 ), 2 , 8 , 0 );
18 cvLine( img, pt[ 1 ], pt[ 2 ],CV_RGB( 255 , 0 , 0 ), 2 , 8 , 0 );
19 cvLine( img, pt[ 2 ], pt[ 3 ],CV_RGB( 255 , 0 , 0 ), 2 , 8 , 0 );
20 cvLine( img, pt[ 3 ], pt[ 0 ],CV_RGB( 255 , 0 , 0 ), 2 , 8 , 0 );
21 }

转自:http://www.opencv.org.cn/forum/viewtopic.php?t=8886

CvBox2D和CvRect最大的区别在于Box是带有角度angle的,而Rect则只能是水平和垂直。
相应的,绘制Box时就不能用cvRectangle()了,可以先算出Box的4个顶点,再用多边形绘制函数cvPolyLine()来画出Box。
同理,要找到最大的矩形,就不能用cvMaxRect,可以逐个计算每个Box的面积 box_area = box.size.width * box.size.height 来找出最大矩形。

//找出完整包含轮廓的最小矩形
CvBox2D rect = cvMinAreaRect2(contour);
//用cvBoxPoints找出矩形的4个顶点
CvPoint2D32f rect_pts0[4];
cvBoxPoints(rect, rect_pts0);
//因为cvPolyLine要求点集的输入类型是CvPoint**
//所以要把 CvPoint2D32f 型的 rect_pts0 转换为 CvPoint 型的 rect_pts
//并赋予一个对应的指针 *pt
int npts = 4;
CvPoint rect_pts[4], *pt = rect_pts;
for (int rp=0; rp<4; rp++)
rect_pts[rp]= cvPointFrom32f(rect_pts0[rp]);
//画出Box
cvPolyLine(dst_img, &pt, &npts, 1, 1, CV_RGB(255,0,0), 2);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值