版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
本文的C#版本请访问:图像分割-Grabcut法(C#)-CSDN博客
GrabCut是一种基于图像分割的技术,它可以用于将图像中的前景和背景分离。在实现中,GrabCut算法通常需要使用高斯混合模型(GMM)来建立前景和背景的概率分布,以便更好的估计像素的标签。同时,还需要考虑如何处理边界处的像素,以避免边界处的像素被错误地分类。GrabCut算法在图像分割中有着广泛的应用,例如人像分割、物体抠图等。
EmguCV使用CvInvoke.GrabCut方法来执行GrabCut算法,该方法声明如下:
Public Shared Sub GrabCut (
???????? img As IInputArray,
???????? mask As IInputOutputArray,
???????? rect As Rectangle,
???????? bgdModel As IInputOutputArray,
???????? fgdModel As IInputOutputArray,
???????? iterCount As Integer,
???????? type As GrabcutInitType
)
参数说明:
该方法没有返回值,而是直接在mask图像上进行前景分割操作,最终获得的mask包含0-3的值,含义如参数中说明。
'Grabcut法
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim m As New Mat("C:\learnEmgucv\tower.jpg", ImreadModes.AnyColor)
Dim result As New Mat
Dim bg As New Mat
Dim fg As New Mat
Dim rect As New Rectangle(80, 30, 680, 450)
CvInvoke.GrabCut(m, result, rect, bg, fg, 1, GrabcutInitType.InitWithRect)
'输出的result只有4个值:
'0:确定背景
'1:确定前景
'2:可能背景
'3:可能前景
'演示框选范围
CvInvoke.Rectangle(m, rect, New MCvScalar(255, 255, 255), 1)
ImageBox1.Image = m
'标记区域
Dim matr As New Matrix(Of Byte)(result.Rows, result.Cols)
result.CopyTo(matr)
For i As Integer = 0 To matr.Cols - 1
For j As Integer = 0 To matr.Rows - 1
'将确定背景和可能背景标记为0,否则为255
If matr(j, i) = 0 Or matr(j, i) = 2 Then
matr(j, i) = 0
Else
matr(j, i) = 255
End If
Next
Next
Dim midm As New Mat
midm = matr.Mat
'显示标记的图像
CvInvoke.Imshow("midm", midm)
'灰度转为彩色
Dim midm1 As New Mat
CvInvoke.CvtColor(midm, midm1, ColorConversion.Gray2Bgr)
Dim mout As New Mat
'And运算
CvInvoke.BitwiseAnd(m, midm1, mout)
CvInvoke.Imshow("mout", mout)
End Sub
输出结果如下图所示:
图8-5 Grabcut法分离前景
'Grabcut法
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim m As Mat = CvInvoke.Imread("C:\learnEmgucv\tower.jpg", CvEnum.ImreadModes.Color)
Dim result As New Mat
Dim bg As New Mat
Dim fg As New Mat
Dim rect As New Rectangle(80, 30, 680, 450)
CvInvoke.GrabCut(m, result, rect, bg, fg, 5, CvEnum.GrabcutInitType.InitWithRect)
Dim src As Image(Of Bgr, Byte) = m.ToImage(Of Bgr, Byte)
Dim dst As New Image(Of Bgr, Byte)(New Size(src.Width, src.Height))
Dim mask As Image(Of Gray, Byte) = result.ToImage(Of Gray, Byte)
'直接操作Image像素点
For i As Integer = 0 To src.Rows - 1
For j As Integer = 0 To src.Cols - 1
'如果是确定前景和可能前景,直接保留原像素点颜色,否则为黑色
If mask.Data(i, j, 0) = 1 Or mask.Data(i, j, 0) = 3 Then
dst.Data(i, j, 0) = src.Data(i, j, 0)
dst.Data(i, j, 1) = src.Data(i, j, 1)
dst.Data(i, j, 2) = src.Data(i, j, 2)
Else
dst.Data(i, j, 0) = 0
dst.Data(i, j, 1) = 0
dst.Data(i, j, 2) = 0
End If
Next
Next
ImageBox1.Image = dst
End Sub
输出结果如下图所示:
图8-6 Grabcut法分离前景
'标记为确定前景,这里使用InitWithMask 参数
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim m As New Mat("c:\learnEmgucv\lena.jpg", ImreadModes.AnyColor)
Dim mask As New Mat
Dim bg As New Mat
Dim fg As New Mat
Dim rect As New Rectangle(80, 30, 340, 480)
'使用前景为全白色
Dim m1 As New Mat("c:\learnEmgucv\lena_fillwhite.jpg", ImreadModes.Grayscale)
Dim mask1 As New Mat
'二值化
CvInvoke.Threshold(m1, mask1, 250, 1, ThresholdType.Binary)
CvInvoke.Rectangle(m, rect, New MCvScalar(255, 255, 255), 1)
'标记之后再调用GrabCut,使用InitWithMask参数
CvInvoke.GrabCut(m, mask1, rect, bg, fg, 2, GrabcutInitType.InitWithMask)
Dim matrx As New Matrix(Of Byte)(mask1.Rows, mask1.Cols)
mask1.CopyTo(matrx)
For i As Integer = 0 To matrx.Cols - 1
For j As Integer = 0 To matrx.Rows - 1
If matrx(i, j) = 0 Or matrx(i, j) = 2 Then
matrx(i, j) = 0
Else
matrx(i, j) = 255
End If
Next
Next
Dim midm2 As New Mat
midm2 = matrx.Mat
Dim midm1 As New Mat
CvInvoke.CvtColor(midm2, midm1, ColorConversion.Gray2Bgr)
Dim mout As New Mat
CvInvoke.BitwiseAnd(m, midm1, mout)
CvInvoke.Imshow("mout", mout)
End Sub
输出结果如下图所示:
图8-7 Grabcut法分离前景
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录