java findcontour_cvFindContours获取轮廓数目和轮廓图像

cvFindContours使用方法

我画了三个椭圆,用cvFindContours函数找轮廓,并且输出轮廓数目和轮廓图像,但是输出的轮廓数目是6,不知道为什么? 首先要配置下环境,即:cv highgui ml 的配置。下面是代码在Win32下运行的,如下:

#include "stdafx.h"

#include

#include

#include

using namespace std;

int main ()

{

IplImage *img =

cvCreateImage(cvSize(640,480),8,1);

cvZero(img);

cvEllipse(img,cvPoint(320,240),cvSize(200,100),0,0,360,cvScalarAll(255));

cvEllipse(img,cvPoint(320,240),cvSize(120,60),0,0,360,cvScalarAll(255));

cvEllipse(img,cvPoint(320,240),cvSize(100,20),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;

}

按我的理解,轮廓应该分为外部边界和孔,你程序中的每个椭圆其实也应该有两个轮廓,也就是被统计了两次,所以总共有6个轮廓;如果想只统计外部边界轮廓,可以将修改程序中,cvFindContours(img,storage,&contours,sizeof(CvContour),CV_RETR_CCOMP);cvDrawContours(img,contours,cvScalarAll(50),cvScalarAll(50),1);

CV_RETR_CCOMP方法将所有轮廓组织成双层结构,上层是所有外部边界,而设置max_level为1则只画出属于同一等级的所有轮廓;这样就应该只会统计3次;

图如下:

a4c26d1e5885305701be709a3d33442f.pnga4c26d1e5885305701be709a3d33442f.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值