opencv查找轮廓---cvFindContours && cvDrawCountours 用法及例子

环境: vs2008 + opencv2.1

先看,这两个函数的用法(参考 opencv手册):

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

FindContours        在二值图像中寻找轮廓 


int cvFindContours( CvArr* image, CvMemStorage* storage, CvSeq** first_contour,
int header_size=sizeof(CvContour), int mode=CV_RETR_LIST,
int method=CV_CHAIN_APPROX_SIMPLE, CvPoint offset=cvPoint(0,0) );


image 
输入的 8-比特、单通道图像. 非零元素被当成 1, 0 象素值保留为 0 - 从而图像被看成二值的。为了从灰度图像中得到这样的二值图像,可以使用 cvThreshold, cvAdaptiveThreshold 或 cvCanny. 本函数改变输入图像内容。 


storage 
得到的轮廓的存储容器 


first_contour 
输出参数:包含第一个输出轮廓的指针 


header_size 
如果 method=CV_CHAIN_CODE,则序列头的大小 >=sizeof(CvChain),否则 >=sizeof(CvContour) . 


mode 
提取模式. 
CV_RETR_EXTERNAL - 只提取最外层的轮廓 
CV_RETR_LIST - 提取所有轮廓,并且放置在 list 中 
CV_RETR_CCOMP - 提取所有轮廓,并且将其组织为两层的 hierarchy: 顶层为连通域的外围边界,次层为洞的内层边界。 
CV_RETR_TREE - 提取所有轮廓,并且重构嵌套轮廓的全部 hierarchy 


method 
逼近方法 (对所有节点, 不包括使用内部逼近的 CV_RETR_RUNS). 
CV_CHAIN_CODE - Freeman 链码的输出轮廓. 其它方法输出多边形(定点序列). 
CV_CHAIN_APPROX_NONE - 将所有点由链码形式翻译(转化)为点序列形式 
CV_CHAIN_APPROX_SIMPLE - 压缩水平、垂直和对角分割,即函数只保留末端的象素点; 
CV_CHAIN_APPROX_TC89_L1, 
CV_CHAIN_APPROX_TC89_KCOS - 应用 Teh-Chin 链逼近算法. CV_LINK_RUNS - 通过连接为 1 的水平碎片使用完全不同的轮廓提取算法。仅有 CV_RETR_LIST 提取模式可以在本方法中应用. 


offset 
每一个轮廓点的偏移量. 当轮廓是从图像 ROI 中提取出来的时候,使用偏移量有用,因为可以从整个图像上下文来对轮廓做分析. 
函数 cvFindContours 从二值图像中提取轮廓,并且返回提取轮廓的数目。指针 first_contour 的内容由函数填写。它包含第一个最外层轮廓的指针,如果指针为 NULL,则没有检测到轮廓(比如图像是全黑的)。其它轮廓可以从 first_contour 利用 h_next 和 v_next 链接访问到。 在 cvDrawContours 的样例显示如何使用轮廓来进行连通域的检测。轮廓也可以用来做形状分析和对象识别 - 见CVPR2001 教程中的 squares 样例。该教程可以在 SourceForge 网站上找到。 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

DrawContours    在图像中绘制外部和内部的轮廓。 


void cvDrawContours( CvArr *img, CvSeq* contour,
                     CvScalar external_color, CvScalar hole_color,
                     int max_level, int thickness=1,
                     int line_type=8, CvPoint offset=cvPoint(0,0) );
img 
用以绘制轮廓的图像。和其他绘图函数一样,边界图像被感兴趣区域(ROI)所剪切。 


contour  指针指向第一个轮廓。 


external_color   外层轮廓的颜色。 


hole_color   内层轮廓的颜色。 


max_level   绘制轮廓的最大等级。如果等级为0,绘制单独的轮廓。如果为1,绘制轮廓及在其后的相同的级别下轮廓。如果值为2,所有的轮廓。如果等级为2,绘制所有同级轮廓及所有低一级轮廓,诸此种种。如果值为负数,函数不绘制同级轮廓,但会升序绘制直到级别为abs(max_level)-1的子轮廓。 


thickness   绘制轮廓时所使用的线条的粗细度。如果值为负(e.g. =CV_FILLED),绘制内层轮廓。 


line_type   线条的类型。参考cvLine. 


offset  照给出的偏移量移动每一个轮廓点坐标.当轮廓是从某些感兴趣区域(ROI)中提取的然后需要在运算中考虑ROI偏移量时,将会用到这个参数。 


当thickness>=0,函数cvDrawContours在图像中绘制轮廓,或者当thickness<0时,填充轮廓所限制的区域。 


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


code1 :


[cpp]  view plain copy
  1. #include "stdafx.h"    
  2. #include "cxcore.h"     
  3. #include "cv.h"     
  4. #include "highgui.h"    
  5.   
  6.   
  7. // 内轮廓填充     
  8. // 参数:     
  9. // 1. pBinary: 输入二值图像,单通道,位深IPL_DEPTH_8U。    
  10. // 2. dAreaThre: 面积阈值,当内轮廓面积小于等于dAreaThre时,进行填充。     
  11. void FillInternalContours(IplImage *pBinary, double dAreaThre)     
  12. {     
  13.     double dConArea;     
  14.     CvSeq *pContour = NULL;     
  15.     CvSeq *pConInner = NULL;     
  16.     CvMemStorage *pStorage = NULL;     
  17.     // 执行条件     
  18.     if (pBinary)     
  19.     {     
  20.         // 查找所有轮廓     
  21.         pStorage = cvCreateMemStorage(0);     
  22.         cvFindContours(pBinary, pStorage, &pContour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);     
  23.         // 填充所有轮廓     
  24.         cvDrawContours(pBinary, pContour, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 2, CV_FILLED, 8, cvPoint(0, 0));    
  25.         // 外轮廓循环     
  26.         int wai = 0;    
  27.         int nei = 0;    
  28.         for (; pContour != NULL; pContour = pContour->h_next)     
  29.         {     
  30.             wai++;    
  31.             // 内轮廓循环     
  32.             for (pConInner = pContour->v_next; pConInner != NULL; pConInner = pConInner->h_next)     
  33.             {     
  34.                 nei++;    
  35.                 // 内轮廓面积     
  36.                 dConArea = fabs(cvContourArea(pConInner, CV_WHOLE_SEQ));    
  37.                 printf("%f\n", dConArea);     
  38.             }    
  39.             CvRect rect = cvBoundingRect(pContour,0);  
  40.             cvRectangle(pBinary, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),CV_RGB(255,255, 255), 1, 8, 0);  
  41.         }     
  42.   
  43.         printf("wai = %d, nei = %d", wai, nei);    
  44.         cvReleaseMemStorage(&pStorage);     
  45.         pStorage = NULL;     
  46.     }     
  47. }     
  48. int Otsu(IplImage* src)        
  49. {        
  50.     int height=src->height;        
  51.     int width=src->width;            
  52.   
  53.     //histogram        
  54.     float histogram[256] = {0};        
  55.     for(int i=0; i < height; i++)      
  56.     {        
  57.         unsigned char* p=(unsigned char*)src->imageData + src->widthStep * i;        
  58.         for(int j = 0; j < width; j++)       
  59.         {        
  60.             histogram[*p++]++;        
  61.         }        
  62.     }        
  63.     //normalize histogram        
  64.     int size = height * width;        
  65.     for(int i = 0; i < 256; i++)      
  66.     {        
  67.         histogram[i] = histogram[i] / size;        
  68.     }        
  69.   
  70.     //average pixel value        
  71.     float avgValue=0;        
  72.     for(int i=0; i < 256; i++)      
  73.     {        
  74.         avgValue += i * histogram[i];  //整幅图像的平均灰度      
  75.     }         
  76.   
  77.     int threshold;          
  78.     float maxVariance=0;        
  79.     float w = 0, u = 0;        
  80.     for(int i = 0; i < 256; i++)       
  81.     {        
  82.         w += histogram[i];  //假设当前灰度i为阈值, 0~i 灰度的像素(假设像素值在此范围的像素叫做前景像素) 所占整幅图像的比例      
  83.         u += i * histogram[i];  // 灰度i 之前的像素(0~i)的平均灰度值: 前景像素的平均灰度值      
  84.   
  85.         float t = avgValue * w - u;        
  86.         float variance = t * t / (w * (1 - w) );        
  87.         if(variance > maxVariance)       
  88.         {        
  89.             maxVariance = variance;        
  90.             threshold = i;        
  91.         }        
  92.     }        
  93.   
  94.     return threshold;        
  95. }       
  96.   
  97. int main()    
  98. {    
  99.     IplImage *img = cvLoadImage("c://temp.jpg", 0);    
  100.     IplImage *bin = cvCreateImage(cvGetSize(img), 8, 1);    
  101.   
  102.     int thresh = Otsu(img);    
  103.     cvThreshold(img, bin, thresh, 255, CV_THRESH_BINARY);    
  104.   
  105.     FillInternalContours(bin, 200);    
  106.   
  107.     cvNamedWindow("img");    
  108.     cvShowImage("img", img);    
  109.   
  110.     cvNamedWindow("result");    
  111.     cvShowImage("result", bin);    
  112.   
  113.     cvWaitKey(-1);    
  114.   
  115.     cvReleaseImage(&img);    
  116.     cvReleaseImage(&bin);    
  117.   
  118.     return 0;    
  119. }    


result1:



这种情况下,大月亮内部的两个内轮廓没有框出来。这个不是因为我的 rect框是 白色的缘故。。。。应该。

我断点试了,就 cvRectangle 了 4次···


code2:


[cpp]  view plain copy
  1. // test.cpp : 定义控制台应用程序的入口点。    
  2. //    
  3. #include "stdafx.h"    
  4. #include "stdio.h"    
  5. #include "cv.h"    
  6. #include "highgui.h"    
  7. #include "Math.h"    
  8.   
  9.   
  10. int _tmain(int argc, _TCHAR* argv[])    
  11. {    
  12.     IplImage *src = cvLoadImage("c:\\temp.jpg", 0);    
  13.     IplImage *dsw = cvCreateImage(cvGetSize(src), 8, 1);    
  14.     IplImage *dst = cvCreateImage(cvGetSize(src), 8, 3);    
  15.     CvMemStorage *storage = cvCreateMemStorage(0);    
  16.     CvSeq *first_contour = NULL;    
  17.   
  18.     //turn the src image to a binary image    
  19.     //cvThreshold(src, dsw, 125, 255, CV_THRESH_BINARY_INV);    
  20.     cvThreshold(src, dsw, 100, 255, CV_THRESH_BINARY);    
  21.   
  22.     cvFindContours(dsw, storage, &first_contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);    
  23.     cvZero(dst);    
  24.     int cnt = 0;    
  25.     for(; first_contour != 0; first_contour = first_contour->h_next)    
  26.     {    
  27.         cnt++;    
  28.         CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255);    
  29.         cvDrawContours(dst, first_contour, color, color, 0, 2, CV_FILLED, cvPoint(0, 0));    
  30.         CvRect rect = cvBoundingRect(first_contour,0);  
  31.         cvRectangle(dst, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),CV_RGB(255, 0, 0), 1, 8, 0);  
  32.     }    
  33.   
  34.     printf("the num of contours : %d\n", cnt);    
  35.   
  36.     cvNamedWindow( "Source", 1 );    
  37.     cvShowImage( "Source", src );    
  38.   
  39.     cvNamedWindow( "dsw", 1 );    
  40.     cvShowImage( "dsw", dsw );    
  41.   
  42.     cvNamedWindow( "Components", 1 );    
  43.     cvShowImage( "Components", dst );    
  44.   
  45.     cvReleaseMemStorage(&storage);    
  46.     cvWaitKey(-1);    
  47.   
  48.     return 0;    
  49. }    


resul2:



这种情况下 内轮廓也框出来了。。。。。


=======================================

看来阈值的选择与想要的结果有很大关系哦。

如何适应不同的图片呢?????????????????


=======================================

还有,每幅图片里面,最大的轮廓是整幅图像,可以根据其面积最大,去除 哦~~~修改如下:


area = fabs(cvContourArea(first_contour, CV_WHOLE_SEQ)); //cal the hole's area



++++++++++++++++++++++++++++++++

ps:

在写后面那个 内轮廓填充的时候,才发现, dsw 是我二值化之后的图像,很明显不应该是这样子的呀。

我把 关于 Contours 的函数删除之后 又 恢复正常了。不知道为嘛呢。 很显然查出来的轮廓是 正确二值化之后的吧。 不知道为嘛会这样显示呢。


+++++++++++++++++++++++++++++==

ps:

再看另一个图的结果:

总有 9 个轮廓。

另外,计算了下,每个大轮廓内部的 小轮廓的数目 conner ,结果显示都为0.

看看第一个大五角星。 应该是把 边边作为了一个轮廓, 把 内部 黑色区域作为一个 轮廓 了吧????

还有,这幅图片 没有被当做一个大轮廓,上面那个小猫的,整幅图片被框了一下啊。。。。。。。。。。。。





另外i, 把 关于 cvFindContours && cvDrawContours 两个函数部分删除,二值化结果如下:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值