如何使用opencv自动检测和填充闭合区域?
我只有以下位图: 我要做的是自动填充轮廓,如下所示: 这有点像MS Painter中的填充函数。初始轮廓不会穿过图像的边界 我还没有一个好主意。OpenCV中有什么方法可以做到这一点吗?或者有什么建议如何使用opencv自动检测和填充闭合区域?,opencv,image-processing,Opencv,Image Processing,我只有以下位图: 我要做的是自动填充轮廓,如下所示: 这有点像MS Painter中的填充函数。初始轮廓不会穿过图像的边界 我还没有一个好主意。OpenCV中有什么方法可以做到这一点吗?或者有什么建议 提前谢谢 如果您知道必须关闭区域,您可以水平扫描并保持边缘计数: // Assume image is an CV_8UC1 with only black and white pixels. uchar white(255); uchar black(0); cv::Mat output = i
提前谢谢 如果您知道必须关闭区域,您可以水平扫描并保持边缘计数:
// 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];
}
}
//假设图像是只有黑白像素的CV_8UC1。
乌查尔·怀特(255);
乌查尔黑(0);
cv::Mat output=image.clone();
对于(int y=0;y
可能会帮助您实现这一目标
你需要这样做
- 找到每一个轮廓
- 检查每个轮廓的层次结构
- 根据层次结构,将每个轮廓绘制到新垫上,填充厚度
或
1