2014-07-23 81 views
0

我只有下列位图: original contours image 什么我要做的是自动填写轮廓像下面这样: filled 这有点像MS画家填充功能。初始轮廓不会穿过图像的边界。如何使用opencv自动检测并填充封闭区域?

我还没有一个好主意呢。 OpenCV中有没有办法可以做到这一点?或者有什么建议?

在此先感谢!

+0

输入是否保证关闭?或者,例如,输入是否有简单的行?后者会将一个简单的问题转化为更难的方法 – Bull

+0

opencv中有一个[floodfill方法](http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#floodfill),其工作原理与一个在油漆。 – HugoRune

回答

0

如果您知道的区域必须被关闭,你可以横向扫描,并保持边缘计数:

// Assume image is an CV_8UC1 with only black and white pixels. 
uchar white(255); 
uchar black(0); 
cv::Mat output = image.clone(); 

for(int y = 0; y < image.rows; ++y) 
{ 
    uchar* irow = image.ptr<uchar>(y) 
    uchar* orow = output.ptr<uchar>(y) 
    uchar previous = black; 
    int filling = 0; 

    for(int x = 0; x < image.cols; ++x) 
    { 
     // if we are not filling, turn it on at a black to white transition 
     if((filling == 0) && previous == black && irow[x] == white) 
      ++filling ; 

     // if we are filling, turn it off at a white to black transition 
     if((filling != 0) && previous == white && irow[x] == black) 
      --filling ; 

     // write output image 
     orow[x] = filling != 0 ? white : black; 

     // update previous pixel 
     previous = irow[x]; 
    } 
} 
1

大概Contours Hierarchy可以帮助你实现这一目标,

你需要做的,

  • 找到每个轮廓。
  • 检查每个轮廓的层次结构。
  • 根据层次结构将每个轮廓绘制到厚度为filled1的新垫子上。