OpenCV图象孔洞内轮廓填充

图像内轮廓填充通常称为孔洞填充,主要用于目标提取。不带任何条件的内轮廓填充,在目标密度很大时,可能导致错误填充。一种典型情况,当多个目标粘连,并 且形成环状时,简单的内轮廓填充会将环状内部背景部分误认为目标空洞进行错误填充。这种错误对于目标分割和提取是非常致命的。


  如果将内轮廓面积作为限制条件进行填充,就可以很好解决上述问题。通常内轮廓面积应该不大于目标的最大面积。


[cpp]  view plain copy
  1. #include <cv.h>   
  2. #include <cxcore.h>   
  3. #include <highgui.h>  
  4.   
  5. #pragma comment(lib, "cv.lib")  
  6. #pragma comment(lib, "cxcore.lib")   
  7. #pragma comment(lib, "highgui.lib")  
  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.         for (; pContour != NULL; pContour = pContour->h_next)   
  29.         {   
  30.             // 内轮廓循环   
  31.             for (pConInner = pContour->v_next; pConInner != NULL; pConInner = pConInner->h_next)   
  32.             {   
  33.                 // 内轮廓面积   
  34.                 dConArea = fabs(cvContourArea(pConInner, CV_WHOLE_SEQ));   
  35.                 if (dConArea <= dAreaThre)   
  36.                 {   
  37.                     cvDrawContours(pBinary, pConInner, CV_RGB(255, 255, 255), CV_RGB(255, 255, 255), 0, CV_FILLED, 8, cvPoint(0, 0));  
  38.                 }   
  39.             }   
  40.         }   
  41.         cvReleaseMemStorage(&pStorage);   
  42.         pStorage = NULL;   
  43.     }   
  44. }   
  45. int main()    
  46. {    
  47.     IplImage *img = cvLoadImage(".//test.png", 0);    
  48.     IplImage *bin = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);  
  49.     cvCopy(img, bin);  
  50.   
  51.     FillInternalContours(bin, 200);    
  52.   
  53.     cvNamedWindow("img");    
  54.     cvShowImage("img", img);    
  55.   
  56.     cvNamedWindow("result");    
  57.     cvShowImage("result", bin);    
  58.   
  59.     cvWaitKey(-1);    
  60.   
  61.     cvReleaseImage(&img);    
  62.     cvReleaseImage(&bin);    
  63.   
  64.     return 0;    
  65. }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值