应用 AForge.Net 解决,代码如下:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ImagePath As String
Dim dlg As New OpenFileDialog
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
ImagePath = dlg.FileName
Else
Return
End If
Dim img As Bitmap = System.Drawing.Bitmap.FromFile(ImagePath)
Dim NewBitmap As Bitmap
Dim Oldimage As System.Drawing.Bitmap
Dim t As Date = Date.Now()
Try
Dim CloneRect As New Rectangle(0, 0, img.Width, img.Height)
Oldimage = img.Clone(CloneRect, PixelFormat.Format8bppIndexed) '从这个API文档页面可以看出,SobolEdgeDetector过滤器只支持8bpp灰度图像
Dim filter As AForge.Imaging.Filters.BradleyLocalThresholding = New AForge.Imaging.Filters.BradleyLocalThresholding()
NewBitmap = filter.Apply(Oldimage)
Application.DoEvents()
Me.PictureBox2.SizeMode = PictureBoxSizeMode.Zoom
Me.PictureBox2.Image = NewBitmap
Catch ex As Exception
MsgBox(ex.Message)
End Try
MsgBox("耗时: " & (Date.Now - t).TotalSeconds & " 秒")
End Sub