int cvFindContours( void* img, CvMemStorage* storage, CvSeq** firstContour,int cntHeaderSize, int mode, int method, CvPoint offset );
百度释意:
讨论部分cvDrawContours中的案例显示了任何使用轮廓检测连通区域。轮廓可以用于形状分析和目标识别——可以参考文件夹OpenCV sample中的squares.c
OpenCV1.0版本例子
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
int main(int argc, char* argv[])
{
IplImage* src;
int area;
src=cvLoadImage("E:\\grayimage.jpg",0); //加载并显示原始灰度图像
cvNamedWindow( "PIC", 1);
cvShowImage("PIC",src);
IplImage* dst = cvCreateImage(cvGetSize(src), 8, 3 ); //申请三通道的目标图像
int num=0;
int count=0;
CvMemStorage* storage =cvCreateMemStorage(0);
CvSeq* contour = 0;
cvThreshold( src, src, 100,255.0,CV_THRESH_BINARY ); //二值化
cvFindContours( src, storage,&contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );//二值图轮廓检索;
cvZero( dst );
//遍历每个轮廓
for( ; contour != 0; contour =contour->h_next )
{
//获取当前轮廓面积,并用随即颜色填充
area= int(fabs(cvContourArea( contour, CV_WHOLE_SEQ ))); //获取当前面积
num=num+area;
count=count+1;
printf("%d:area== %d\n", count,area);
CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255 ); //随即颜色
cvDrawContours( dst, contour,color, color, -1, CV_FILLED, 8 ); //随即颜色填充
}
printf("num == %d\n",num);
printf("rectarea:%d",src->width*src->height);
cvNamedWindow("Components", 1 );
cvShowImage("Components", dst );
cvWaitKey(0);
return 0;
}
原图:
结果图: