首页新闻找找看学习计划

Open CV 新人请教:检测轮廓数目不准确

0
悬赏园豆:40 [已关闭问题] 关闭于 2016-08-19 20:15

新人求教!
小弟编写一个简单的程序,程序功能是画出轮廓图并显示轮廓数量。
自己简单做了一张有10个轮廓的图做测试,结果画出是个轮廓,
1.当采用 CV_RETR_TREE模式时,画出10个轮廓显示轮廓数量为14;
2.采用CV_RETR_EXTERNAL模式时,只画出最外轮廓显示轮廓数量为8;
3.采用CV_RETR_LIST和 CV_RETR_CCOMP模式时,画出10个轮廓,显示轮廓数量为8;
求教各位大神我是哪里出了问题,怎么修改代码可以成功画出全部轮廓并正确显示轮廓数量!
代码以及截图如下:

 

 1 #include "cv.h"
 2 #include "highgui.h"
 3 #include <stdio.h>
 4 #include <math.h>
 5 #include <iostream>
 6 #include <ctime>
 7 using namespace cv;
 8 CvSeq *contourSeq = NULL;
 9 
10 IplImage* ImageThreshold(IplImage* src)
11 {
12     IplImage *gray,*binaryim;
13     int height,width;
14     gray=cvCreateImage(cvGetSize(src),src->depth,1);
15     cvCvtColor(src,gray,CV_BGR2GRAY);
16     height=gray->height;
17     width=gray->width;
18     printf("The canvas'width is :%d,  height is :%d\n\n",width,height);
19 
20     binaryim=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
21     cvThreshold(gray,binaryim,128,255,CV_THRESH_BINARY_INV);
22     return binaryim;
23 
24 }
25 
26 IplImage* printlunkuo(IplImage *binaryim)
27 {
28     CvMemStorage *contourStorage=cvCreateMemStorage();
29     cvFindContours(binaryim, contourStorage, &contourSeq, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 
30     
31     int contourcount=contourSeq->total;
32     printf("contour count is %d",contourcount);
33 
34     IplImage *pOutlineImage = cvCreateImage(cvGetSize(binaryim), IPL_DEPTH_8U, 3);    
35     int nLevels = 5;
36 
37     cvRectangle(pOutlineImage, cvPoint(0, 0), cvPoint(pOutlineImage->width, pOutlineImage->height), CV_RGB(0,0,0), CV_FILLED);    
38     cvDrawContours(pOutlineImage, contourSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), nLevels,0.5); 
39     return pOutlineImage;
40     
41 
42 
43 }
44 
45 
46 int main(int argc,char** argv)
47 {
48     IplImage *src,*binaryim,*contourim;
49     src=cvLoadImage("gtest2.bmp",1);
50     binaryim=ImageThreshold(src);
51     contourim = printlunkuo(binaryim);
52     
53     cvNamedWindow("Binary_image",0);    
54     cvShowImage("Binary_image",binaryim);
55     cvNamedWindow("Contour_image",0);    
56     cvShowImage("Contour_image",contourim);
57     cvWaitKey(0);
58     cvDestroyWindow("Binary_image");
59     cvReleaseImage(&binaryim);
60     cvDestroyWindow("Contours_image");
61     cvReleaseImage(&contourim);
62 
63 
64 }

                      原图

 

   轮廓图

轮廓数量

 

 

 

Henry2017的主页 Henry2017 | 初学一级 | 园豆:8
提问于:2016-08-02 17:34
< >
分享
所有回答(1)
0

contourcount=contourSeq->total;

表示当前contourSeq指针所指序列所包含的的point数量

要求轮廓数,可以设置一个flag遍历contourSeq

Henry2017 | 园豆:8 (初学一级) | 2016-08-19 20:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册