昨天被问到关于基于vb.net       emgucv形态学(开、闭、腐蚀、膨胀)操作,网上VB.net资料也太少了,在这里我就把他整理成一个函数,方便使用vb.net的同志直接调用。形态学(开、闭、腐蚀、膨胀)的具体意义、目的我在这里就不啰嗦了,网上一搜一大把。

具体函数如下:

形态学操作函数 imagecloseopen:其中 picturebox1输入图像, picturebox2为输出图像,iselect为选择开、闭、腐蚀、膨胀,可选择“0,1,2,3”,用了try-Catch容错语句,同志们可以去掉,返回值为string,正确返回“1”,错误返回“0”

 

Public Shared Function imagecloseopen(ByVal As PictureBox, ByVal picturebox2 As PictureBox, ByVal iselect As Int16) As String
        Try
            Dim img As Image(Of Bgr, Byte) = New Image(Of Bgr, Byte)(picturebox1.Image)
            Dim img_threshold As Image(Of Bgr, Byte) = New Image(Of Bgr, Byte)(img.Width, img.Height)

            Dim img_element As Mat = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Ellipse, New Size(3, 3), New Point(-1, -1))
            If iselect = 0 Then
                CvInvoke.MorphologyEx(img, img_threshold, MorphOp.Close, img_element, New Point(-1, -1), 5, BorderType.Default, New MCvScalar(0, 0, 0))
            ElseIf iselect = 1 Then
                CvInvoke.MorphologyEx(img, img_threshold, MorphOp.Open, img_element, New Point(-1, -1), 9, BorderType.Default, New MCvScalar(0, 0, 0))
            ElseIf iselect = 3 Then 
                CvInvoke.MorphologyEx(img, img_threshold, MorphOp.Erode, img_element, New Point(-1, -1), 2, BorderType.Default, New MCvScalar(0, 0, 0))
            ElseIf iselect = 2 Then 
                CvInvoke.MorphologyEx(img, img_threshold, MorphOp.Dilate, img_element, New Point(-1, -1), 2, BorderType.Default, New MCvScalar(0, 0, 0))
            End If

            picturebox2.Image = img_threshold.ToBitmap()
            picturebox2.SizeMode = PictureBoxSizeMode.Zoom

            Return 1
        Catch ex As Exception
            Return 0
        End Try

    End Function