cvFindContours使用方法

原文地址:cvFindContours使用方法 作者:海上灯塔
我画了四个椭圆,用cvFindContours函数找轮廓,并且输出轮廓数目和轮廓图像,但是输出的轮廓数目是8,不知道为什么?如何求找到的轮廓数目呢?还有如何访问到每个轮廓中的点呢?
代码如下:
#include "stdafx.h"
#include
#include
#include

using namespace std;

int main()
{
IplImage *img=cvCreateImage(cvSize(320,240),8,1);
cvZero(img);
//在图像上画四个同心椭圆
cvEllipse(img,cvPoint(160,120),cvSize(100,50),0,0,360,cvScalarAll(255));
cvEllipse(img,cvPoint(160,120),cvSize(60,30),0,0,360,cvScalarAll(255));
cvEllipse(img,cvPoint(160,120),cvSize(50,20),0,0,360,cvScalarAll(255));
cvEllipse(img,cvPoint(160,120),cvSize(40,10),0,0,360,cvScalarAll(255));

CvMemStorage *storage=cvCreateMemStorage(0);
CvSeq *contours=0;
cvFindContours(img,storage,&contours);

cvZero(img);
cvDrawContours(img,contours,cvScalarAll(50),cvScalarAll(50),100);
int num=1;

while(contours->h_next)
{
num++;
contours=contours->h_next;
}
cout<<"有"<

cvNamedWindow("Contours",1);
cvShowImage("Contours",img);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyWindow("Contours");
return 0;
}
按我的理解,轮廓应该分为外部边界和孔,你程序中的每个椭圆其实也应该有两个轮廓,也就是被统计了两次,所以总共有8个轮廓;如果想只统计外部边界轮廓 ,可以将修改程序中,cvFindContours(img,storage,&contours,sizeof(CvContour),CV_RETR_CCOMP);cvDrawContours(img,contours,cvScalarAll(50),cvScalarAll(50),1); CV_RETR_CCOMP方法将所有轮廓组织成双层结构,上层是所有外部边界,而设置max_level为1则只画出属于同一等级的所有轮廓; 这样就应该只会统计4次;

你可能感兴趣的:(cvFindContours使用方法)