4. Laplace_拉普拉斯算子
Code: Select all
;***********************************
;迷路仟整理 2021.05.08
;LOG_高斯拉普拉斯算子
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0
UseJPEGImageDecoder()
Procedure Kernel_Gauss5x5(Array DimKernel.f(2), Weight.f)
Total.f = 0
For Y = -2 To 2
For X = -2 To 2
Kernel.f =(1/(2*#PI*Weight*Weight)) * Exp(-(X*X+Y*Y)/(2*Weight*Weight))
Total + Kernel
DimKernel(X+2,Y+2) = Kernel
Next
Next
For Y = 0 To 5-1
For X = 0 To 5-1
DimKernel(X,Y) = DimKernel(X,Y)/Total
Next
Next
EndProcedure
Procedure Drawing_cvsScreen()
ImageW = ImageWidth(#picScreen)
ImageH = ImageHeight(#picScreen)
Dim DimPixel.f(ImageW+5-1, ImageH+5-1) ;像素
Dim DimGauss.f(ImageW+5-1, ImageH+5-1) ;像素
Dim DimKernelG.f(5-1, 5-1) ;卷积核
Dim DimKernelL.f(5-1, 5-1) ;卷积核
Kernel_Gauss5x5(DimKernelG(), 1.5)
CopyMemory(?_Bin_Laplace, DimKernelL(), 5*5*4)
If StartDrawing(CanvasOutput(GagetID))
DrawImage(ImageID(ImageID), 0, 0)
;图像灰度化
For Y = 0 To ImageH-1
For X = 0 To ImageW-1
Color = Point(X, Y)
R = Red (Color)
G = Green(Color)
B = Blue (Color)
DimPixel(X+2, Y+2) = (R * 299 + G * 587 + B * 114)/1000
Next
Next
;卷积并输出
For Y = 0 To ImageH-1
For X = 0 To ImageW-1
Gauss.f = 0
For j = 0 To 5-1
For i = 0 To 5-1
Gauss + DimPixel(X+i, Y+j) * DimKernelG(i, j)
Next
Next
DimGauss(X+2, Y+2) = Gauss
Next
Next
;卷积并输出
For Y = 0 To ImageH-1
For X = 0 To ImageW-1
Convol.f = 0
For j = 0 To 5-1
For i = 0 To 5-1
Convol + DimGauss(X+i, Y+j) * DimKernelL(i, j)
Next
Next
If Convol < 000 : Convol = 000 : EndIf
If Convol > 255 : Convol = 255 : EndIf
Plot(X, Y, RGB(Convol, Convol, Convol))
Next
Next
DrawImage(ImageID(ImageID), 010, 010, ImageW/4, ImageH/4)
StopDrawing()
EndIf
EndProcedure
Procedure Drawing_cvsScreen2()
ImageW = ImageWidth(#picScreen)
ImageH = ImageHeight(#picScreen)
Dim DimPixel.f(ImageW+5-1, ImageH+5-1) ;像素
Dim DimGauss.f(ImageW+5-1, ImageH+5-1) ;像素
Dim DimKernel.f(5-1, 5-1) ;卷积核
Kernel_Gauss5x5(DimKernel(), 1.5)
If StartDrawing(CanvasOutput(GagetID))
DrawImage(ImageID(ImageID), 0, 0)
;图像灰度化
For Y = 0 To ImageH-1
For X = 0 To ImageW-1
Color = Point(X, Y)
R = Red (Color)
G = Green(Color)
B = Blue (Color)
DimPixel(X+2, Y+2) = (R * 299 + G * 587 + B * 114)/1000
Next
Next
;卷积并输出
For Y = 0 To ImageH-1
For X = 0 To ImageW-1
Gauss.f = 0
For j = 0 To 5-1
For i = 0 To 5-1
Gauss + DimPixel(X+i, Y+j) * DimKernel(i, j)
Next
Next
DimGauss(X+2, Y+2) = Gauss
Next
Next
;卷积并输出
Convol.f = 0
For Y = 0 To ImageH-1
For X = 0 To ImageW-1
Convol = DimGauss(X+2, Y+0) + DimGauss(X+2, Y+4) + DimGauss(X+2, Y+1) * 2
Convol + DimGauss(X+1, Y+1) + DimGauss(X+3, Y+1) + DimGauss(X+1, Y+2) * 2
Convol + DimGauss(X+0, Y+2) + DimGauss(X+4, Y+2) + DimGauss(X+3, Y+2) * 2
Convol + DimGauss(X+1, Y+3) + DimGauss(X+3, Y+3) + DimGauss(X+2, Y+3) * 2
Convol - DimGauss(X+2, Y+2) * 16
If Convol < 000 : Convol = 000 : EndIf
If Convol > 255 : Convol = 255 : EndIf
Plot(X, Y, RGB(Convol, Convol, Convol))
Next
Next
DrawImage(ImageID(ImageID), 010, 010, ImageW/4, ImageH/4)
StopDrawing()
EndIf
EndProcedure
FileName$ = "..\Test2.jpg"
LoadImage(#picScreen, FileName$)
ImageW = ImageWidth(#picScreen)
ImageH = ImageHeight(#picScreen)
WindowFlags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget
hWindow = OpenWindow(#winScreen, 0, 0, ImageW, ImageH, "LOG_高斯拉普拉斯算子", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH)
Drawing_cvsScreen2()
Repeat
EventNum = WindowEvent()
GadgetID = EventGadget()
EventType = EventType()
Select EventNum
Case #PB_Event_CloseWindow : IsExitWindow = #True
Case #PB_Event_SizeWindow
Case #PB_Event_Gadget
EndSelect
Delay(1)
Until IsExitWindow = #True
End
DataSection
_Bin_Laplace:
Data.f 00, 00, 01, 00, 00
Data.f 00, 01, 02, 01, 00
Data.f 01, 02,-16, 02, 01
Data.f 00, 01, 02, 01, 00
Data.f 00, 00, 01, 00, 00
EndDataSection