关于Opencv 分水岭算法种子点(注水口)的选取问题

Opencv 分水岭算法

void watershed( InputArray image, InputOutputArray markers );

官方文档中这样表述分水岭算法种子的选取

/*
Before passing the image to the function, you have to roughly outline the desired regions in the
image markers with positive (\>0) indices. So, every region is represented as one or more connected
components with the pixel values 1, 2, 3, and so on. Such markers can be retrieved from a binary
mask using #findContours and #drawContours (see the watershed.cpp demo). The markers are "seeds" of
the future image regions. All the other pixels in markers , whose relation to the outlined regions
is not known and should be defined by the algorithm, should be set to 0's. In the function output,
each pixel in markers is set to a value of the "seed" components or to -1 at boundaries between the
regions.
*/
//寻找种子点
vector<vector<Point>>contours;
findContours(binaryImg, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
Mat markers = Mat::zeros(srcImg.size(), CV_32S);
for (int i = 0; i < contours.size(); i++) 
{
	drawContours(markers, contours, i, Scalar(i + 1), -1);     // i 标记markers
}

简单说就是在在进行要先用findcontours和drawcontours将markers(种子点,注水口)找出来。

这里就要提一下为什么在进行findcontours之前都要先进行距离变换或者腐蚀呢?

因为原始图像经过二值化后,如果直接用该二值图像调用findcontours,那么最后用drawcontours画出来的markers填充的大小就是我们需要寻找的原图轮廓大小,意思就是注水口markers太大了,水刚注进去就漫了,所以最后分水岭算法得出的图像为四不像,如下图所示:有种水漫金山的感觉。

但是当我们在进行findcontours之前都要先进行距离变换或者腐蚀后会发现,二值图像中前景的轮廓变小了 ,此时再用这个图像寻找注水口,注水口的大小(这个轮廓大小)比我们需要寻找的原图轮廓小那么一些些,进行分水岭算法,这下灌水便会得到较好的结果了,如下图所示:(腐蚀核越大,越不容易出现水漫出去,效果越好)

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV分水岭算法是一种常用的图像分割算法,可以有效地将图像中的目标从背景中分离出来。该算法基于分水岭的概念,通过将图像视为地形图,将图像中的边缘和局部极值作为水域,然后根据水流的规则,将图像分割为不同的区域。 在OpenCV中,使用分水岭算法进行图像分割需要经过以下几个步骤: 1. 预处理:首先,对图像进行预处理,包括灰度化、平滑处理和二值化等操作,以便更好地分离目标和背景。 2. 标记:通过手动或自动方式,标记图像中的前景和背景区域。通常情况下,前景区域是我们感兴趣的目标,而背景区域是我们想要分离的部分。 3. 距离变换:基于标记的结果,计算图像中每个像素到最近标记的距离,这将被用于下一步计算分水岭线。 4. 分水岭转换:根据距离变换结果,计算分水岭线,将图像划分为不同的区域。这些区域将成为我们的分割结果。 5. 后处理:根据实际需求,对分割结果进行后处理,如去除小区域、连接相邻区域等。 通过这些步骤,OpenCV分水岭算法能够对图像进行准确的分割,将目标从背景中分离出来,为后续的图像处理和分析提供基础。 引用提供了关于OpenCV分水岭算法的详细介绍和应用案例,可以参考该文献获取更多相关信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [OpenCV——分水岭算法](https://blog.csdn.net/qq_36686437/article/details/131357062)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [分水岭算法(Watershed algorithm)与OpenCV实现](https://blog.csdn.net/qingyafan/article/details/44260817)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值