opencv轮廓相关的函数

typedef struct CvSeq
{
    CV_SEQUENCE_FIELDS()
}
CvSeq;

#define CV_CONTOUR_FIELDS()  \
    CV_SEQUENCE_FIELDS()     \
    CvRect rect;             \
    int color;               \
    int reserved[3];

typedef struct CvContour
{
    CV_CONTOUR_FIELDS()
}
CvContour;

 

则:CvContour比CvSeq多了如下三个参数

CvRect rect;             \连通区域的外接矩形

int color;               \

int reserved[3];

 

opencv轮廓的简单应用:
相关函数如下:
cvFindContours,cvThreshold,cvContourArea,cvArcLength,cvBoundingRect,cvMinAreaRect2,cvBoxPoints,cvMinEnclosingCircle,cvDrawContours。
1.查找轮廓
2.按地址依次取出轮廓
3.计算轮廓的相关参数
4、根据实际需求做取舍,画出轮廓

1.cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,
                            int header_size CV_DEFAULT(sizeof(CvContour)),
                            int mode CV_DEFAULT(CV_RETR_LIST),
                            int method CV_DEFAULT(CV_CHAIN_APPROX_SIMPLE),
                            CvPoint offset CV_DEFAULT(cvPoint(0,0)));
查找轮廓,返回查找到轮廓总数,使用该函数时输入图像会被直接涂改,如果是还有用的图像,应复制后,传递图像副本,使用该函数前应先将图像转化为二值化图像,可使用下面的函数
cvThreshold( const CvArr*  src, CvArr*  dst,
                          double  threshold, double  max_value,
                          int threshold_type );

2.for (CvSeq *c = first_contour;c !=NULL;c = c->h_next) {}

3.double cvContourArea( const CvArr* contour, CvSlice slice=CV_WHOLE_SEQ );

contour:轮廓(顶点的序列或数组)。
slice:感兴趣区轮廓部分的起点和终点,默认计算整个轮廓的面积。
函数cvContourArea计算整个或部分轮廓的面积。在计算部分轮廓的情况时,由轮廓弧线和连接两端点的弦

围成的区域总面积被计算,如下图所示:

contoursecarea.png

注意:轮廓的位置将影响区域面积的符号,因此函数范围的有可能是负值。可以使用C运行时函数fabs()来

得到面积的绝对值。


cvArcLength( const void* curve,
                            CvSlice slice CV_DEFAULT(CV_WHOLE_SEQ),
                            int is_closed CV_DEFAULT(-1));


计算轮廓长度。

cvBoundingRect( CvArr* points, int update CV_DEFAULT(0) );

计算轮廓的最小正外接矩形,返回 CvRect,矩形边界与图形边界平行

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

计算轮廓的最小外接矩形,返回 CvBox2D,矩形边界可能与边界有一点角度,可通过

cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] );获取矩形的4个顶点,再依次连接顶点即可。

cvMinEnclosingCircle( const CvArr* points,CvPoint2D32f* center, float* radius );

计算轮廓的最小外接圆。


4.画出轮廓cvDrawContours( CvArr *img, CvSeq* contour,
                            CvScalar external_color, CvScalar hole_color,
                            int max_level, int thickness CV_DEFAULT(1),
                            int line_type CV_DEFAULT(8),
                            CvPoint offset CV_DEFAULT(cvPoint(0,0)));



画出矩形cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2,
                          CvScalar color, int thickness CV_DEFAULT(1),
                          int line_type CV_DEFAULT(8),
                          int shift CV_DEFAULT(0));


如:cvRectangle(contourimage,cvPoint(testrect.x,testrect.y+testrect.height),cvPoint(testrect.x+testrect.width,testrect.y),cvScalar(0,0,0),2);

画圆cvCircle( CvArr* img, CvPoint center, int radius,
                       CvScalar color, int thickness CV_DEFAULT(1),
                       int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0));


画线cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
                     CvScalar color, int thickness CV_DEFAULT(1),
                     int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );


最后结果如下图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值