版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
本文的C#版本请访问:图像分割-漫水填充法 floodFill (C#)-CSDN博客
FloodFill方法是一种图像处理算法,它的目的是将一个闭合的区域内的像素全部涂上同一个颜色。在实现中,通常会使用递归或队列来处理待处理列表。具体的实现方式会根据具体应用场景的需求而有所不同。
CvInvoke.FloodFill方法用于执行图像的漫水填充操作,它将连通区域中的所有像素点填充为指定颜色的操作。该方法声明如下:
Public Shared Function FloodFill (
???????? src As IInputOutputArray,
???????? mask As IInputOutputArray,
???????? seedPoint As Point,
???????? newVal As MCvScalar,
???????? <OutAttribute> ByRef rect As Rectangle,
???????? loDiff As MCvScalar,
???????? upDiff As MCvScalar,
???????? Optional connectivity As Connectivity = Connectivity.FourConnected,
???????? Optional flags As FloodFillType = FloodFillType.Default
) As Integer
参数说明:
返回值:
返回一个整数值,表示填充的像素个数。
'漫水填充法 floodFill
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim m As New Mat("C:\learnEmgucv\tower.jpg", ImreadModes.Color)
'种子点的坐标
Dim seedPoint As New Point(100, 100)
'填充颜色
Dim newVal As New MCvScalar(0, 255, 255)
'掩码图像
Dim mmask As New Mat
mmask = Mat.Zeros(m.Rows + 2, m.Cols + 2, DepthType.Cv8U, 1)
Dim rect As New Rectangle
' 执行漫水填充
Dim numFilledPixels As Integer = CvInvoke.FloodFill(m, mmask, seedPoint, newVal, rect,
New MCvScalar(10, 10, 10),
New MCvScalar(20, 20, 20))
'输出填充的像素个数
Console.WriteLine("Number of filled pixels: " & numFilledPixels)
'显示填充后的图像
ImageBox1.Image = m
End Sub
输出结果如下图所示:
图8-1漫水填充法实现图像分割
'漫水填充法 floodFill
'选取不同的起始点坐标进行填充
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim msrc As New Mat("c:\learnEmgucv\tower.jpg", ImreadModes.AnyColor)
ImageBox1.Image = msrc
Dim mask As New Mat(msrc.Rows + 2, msrc.Cols + 2, DepthType.Cv8U, 1)
Dim area As Integer
'从坐标(250, 250)进行填充
Dim mdst1 As Mat = msrc.Clone
area = CvInvoke.FloodFill(mdst1, mask,
New Point(250, 250),
New MCvScalar(0, 0, 255),
New Rectangle(),
New MCvScalar(40, 40, 40),
New MCvScalar(40, 40, 40),
Connectivity.FourConnected,
FloodFillType.FixedRange)
CvInvoke.Imshow("mdst1", mdst1)
'从坐标(400, 400)进行填充
Dim mdst2 As Mat = msrc.Clone
area = CvInvoke.FloodFill(mdst2, mask,
New Point(400, 400),
New MCvScalar(0, 255, 0),
New Rectangle(),
New MCvScalar(60, 60, 60),
New MCvScalar(80, 80, 80),
Connectivity.FourConnected,
FloodFillType.FixedRange)
CvInvoke.Imshow("mdst2", mdst2)
End Sub
输出结果如下图所示:
图8-2 不同坐标点填充效果
'FloodFill随机坐标点使用随机颜色填充
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim msrc As New Mat("c:\learnEmgucv\tower.jpg", ImreadModes.AnyColor)
ImageBox1.Image = msrc
Dim mask As New Mat(msrc.Rows + 2, msrc.Cols + 2, DepthType.Cv8U, 1)
Dim lodiff As New MCvScalar(20, 20, 20)
Dim hidiff As New MCvScalar(20, 20, 20)
Dim r As New Random
For i As Integer = 0 To 99
Dim x As Integer = r.Next(msrc.Cols)
Dim y As Integer = r.Next(msrc.Rows)
Dim p As New Point(x, y)
Dim newsc As New MCvScalar(r.Next(256), r.Next(256), r.Next(256))
Dim area As Integer
area = CvInvoke.FloodFill(msrc, mask,
p,
newsc,
New Rectangle(),
lodiff,
hidiff,
Connectivity.FourConnected,
FloodFillType.FixedRange)
Next
CvInvoke.Imshow("mout", msrc)
End Sub
输出结果如下图所示:
图8-3 随机坐标点使用随机颜色填充
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录