||
Visual Basic与图像处理(三)
8、Priwitt微分算子
Prewitt微分算子的思路与Sobel微分算子的思路类似,也是在一个3×3模板下进行图像的锐化处理。Prewitt微分算子的定义如下:
上面的式子同样也可以用模板进行表示:
Priwitt微分算子与Sobel微分算子的模板基本一样,只是系数的微调,因此处理结果也基本一致,从肉眼很难看出明显的差别,本例的运行界面如下:
Priwitt滤波按钮对应的代码如下:
Private Sub cmdPriwittFilter_Click()
Dim i As Integer, j As Integer
Dim PixelValue As Long
Dim r As Integer, g As Integer, b As Integer
Dim GrayValue As Integer
Dim Temp(2, 2) As Integer
Dim x As Integer, y As Integer
Dim Dx As Integer, Dy As Integer
Dim NGrayValue As Integer
'设置目标图像的高度和宽度
DestImHeight = SourceImHeight - 2 '边界像素直接不要了
DestImWidth = SourceImWidth - 2
'设置目标PictureBox的大小
PicDest.Height = DestImHeight
PicDest.Width = DestImWidth
ReDim dbits(DestImHeight - 1, DestImWidth - 1, 2) As Byte '重新定义目标数组的维数
dbits = sbits '数组的直接复制,只有数组维数完全一样才可以
'图像的Priwitt滤波
For i = 1 To SourceImHeight - 2 '边界像素不处理
For j = 1 To SourceImWidth - 2
NGrayValue = 0
For x = 0 To 2
For y = 0 To 2
r = sbits(i + 1 - x, j + 1 - y, 0)
g = sbits(i + 1 - x, j + 1 - y, 1)
b = sbits(i + 1 - x, j + 1 - y, 2)
'插入图像处理的过程,对r, g, b进行处理
Temp(x, y) = 0.3 * r + 0.59 * g + 0.11 * b
Next y
Next x
Dx = -(Temp(0, 0) + Temp(0, 1) + Temp(0, 2)) + (Temp(2, 0) + Temp(2, 1) + Temp(2, 2))
Dy = -(Temp(0, 0) + Temp(1, 0) + Temp(2, 0)) + (Temp(0, 2) + Temp(1, 2) + Temp(2, 2))
NGrayValue = Sqr(Dx ^ 2 + Dy ^ 2)
If NGrayValue > 255 Then NGrayValue = 255
dbits(i - 1, j - 1, 0) = NGrayValue
dbits(i - 1, j - 1, 1) = NGrayValue
dbits(i - 1, j - 1, 2) = NGrayValue
Next j
Next i
Dim tDBmpInfo As BITMAP
Dim hDestBmp As Long
hDestBmp = PicDest.Image.Handle
'获得位图信息
Call GetObject(hDestBmp, Len(tDBmpInfo), tDBmpInfo)
ReDim ImageArray(1 To DestImWidth * 4, 1 To DestImHeight) As Byte
For i = 1 To DestImHeight
For j = 1 To DestImWidth
ImageArray((j - 1) * 4 + 1, i) = dbits(i - 1, j - 1, 2)
ImageArray((j - 1) * 4 + 2, i) = dbits(i - 1, j - 1, 1)
ImageArray((j - 1) * 4 + 3, i) = dbits(i - 1, j - 1, 0)
Next j
Next i
'获得源图与目标图二进制位
Call SetBitmapBits(hDestBmp, tDBmpInfo.bmWidthBytes * tDBmpInfo.bmHeight, ImageArray(1, 1))
End Sub
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-12-17 21:38
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社