cvFindContours && cvDrawContours 的应用2-----图像内轮廓填充

来源:互联网 发布:交响乐 知乎 编辑:程序博客网 时间:2024/05/22 00:51


转自: timidsmile http://blog.csdn.net/timidsmile/article/details/8520803

本文根据 http://geniusshare.i.sohu.com/blog/view/149925914.htm  整理而来 若查看原文,请点击这里 


 图像内轮廓填充通常称为孔洞填充,主要用于目标提取。

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


原图:



二值化之后的图 && 填充之后的图:



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

PS:

特意 在大月亮里面  涂了 两个大点点 !哈哈哈

是为了要看看 cvFindContours 函数中返回的  pContour  中的元素是如何存储的。

嗯, 看见了么? O(∩_∩)O哈哈~

pContour ->h_next  连接的是图中的四个大轮廓: 一个大月亮, 两颗小星星 和一个小白点。

pContour->v_next 连接的是每个大轮廓下面的 孩子们 。 pContour->v_next 指向第一个孩子,第二个孩子 在 第一个孩子 的 水平 方向 而不是 垂直方向, 所以 第二个孩子是

pContour->v_next->h_next   哦~~~

所以 大月亮轮廓里面有 2个 小轮廓, 就是我们要找的内轮廓。 找到 之后, 当其 面积 < 200个像素  的时候,才对其进行填充 。。。。 这样做是为了考虑到, 当 内部轮廓不是 目标空洞 而是 目标背景时, 防止填错 。。。。

0 0
原创粉丝点击