公司的网站建设费做什么费用,蓝色系网站设计,阳江网约车,营销型网站模板下载版权声明#xff1a;本文为博主原创文章#xff0c;转载请在显著位置标明本文出处以及作者网名#xff0c;未经作者允许不得用于商业目的。
本文的C#版本请访问#xff1a;图像分割 分水岭法 watershed#xff08;C##xff09;-CSDN博客
Watershed算法是一种图像处理算…版权声明本文为博主原创文章转载请在显著位置标明本文出处以及作者网名未经作者允许不得用于商业目的。
本文的C#版本请访问图像分割 分水岭法 watershedC#-CSDN博客
Watershed算法是一种图像处理算法它是基于形态学的图像分割方法。在实现中Watershed算法通常需要先对图像进行预处理例如去除噪声、边缘检测等操作以便更好的构建隔板。在填充山谷时可以使用队列或其他数据结构来实现以便管理待处理的像素。对于连通的区域可以使用标记或者颜色来区分。最终得到的分割结果可以用于物体识别、形状分析等应用场景。
CvInvoke.Watershed方法用于执行图像的分水岭分割操作。该方法声明如下
Public Shared Sub Watershed ( image As IInputArray, markers As IInputOutputArray
)
参数说明
image输入图像必须是CV8U三通道彩色图。markers指定的标记图像这是一个CV32S的单通道图像并且与输入图像具有相同的尺寸。可以通过FindContours和DrawContours来获得这个图像使用像数值1、2、3……作为连通分量来粗略勾勒出图像期望分割的区域而未确定的区域未标记为0。
该方法没有返回值而是直接在markers图像上进行分割操作。最终输出的图像两个区域间使用-1作为分割线。 分水岭法 watershedPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickDim m As New Mat(c:\learnEmgucv\tower.jpg, ImreadModes.Color)灰度Dim mgray As New MatCvInvoke.CvtColor(m, mgray, ColorConversion.Bgr2Gray)滤波Dim mgaussian As New MatCvInvoke.GaussianBlur(mgray, mgaussian, New Drawing.Size(5, 5), 2)ImageBox1.Image mgaussian边缘检测Dim mcanny As New MatCvInvoke.Canny(mgaussian, mcanny, 60, 120)ImageBox2.Image mcanny获取轮廓Dim contours As New VectorOfVectorOfPointDim hierarchy As New VectorOfRectCvInvoke.FindContours(mcanny, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxNone)Dim mmark As New Matmmark 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 Matmc mmark.Clone转为可以显示的CV8Umc.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,bgrDim count As Integer 0对每个像素点操作For i As Integer 0 To matrwater.Rows - 1For j As Integer 0 To matrwater.Cols - 1获得连通分量像素点值Dim index As Int32 matrwater(i, j)如果是区域的分割线If index -1 Thenimgwater.Data(i, j, 0) 255imgwater.Data(i, j, 1) 255imgwater.Data(i, j, 2) 255ElseDim pointcolor As Bgr检查是否已经存在索引对应的颜色If HSpointcolor.ContainsKey(index) Thenpointcolor HSpointcolor(index)Else获得随机颜色pointcolor getRadomBgr()将已经使用的随机颜色加入ListHSpointcolor.Add(index, pointcolor)count 1End If像素点填充颜色imgwater.Data(i, j, 0) pointcolor.Blueimgwater.Data(i, j, 1) pointcolor.Greenimgwater.Data(i, j, 2) pointcolor.RedEnd IfNextNext输出使用分水岭法后的结果CvInvoke.Imshow(imgwater, imgwater)End Sub获得随机颜色Private Function getRadomBgr() As BgrDim 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 教程 目录