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计算整个或部分轮廓的面积。在计算部分轮廓的情况时,由轮廓弧线和连接两端点的弦围成的区域总面积被计算,如下图所示:

注意:轮廓的位置将影响区域面积的符号,因此函数范围的有可能是负值。可以使用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) );画线。最后结果如下图:

测试图像以及完整代码下载:

没有什么可留恋,只有抑制不住的梦想,

opencv轮廓的简单应用

相关文章:

你感兴趣的文章:

标签云: