cvFindContours使用方法

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

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<<"有"<<num<<"个轮廓"<<endl;


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值