opencv矩形轮廓提取

转自:http://blog.youkuaiyun.com/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); 寻找盒子的顶点

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;
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

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 ;
    }
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);

OpenCV实现矩形轮廓检测可借助其强大的轮廓检测和处理功能,这在计算机视觉和图像处理的对象识别、形状分析等领域应用广泛[^1]。 首先使用`findContours`函数提取目标轮廓,该函数的输入图像要求是一幅二值图像,其输出为每一个连通区域的轮廓点的集合(`vector<vector<Point>>`),外层`vector`的大小代表图像中轮廓的个数,里层`vector`的大小代表轮廓上点的个数[^2]。示例代码如下: ```python import cv2 import numpy as np # 读取图像并转换为灰度图 image = cv2.imread('your_image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 进行二值化处理 _, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 查找轮廓 contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ``` 在调用`findContours`函数时,可通过不同的模式参数来控制检测的轮廓类型,如使用`CV_RETR_EXTERNAL`只检测外部轮廓[^3]。 找到轮廓后,可对每个轮廓进行矩形拟合,使用`cv2.minAreaRect`函数获取轮廓的最小外接矩形,使用`cv2.boundingRect`函数获取轮廓的直边界矩形。示例代码如下: ```python for contour in contours: # 获取最小外接矩形 min_rect = cv2.minAreaRect(contour) box = cv2.boxPoints(min_rect) box = np.int0(box) cv2.drawContours(image, [box], 0, (0, 255, 0), 2) # 获取直边界矩形 x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) # 显示结果 cv2.imshow('Rectangular Contours', image) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值