我做的均值滤波,模板可变 Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long Private Type BITMAP bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Public Function max(x As Long, y As Long) As Long If x > y Then max = x Else max = y End If End Function Public Function min(x As Long, y As Long) As Long If x < y Then min = x Else min = y End If End FunctionPrivate Sub Command1_Click() CommonDialog1.Filter = "图片(*.jpg)|*.jpg|bitmap(*.bmp)|*.bmp" CommonDialog1.ShowOpen Picture1.Picture = LoadPicture(CommonDialog1.FileName) End SubPrivate Sub Command2_Click() Dim bmp As BITMAP Dim i As Integer, j As Integer Dim result As Long Dim bytArrImg() As Byte Dim arry() As Byte Dim temp As Integer Dim x, y As Integer Dim m, n As Integer Dim lngsum As Long, lngcount As Long Dim size As Long size = InputBox("请输入模板大小") result = GetObject(Picture1.Picture.Handle, Len(bmp), bmp) ReDim bytArrImg(bmp.bmHeight * bmp.bmWidth - 1) GetBitmapBits Picture1.Picture, bmp.bmWidth * bmp.bmHeight - 1, bytArrImg(0) ReDim arry(0 To bmp.bmHeight - 1, 0 To bmp.bmWidth - 1) For x = 0 To bmp.bmHeight - 1 For y = 0 To bmp.bmWidth - 1 arry(x, y) = bytArrImg(x * bmp.bmWidth + y) Next y Next x For i = 0 To bmp.bmHeight - 1 For j = 0 To bmp.bmWidth - 1 lngsum = 0 lngcount = 0 For m = max(0, i - size \ 2) To min(bmp.bmHeight - 1, i + size \ 2) For n = max(0, j - size \ 2) To min(bmp.bmWidth - 1, j + size \ 2) lngsum = lngsum + arry(m, n) lngcount = lngcount + 1 Next n Next m arry(i, j) = CByte(lngsum / lngcount) Next j Next i For x = 0 To bmp.bmHeight - 1 For y = 0 To bmp.bmWidth - 1 bytArrImg(x * bmp.bmWidth + y) = arry(x, y) Next y Next x SetBitmapBits Picture1.Picture, bmp.bmWidth * bmp.bmHeight - 1, bytArrImg(0) Picture2.Picture = Picture1.Picture 'Refresh End Sub
Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Type BITMAP
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
Public Function max(x As Long, y As Long) As Long
If x > y Then
max = x
Else
max = y
End If
End Function
Public Function min(x As Long, y As Long) As Long
If x < y Then
min = x
Else
min = y
End If
End FunctionPrivate Sub Command1_Click()
CommonDialog1.Filter = "图片(*.jpg)|*.jpg|bitmap(*.bmp)|*.bmp"
CommonDialog1.ShowOpen
Picture1.Picture = LoadPicture(CommonDialog1.FileName)
End SubPrivate Sub Command2_Click()
Dim bmp As BITMAP
Dim i As Integer, j As Integer
Dim result As Long
Dim bytArrImg() As Byte
Dim arry() As Byte
Dim temp As Integer
Dim x, y As Integer
Dim m, n As Integer
Dim lngsum As Long, lngcount As Long
Dim size As Long
size = InputBox("请输入模板大小")
result = GetObject(Picture1.Picture.Handle, Len(bmp), bmp)
ReDim bytArrImg(bmp.bmHeight * bmp.bmWidth - 1)
GetBitmapBits Picture1.Picture, bmp.bmWidth * bmp.bmHeight - 1, bytArrImg(0)
ReDim arry(0 To bmp.bmHeight - 1, 0 To bmp.bmWidth - 1)
For x = 0 To bmp.bmHeight - 1
For y = 0 To bmp.bmWidth - 1
arry(x, y) = bytArrImg(x * bmp.bmWidth + y)
Next y
Next x
For i = 0 To bmp.bmHeight - 1
For j = 0 To bmp.bmWidth - 1
lngsum = 0
lngcount = 0
For m = max(0, i - size \ 2) To min(bmp.bmHeight - 1, i + size \ 2)
For n = max(0, j - size \ 2) To min(bmp.bmWidth - 1, j + size \ 2)
lngsum = lngsum + arry(m, n)
lngcount = lngcount + 1
Next n
Next m
arry(i, j) = CByte(lngsum / lngcount)
Next j
Next i
For x = 0 To bmp.bmHeight - 1
For y = 0 To bmp.bmWidth - 1
bytArrImg(x * bmp.bmWidth + y) = arry(x, y)
Next y
Next x
SetBitmapBits Picture1.Picture, bmp.bmWidth * bmp.bmHeight - 1, bytArrImg(0)
Picture2.Picture = Picture1.Picture
'Refresh
End Sub