图像分割 分水岭法 watershed

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

本文的C#版本请访问:图像分割 分水岭法 watershed(C#)-CSDN博客

Watershed算法是一种图像处理算法,它是基于形态学的图像分割方法。在实现中,Watershed算法通常需要先对图像进行预处理,例如去除噪声、边缘检测等操作,以便更好的构建隔板。在填充山谷时,可以使用队列或其他数据结构来实现,以便管理待处理的像素。对于连通的区域,可以使用标记或者颜色来区分。最终得到的分割结果可以用于物体识别、形状分析等应用场景。

CvInvoke.Watershed方法用于执行图像的分水岭分割操作。该方法声明如下:

Public Shared Sub Watershed (

image As IInputArray,

markers As IInputOutputArray

)

参数说明:

  1. image:输入图像,必须是CV8U三通道彩色图。
  2. markers:指定的标记图像,这是一个CV32S的单通道图像,并且与输入图像具有相同的尺寸。可以通过FindContours和DrawContours来获得这个图像,使用像数值1、2、3......作为连通分量,来粗略勾勒出图像期望分割的区域,而未确定的区域未标记为0。

该方法没有返回值,而是直接在markers图像上进行分割操作。最终输出的图像,两个区域间使用-1作为分割线。

vbnet 复制代码
   '分水岭法 watershed
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim m As New Mat("c:\learnEmgucv\tower.jpg", ImreadModes.Color)
        '灰度
        Dim mgray As New Mat
        CvInvoke.CvtColor(m, mgray, ColorConversion.Bgr2Gray)

        '滤波
        Dim mgaussian As New Mat
        CvInvoke.GaussianBlur(mgray, mgaussian, New Drawing.Size(5, 5), 2)
        ImageBox1.Image = mgaussian

        '边缘检测
        Dim mcanny As New Mat
        CvInvoke.Canny(mgaussian, mcanny, 60, 120)
        'ImageBox2.Image = mcanny

        '获取轮廓
        Dim contours As New VectorOfVectorOfPoint
        Dim hierarchy As New VectorOfRect
        CvInvoke.FindContours(mcanny, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone)

        Dim mmark As New Mat
        mmark = Mat.Zeros(mcanny.Rows, mcanny.Cols, DepthType.Cv32S, 1)

        '仅为查看轮廓使用
        Dim mcontours As New Mat(mcanny.Rows, mcanny.Cols, DepthType.Cv8U, 1)
        For i As Integer = 0 To contours.Size - 1
            '标记连通分量索引
            CvInvoke.DrawContours(mmark, contours, i, New MCvScalar(i), 1, LineType.EightConnected, hierarchy)
            '仅为查看轮廓使用
            CvInvoke.DrawContours(mcontours, contours, i, New MCvScalar(255), 1, LineType.EightConnected, hierarchy)
        Next
        '仅为查看轮廓使用
        CvInvoke.Imshow("mcontours", mcontours)

        'mmark是32S,显示不了的
        'CvInvoke.Imshow("mmark", mmark)

        '分水岭法
        CvInvoke.Watershed(m, mmark)

        '仅演示,watershed后的数据
        Dim mc As New Mat
        mc = mmark.Clone
        '转为可以显示的CV8U
        mc.ConvertTo(mc, DepthType.Cv8U)
        CvInvoke.Imshow("mc", mc)

        '使用Matrix和Image便于生成图像(Mat操作像素点比较麻烦)
        Dim matrwater As New Matrix(Of Int32)(mmark.Rows, mmark.Cols)
        mmark.CopyTo(matrwater)

        Dim imgwater As New Image(Of Bgr, Byte)(mmark.Cols, mmark.Rows)

        '为每个区域填充不同的颜色
        Dim HSpointcolor As New Hashtable()       'index,bgr

        Dim count As Integer = 0
        '对每个像素点操作
        For i As Integer = 0 To matrwater.Rows - 1
            For j As Integer = 0 To matrwater.Cols - 1
                '获得连通分量(像素点值)
                Dim index As Int32 = matrwater(i, j)
                '如果是区域的分割线
                If index = -1 Then
                    imgwater.Data(i, j, 0) = 255
                    imgwater.Data(i, j, 1) = 255
                    imgwater.Data(i, j, 2) = 255
                Else
                    Dim pointcolor As Bgr
                    '检查是否已经存在索引对应的颜色
                    If HSpointcolor.ContainsKey(index) Then
                        pointcolor = HSpointcolor(index)
                    Else
                        '获得随机颜色
                        pointcolor = getRadomBgr()
                        '将已经使用的随机颜色加入List
                        HSpointcolor.Add(index, pointcolor)
                        count += 1
                    End If
                    '像素点填充颜色
                    imgwater.Data(i, j, 0) = pointcolor.Blue
                    imgwater.Data(i, j, 1) = pointcolor.Green
                    imgwater.Data(i, j, 2) = pointcolor.Red
                End If
            Next
        Next
        '输出使用分水岭法后的结果
        CvInvoke.Imshow("imgwater", imgwater)
    End Sub

    '获得随机颜色
    Private Function getRadomBgr() As Bgr
        Dim rand As New Random(Now.Millisecond)
        Dim b As Byte = rand.Next(0, 256)
        Dim g As Byte = rand.Next(0, 256)
        Dim r As Byte = rand.Next(0, 256)
        Return New Bgr(b, r, g)
End Function

输出结果如下图所示:

图8-4 分水岭法使用随机颜色填充

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

相关推荐
飞翔的佩奇5 小时前
【完整源码+数据集+部署教程】【天线&水】舰船战舰检测与分类图像分割系统源码&数据集全套:改进yolo11-repvit
前端·python·yolo·计算机视觉·数据集·yolo11·舰船战舰检测与分类图像分割系统
山烛5 小时前
一文读懂YOLOv4:目标检测领域的技术融合与性能突破
人工智能·yolo·目标检测·计算机视觉·yolov4
2401_836900337 小时前
YOLOv5:目标检测的实用派王者
人工智能·计算机视觉·目标跟踪·yolov5
sali-tec8 小时前
C# 基于halcon的视觉工作流-章49-网面破损
开发语言·图像处理·算法·计算机视觉·c#
TTGGGFF10 小时前
机器视觉:智能车大赛视觉组技术文档——用 YOLO3 Nano 实现目标检测并部署到 OpenART
人工智能·目标检测·计算机视觉
AI模块工坊10 小时前
AAAI 2025 | 即插即用,川大Mesorch刷新SOTA,用「介观」Transformer架构终结图像造假
人工智能·深度学习·计算机视觉·架构·transformer
亦陈不染12 小时前
c#入门详解(刘铁锰)06 - 数据持久化:TXT文本保存、序列化与反序列化(附详细源码)
开发语言·计算机视觉·c#·wpf
WWZZ202512 小时前
快速上手大模型:机器学习6(过拟合、正则化)
人工智能·算法·机器学习·计算机视觉·机器人·slam·具身感知
WWZZ202512 小时前
快速上手大模型:机器学习5(逻辑回归及其代价函数)
人工智能·算法·机器学习·计算机视觉·机器人·slam·具身感知
姓刘的哦13 小时前
基于线程池的配电房图像检测
人工智能·计算机视觉·目标跟踪