二是效率高,以Laplacian(拉普拉斯)算子对图片进行锐化处理为例,3M图片,下面的VB.Net代码锐化结束需要10秒以上,AForge.Net只需要0.6秒。
在Foxtable中新建一个窗口,添加一个按钮、一个图片框
按钮写入 VB.Net Laplacian(拉普拉斯)算子图片锐化代码如下:
Dim ImagePath As String
Dim dlg As OpenFileDialog = Me.OpenFileDialog1
If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
ImagePath = dlg.FileName
Else
Return
End If
Dim tempImage As Image = Image.FromFile(ImagePath)
Dim Height As Integer
Height = tempImage.Height
Dim width As Integer
width = tempImage.Width
NewBitmap = New Bitmap(width, Height)
Dim OldBitmap As Bitmap = New Bitmap(ImagePath)
Dim t As Date = Date.Now
Try
'在使用本函数对彩色图像进行锐化处理时,应该选择{0,-1,0,-1,5,-1,0,-1,0}作为模板,所用模板能在最大限度保证彩色图像不失真的前提下,使图像轮廓线更清晰。
Dim pixel As Color
'Dim Laplacian() As Integer = {0, -1, 0, -1, 5, -1, 0, -1, 0}
Dim Laplacian() As Integer = {-1, -1, -1, -1, 9, -1, -1, -1, -1}
For x As Integer = 1 To width - 2
For y As Integer = 1 To Height - 2
Me.ProgressBar1.Value = x + y
Dim r As Integer = 0
Dim g As Integer = 0
Dim b As Integer = 0
Dim index As Integer = 0
For col As Integer = -1 To 1 '用Laplacian(拉普拉斯)算子计算当前坐标处像素的上、下、左、右及四角共8处(英文:8 neighbor)的像素值
For row As Integer = -1 To 1
pixel = OldBitmap.GetPixel(x + row, y + col)
r = r + pixel.R * Laplacian(index)
g = g + pixel.G * Laplacian(index)
b = b + pixel.B * Laplacian(index)
index = index + 1
Next
Next
'防止像素值溢出
If r > 255 Then r = 255
If r < 0 Then r = 0
If g > 255 Then g = 255
If g < 0 Then g = 0
If b > 255 Then b = 255
If b < 0 Then b = 0
'将指定坐标处的像素设为指定的颜色
NewBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b))
Next
Next
Dim pic As WinForm.PictureBox = e.Form.Controls("PictureBox1")
pic.SizeMode = ImageSizeMode.Zoom
pic.Image = NewBitmap
Catch ex As Exception
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
MsgBox("耗时: " & (Date.Now - t).TotalSeconds & " 秒")
End Try
一楼:去灰底效果。附件中更新了图像的3种锐化效果、图像对比度、亮度、饱和度调整代码。
效果如下:
原图片:
此主题相关图片如下:img_20170527_163415.jpg
去灰底后的图片:
此主题相关图片如下:clearbord.jpg
去灰底代码:
以下内容只有回复后才可以浏览
图片锐化代码:1、AForge默认锐化(实际就是拉普拉斯算子锐化)。2、高斯锐化。3、自定义卷积滤镜锐化
并附上:对图片像素的亮度、对比度、饱和度调整代码。
以下内容只有回复后才可以浏览