Operator of Neural Network

Share your advanced PureBasic knowledge/code with the community.
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Operator of Neural Network

Post by MiLoo »

Operator of Neural Network
神经网络算子
1. Gaussian_高斯滤波算子
2. Roberts_罗伯茨算子
3. Prewitt_普威特算子
4. Laplace_拉普拉斯算子
5. Kirsch_基尔希算子
6. Robinson_鲁滨逊算子
7. Sobel_索贝尔算子
8. Canny_坎尼边缘检测
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

1. Gaussian_高斯滤波算子
[Gauss_高斯模糊3x3.pb]

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Gauss_高斯模糊/滤波3x3
;***********************************

Structure __PixelInfo
   R.l
   G.l
   B.l
EndStructure

#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Kernel_Gauss3x3(Array DimKernel.f(2), Weight.f)
   Total.f = 0
   For Y = -1 To 1
      For X = -1 To 1
         Kernel.f =(1/(2*#PI*Weight*Weight)) * Exp(-(X*X+Y*Y)/(2*Weight*Weight))
         Total + Kernel
         DimKernel(X+1,Y+1) = Kernel
      Next 
   Next 
   
   For Y = 0 To 3-1
      For X = 0 To 3-1
         DimKernel(X,Y) = DimKernel(X,Y)/Total
      Next 
   Next 
EndProcedure



Procedure Operator_Gauss(GagetID, ImageID, ImageW, ImageH)
   
   Dim DimPixel.__PixelInfo(ImageW+3-1, ImageH+3-1)     ;像素
   Dim DimKernel.f(3-1, 3-1)                            ;卷积核
   
   Kernel_Gauss3x3(DimKernel(), 1.5)

   If StartDrawing(CanvasOutput(GagetID))
      DrawImage(ImageID(ImageID), 0, 0)
      DrawImage(ImageID(ImageID), 000, ImageH)
      ;图像灰度化
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Color = Point(X, Y)
            DimPixel(X+1, Y+1)\R = Red  (Color)
            DimPixel(X+1, Y+1)\G = Green(Color)
            DimPixel(X+1, Y+1)\B = Blue (Color)
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolR.f = 0
            ConvolG.f = 0
            ConvolB.f = 0
            For j = 0 To 3-1
               For i = 0 To 3-1
                  ConvolR + DimPixel(X+i, Y+j)\R * DimKernel(i, j)
                  ConvolG + DimPixel(X+i, Y+j)\G * DimKernel(i, j)
                  ConvolB + DimPixel(X+i, Y+j)\B * DimKernel(i, j)
               Next 
            Next 
            If ConvolR < 000 : ConvolR = 000 : EndIf 
            If ConvolR > 255 : ConvolR = 255 : EndIf 
            If ConvolG < 000 : ConvolG = 000 : EndIf 
            If ConvolG > 255 : ConvolG = 255 : EndIf
            If ConvolB < 000 : ConvolB = 000 : EndIf 
            If ConvolB > 255 : ConvolB = 255 : EndIf               
            Plot(X, Y, RGB(ConvolR, ConvolG, ConvolB))
         Next 
      Next          
      
      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*2, "Gauss_高斯模糊/滤波5x5", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH*2)
Operator_Gauss(#cvsScreen, #picScreen, ImageW, ImageH)

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

I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

1. Gaussian_高斯滤波算子
Gauss_高斯模糊3x3_灰度

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Gauss_高斯模糊_灰度/滤波3x3
;***********************************

#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Kernel_Gauss3x3(Array DimKernel.f(2), Weight.f)
   Total.f = 0
   For Y = -1 To 1
      For X = -1 To 1
         Kernel.f =(1/(2*#PI*Weight*Weight)) * Exp(-(X*X+Y*Y)/(2*Weight*Weight))
         Total + Kernel
         DimKernel(X+1,Y+1) = Kernel
      Next 
   Next 
   
   For Y = 0 To 3-1
      For X = 0 To 3-1
         DimKernel(X,Y) = DimKernel(X,Y)/Total
      Next 
   Next 
EndProcedure



Procedure Operator_Gauss(GagetID, ImageID, ImageW, ImageH)
   
   Dim DimPixel.f(ImageW+3-1, ImageH+3-1)    ;灰度像素
   Dim DimKernel.f(3-1, 3-1)                 ;卷积核
   
   Kernel_Gauss3x3(DimKernel(), 1.5)

   If StartDrawing(CanvasOutput(GagetID))
      DrawImage(ImageID(ImageID), 0, 0)
      DrawImage(ImageID(ImageID), 000, ImageH)
      ;图像灰度化
      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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Convol.f = 0
            For j = 0 To 3-1
               For i = 0 To 3-1
                  Convol + DimPixel(X+i, Y+j) * DimKernel(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          
      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*2, "Gauss_高斯模糊/滤波5x5", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH*2)
Operator_Gauss(#cvsScreen, #picScreen, ImageW, ImageH)

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
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

1. Gaussian_高斯滤波算子
Gauss_高斯模糊5x5

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Gauss_高斯模糊/滤波5x5
;***********************************

Structure __PixelInfo
   R.l
   G.l
   B.l
EndStructure

#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 Operator_Gauss(GagetID, ImageID, ImageW, ImageH)
   
   Dim DimPixel.__PixelInfo(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)
      DrawImage(ImageID(ImageID), 000, ImageH)
      ;图像灰度化
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Color = Point(X, Y)
            DimPixel(X+2, Y+2)\R = Red  (Color)
            DimPixel(X+2, Y+2)\G = Green(Color)
            DimPixel(X+2, Y+2)\B = Blue (Color)
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolR.f = 0
            ConvolG.f = 0
            ConvolB.f = 0
            For j = 0 To 5-1
               For i = 0 To 5-1
                  ConvolR + DimPixel(X+i, Y+j)\R * DimKernel(i, j)
                  ConvolG + DimPixel(X+i, Y+j)\G * DimKernel(i, j)
                  ConvolB + DimPixel(X+i, Y+j)\B * DimKernel(i, j)
               Next 
            Next 
            
            If ConvolR < 000 : ConvolR = 000 : EndIf 
            If ConvolR > 255 : ConvolR = 255 : EndIf 
            If ConvolG < 000 : ConvolG = 000 : EndIf 
            If ConvolG > 255 : ConvolG = 255 : EndIf
            If ConvolB < 000 : ConvolB = 000 : EndIf 
            If ConvolB > 255 : ConvolB = 255 : EndIf               
            Plot(X, Y, RGB(ConvolR, ConvolG, ConvolB))
         Next 
      Next          
      
      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*2, "Gauss_高斯模糊/滤波5x5", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH*2)
Operator_Gauss(#cvsScreen, #picScreen, ImageW, ImageH)

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
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

1. Gaussian_高斯滤波算子
Gauss_高斯模糊5x5_灰度

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Gauss_高斯模糊_灰度/滤波5x5
;***********************************


#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 Operator_Gauss(GagetID, ImageID, ImageW, ImageH)
   
   Dim DimPixel.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)
      DrawImage(ImageID(ImageID), 000, ImageH)
      ;图像灰度化
      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
            Convol.f = 0
            For j = 0 To 5-1
               For i = 0 To 5-1
                  Convol + DimPixel(X+i, Y+j) * DimKernel(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          
      
      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*2, "Gauss_高斯模糊/滤波5x5", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH*2)
Operator_Gauss(#cvsScreen, #picScreen, ImageW, ImageH)

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
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

2. Roberts_罗伯茨算子
Roberts_罗伯茨算子卷积核

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Roberts_罗伯茨算子
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixel.f(ImageW+2-1, ImageH+2-1)      ;像素
   Dim DimKernelX.f(2-1, 2-1)                    ;X卷积核
   Dim DimKernelY.f(2-1, 2-1)                    ;Y卷积核
   CopyMemory(?_Bin_RobertsX, DimKernelX(), 2*2*4)
   CopyMemory(?_Bin_RobertsY, DimKernelY(), 2*2*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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX.f = 0
            ConvolY.f = 0
            For j = 0 To 2-1
               For i = 0 To 2-1
                  ConvolX + DimPixel(X+i, Y+j) * DimKernelX(i, j)
                  ConvolY + DimPixel(X+i, Y+j) * DimKernelY(i, j)
               Next 
            Next 
            Convol = (Abs(ConvolX)+Abs(ConvolY))/2
            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+3-1, ImageH+3-1)      ;像素

   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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 

      ;卷积并输出
      ConvolX.f = 0
      ConvolY.f = 0
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX = DimPixel(X+0, Y+0) - DimPixel(X+1, Y+1)
            ConvolY = DimPixel(X+1, Y+0) - DimPixel(X+0, Y+1)
            Convol = Abs(ConvolX)+Abs(ConvolY)
            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, "Roberts_罗伯茨算子", 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_RobertsX: ;罗伯茨算子卷积核 2x2
   Data.f  01.0, 00.0
   Data.f  00.0, -1.0
   
_Bin_RobertsY: ;罗伯茨算子卷积核 2x2
   Data.f  00.0, 01.0
   Data.f  -1.0, 00.0
EndDataSection
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

3. Prewitt_普威特算子
Prewitt_普威特算子

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Prewitt_普威特算子
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixel.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimKernelX.f(3-1, 3-1)                    ;X卷积核
   Dim DimKernelY.f(3-1, 3-1)                    ;Y卷积核
   CopyMemory(?_Bin_PrewittX, DimKernelX(), 3*3*4)
   CopyMemory(?_Bin_PrewittY, DimKernelY(), 3*3*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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX.f = 0
            ConvolY.f = 0
            For j = 0 To 3-1
               For i = 0 To 3-1
                  ConvolX + DimPixel(X+i, Y+j) * DimKernelX(i, j)
                  ConvolY + DimPixel(X+i, Y+j) * DimKernelY(i, j)
               Next 
            Next 
            Convol = (Abs(ConvolX)+Abs(ConvolY))/2
            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+3-1, ImageH+3-1)      ;像素

   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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 

      ;卷积并输出
      ConvolX.f = 0
      ConvolY.f = 0
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX = DimPixel(X+2, Y+0) + DimPixel(X+2, Y+1) + DimPixel(X+2, Y+2)
            ConvolX - DimPixel(X+0, Y+0) - DimPixel(X+0, Y+1) - DimPixel(X+0, Y+2)
            
            ConvolY = DimPixel(X+0, Y+0) + DimPixel(X+1, Y+0) + DimPixel(X+2, Y+0)
            ConvolY - DimPixel(X+0, Y+2) - DimPixel(X+1, Y+2) - DimPixel(X+2, Y+2)
            Convol = (Abs(ConvolX)+Abs(ConvolY))/2
            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, "Prewitt_普威特算子", 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_PrewittX: ;普威特算子卷积核 3x3[横向]
   Data.f  -1.0, 00.0, 01.0
   Data.f  -1.0, 00.0, 01.0
   Data.f  -1.0, 00.0, 01.0
_Bin_PrewittY: ;普威特算子卷积核 3x3[纵向]
   Data.f  01.0, 01.0, 01.0
   Data.f  00.0, 00.0, 00.0
   Data.f  -1.0, -1.0, -1.0
EndDataSection
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

4. Laplace_拉普拉斯算子
Laplace_拉普拉斯算子_标准核

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Laplace_拉普拉斯算子_标准核
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixels.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimKernel.f(3-1, 3-1)                    ;卷积核
   CopyMemory(?_Bin_Laplace, DimKernel(), 3*3*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)
            DimPixels(X+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Convol.f = 0
            For j = 0 To 3-1
               For i = 0 To 3-1
                  Convol + DimPixels(X+i, Y+j) * DimKernel(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 DimPixels.f(ImageW+3-1, ImageH+3-1)      ;像素

   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)
            DimPixels(X+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      Convol.f = 0
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Convol = DimPixels(X+1, Y+0) + DimPixels(X+0, Y+1)
            Convol + DimPixels(X+2, Y+1) + DimPixels(X+1, Y+2)             
            Convol - DimPixels(X+1, Y+1) * 4
            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, "Laplace_拉普拉斯算子_标准核", 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: ;拉普拉斯标准卷积核 3x3 σ=1.0
   Data.f  0.0,  1.0,  0.0
   Data.f  1.0, -4.0,  1.0
   Data.f  0.0,  1.0,  0.0
EndDataSection

I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

4. Laplace_拉普拉斯算子
Laplace_拉普拉斯算子_拓展核

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Laplace_拉普拉斯算子_拓展核
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixels.f(ImageW+5-1, ImageH+5-1)      ;像素
   Dim DimKernel.f(5-1, 5-1)                    ;卷积核
   CopyMemory(?_Bin_Laplace, DimKernel(), 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)
            DimPixels(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
            Convol.f = 0
            For j = 0 To 5-1
               For i = 0 To 5-1
                  Convol + DimPixels(X+i, Y+j) * DimKernel(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)      ;像素

   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 
      
      ;卷积并输出
      Convol.f = 0
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Convol = DimPixel(X+2, Y+0) + DimPixel(X+2, Y+4) + DimPixel(X+2, Y+1) * 2 
            Convol + DimPixel(X+1, Y+1) + DimPixel(X+3, Y+1) + DimPixel(X+1, Y+2) * 2            
            Convol + DimPixel(X+0, Y+2) + DimPixel(X+4, Y+2) + DimPixel(X+3, Y+2) * 2            
            Convol + DimPixel(X+1, Y+3) + DimPixel(X+3, Y+3) + DimPixel(X+2, Y+3) * 2           
            Convol - DimPixel(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, "Laplace_拉普拉斯算子_拓展核", 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
I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

4. Laplace_拉普拉斯算子
LOG_高斯拉普拉斯算子

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

I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

5. Kirsch_基尔希算子
Kirsch_基尔希算子

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Kirsch_基尔希算子
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixel.f(ImageW+3-1, ImageH+3-1)       ;像素
   Dim DimKernel.f(8-1, 3-1, 3-1)               ;卷积核
   CopyMemory(?_Bin_Kirsch0, DimKernel(), 8*3*3*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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            MaxConvol.f = 0
            For k = 0 To 7
               Convol.f = 0
               For j = 0 To 3-1
                  For i = 0 To 3-1
                     Convol + DimPixel(X+i, Y+j) * DimKernel(k, i, j)
                  Next 
               Next 
               Convol = Abs(Convol)
               If MaxConvol < Convol : MaxConvol = Convol : EndIf 
            Next 
            If MaxConvol < 000 : MaxConvol = 000 : EndIf 
            If MaxConvol > 255 : MaxConvol = 255 : EndIf 
            Plot(X, Y, RGB(MaxConvol, MaxConvol, MaxConvol))
         Next 
      Next       
      DrawImage(ImageID(ImageID), 010, 010, ImageW/4, ImageH/4)
      StopDrawing()
   EndIf
   
EndProcedure


FileName$ = "..\Test1.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, "Kirsch_基尔希算子", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH)
Drawing_cvsScreen()

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_Kirsch0: ;基尔希算子M0卷积核 3x3
   Data.f  05, 05, 05
   Data.f  -3, 00, -3
   Data.f  -3, -3, -3

_Bin_Kirsch1: ;基尔希算子M1卷积核 3x3
   Data.f  -3, 05, 05
   Data.f  -3, 00, 05
   Data.f  -3, -3, -3

_Bin_Kirsch2: ;基尔希算子M2卷积核 3x3
   Data.f  -3, -3, 05
   Data.f  -3, 00, 05
   Data.f  -3, -3, 05

_Bin_Kirsch3: ;基尔希算子M3卷积核 3x3
   Data.f  -3, -3, -3
   Data.f  -3, 00, 05
   Data.f  -3, 05, 05

_Bin_Kirsch4: ;基尔希算子M4卷积核 3x3
   Data.f  -3, -3, -3
   Data.f  -3, 00, -3
   Data.f  05, 05, 05

_Bin_Kirsch5: ;基尔希算子M5卷积核 3x3
   Data.f  -3, -3, -3
   Data.f  05, 00, -3
   Data.f  05, 05, -3

_Bin_Kirsch6: ;基尔希算子M6卷积核 3x3
   Data.f  05, -3, -3
   Data.f  05, 00, -3
   Data.f  05, -3, -3

_Bin_Kirsch7: ;基尔希算子M7卷积核 3x3
   Data.f  05, 05, -3
   Data.f  05, 00, -3
   Data.f  -3, -3, -3
EndDataSection



I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

6. Robinson_鲁滨逊算子
Robinson_鲁滨逊算子

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Robinson_鲁滨逊算子
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixel.f(ImageW+3-1, ImageH+3-1)       ;像素
   Dim DimKernel.f(8-1, 3-1, 3-1)               ;卷积核
   CopyMemory(?_Bin_Robinson0, DimKernel(), 8*3*3*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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            MaxConvol.f = 0
            For k = 0 To 7
               Convol.f = 0
               For j = 0 To 3-1
                  For i = 0 To 3-1
                     Convol + DimPixel(X+i, Y+j) * DimKernel(k, i, j)
                  Next 
               Next 
               Convol = Abs(Convol)
               If MaxConvol < Convol : MaxConvol = Convol : EndIf 
            Next 
            If MaxConvol < 000 : MaxConvol = 000 : EndIf 
            If MaxConvol > 255 : MaxConvol = 255 : EndIf 
            Plot(X, Y, RGB(MaxConvol, MaxConvol, MaxConvol))
         Next 
      Next       
      DrawImage(ImageID(ImageID), 010, 010, ImageW/4, ImageH/4)
      StopDrawing()
   EndIf
   
EndProcedure


FileName$ = "..\Test1.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, "Robinson_鲁滨逊算子", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH)
Drawing_cvsScreen()

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_Robinson0: ;基尔希算子M0卷积核 3x3
   Data.f  01, 02, 01
   Data.f  00, 00, 00
   Data.f  -1, -2, -1

_Bin_Robinson1: ;基尔希算子M1卷积核 3x3
   Data.f  00, 01, 02
   Data.f  -1, 00, 01
   Data.f  -2, -1, 00

_Bin_Robinson2: ;基尔希算子M2卷积核 3x3
   Data.f  -1, 00, 01
   Data.f  -2, 00, 02
   Data.f  -1, 00, 01

_Bin_Robinson3: ;基尔希算子M3卷积核 3x3
   Data.f  -2, -1, 00
   Data.f  -1, 00, 01
   Data.f  00, 01, 02

_Bin_Robinson4: ;基尔希算子M4卷积核 3x3
   Data.f  -1, -2, -1
   Data.f  00, 00, 00
   Data.f  01, 02, 01

_Bin_Robinson5: ;基尔希算子M5卷积核 3x3
   Data.f  00, -1, -2
   Data.f  01, 00, -1
   Data.f  02, 01, 00

_Bin_Robinson6: ;基尔希算子M6卷积核 3x3
   Data.f  01, 00, -1
   Data.f  02, 00, -2
   Data.f  01, 00, -1

_Bin_Robinson7: ;基尔希算子M7卷积核 3x3
   Data.f  02, 01, 00
   Data.f  01, 00, -1
   Data.f  00, -1, -2
EndDataSection


I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

7. Sobel_索贝尔算子
Sobel_索贝尔算子

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Sobel_索贝尔算子
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0

UseJPEGImageDecoder()


Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixel.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimKernelX.f(3-1, 3-1)                    ;X卷积核
   Dim DimKernelY.f(3-1, 3-1)                    ;Y卷积核
   CopyMemory(?_Bin_SobelX, DimKernelX(), 3*3*4)
   CopyMemory(?_Bin_SobelY, DimKernelY(), 3*3*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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;卷积并输出
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX.f = 0
            ConvolY.f = 0
            For j = 0 To 3-1
               For i = 0 To 3-1
                  ConvolX + DimPixel(X+i, Y+j) * DimKernelX(i, j)
                  ConvolY + DimPixel(X+i, Y+j) * DimKernelY(i, j)
               Next 
            Next 
            Convol = (Abs(ConvolX)+Abs(ConvolY))/2
            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+3-1, ImageH+3-1)      ;像素

   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+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 

      ;卷积并输出
      ConvolX.f = 0
      ConvolY.f = 0
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX = DimPixel(X+2, Y+0) + DimPixel(X+2, Y+1)*2 + DimPixel(X+2, Y+2)
            ConvolX - DimPixel(X+0, Y+0) - DimPixel(X+0, Y+1)*2 - DimPixel(X+0, Y+2)
            ConvolY = DimPixel(X+0, Y+0) + DimPixel(X+1, Y+0)*2 + DimPixel(X+2, Y+0)
            ConvolY - DimPixel(X+0, Y+2) - DimPixel(X+1, Y+2)*2 - DimPixel(X+2, Y+2)
            Convol = (Abs(ConvolX)+Abs(ConvolY))/2
            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, "Sobel_索贝尔算子", 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_SobelX: ;索贝尔算子卷积核 3x3[横向-0度]
   Data.f  -1.0, 00.0, 01.0
   Data.f  -2.0, 00.0, 02.0
   Data.f  -1.0, 00.0, 01.0

_Bin_SobelY: ;索贝尔算子卷积核 3x3[纵向-90度]
   Data.f  01.0, 02.0, 01.0
   Data.f  00.0, 00.0, 00.0
   Data.f  -1.0, -2.0, -1.0
EndDataSection

I came to the ancient oriental country - China
I will PureBasic called B++
MiLoo
User
User
Posts: 47
Joined: Fri Jan 28, 2011 12:26 pm

Re: Operator of Neural Network

Post by MiLoo »

8. Canny_坎尼边缘检测
Canny_坎尼边缘检测

Code: Select all

;***********************************
;迷路仟整理 2021.05.08
;Canny_坎尼边缘检测
;***********************************
#winScreen = 0
#cvsScreen = 0
#picScreen = 0


Structure __ConvolInfo
   Result.f
   Angles.f
EndStructure

UseJPEGImageDecoder()


Procedure Kernel_Gauss3x3(Array DimKernel.f(2), Weight.f)
   Total.f = 0
   For Y = -1 To 1
      For X = -1 To 1
         Kernel.f =(1/(2*#PI*Weight*Weight)) * Exp(-(X*X+Y*Y)/(2*Weight*Weight))
         Total + Kernel
         DimKernel(X+1,Y+1) = Kernel
      Next 
   Next 
   
   For Y = 0 To 3-1
      For X = 0 To 3-1
         DimKernel(X,Y) = DimKernel(X,Y)/Total
      Next 
   Next 
EndProcedure

Procedure Kernel_Trace(Array DimResult.f(2), Array DimEdges.f(2), LoThreshold, X, Y, ImageW, ImageH)
   If DimEdges(X+1, Y+1) = 0
      DimEdges(X+1, Y+1) = 255
      For j = 0 To 2-1
         For i = 0 To 2-1
            Result.f = DimResult(X+1+i, Y+1+j)
            If X+1+i >= 0 And Y+1+j >= 0 And X+1+i < ImageW And Y+1+j < ImageH
               If Result >= LoThreshold
                  Kernel_Trace(DimResult(), DimEdges(), LoThreshold, X+i, Y+j, ImageW, ImageH)
               EndIf 
            EndIf 
         Next 
      Next 
   EndIf 
EndProcedure

Procedure Drawing_cvsScreen()
   
   ImageW = ImageWidth(#picScreen)
   ImageH = ImageHeight(#picScreen)
   Dim DimPixels.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimConvol.__ConvolInfo(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimGauss.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimEdges.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimResult.f(ImageW+3-1, ImageH+3-1)      ;像素
   Dim DimKernel.f(3-1, 3-1)                 ;卷积核
   
   Kernel_Gauss3x3(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)
            DimPixels(X, Y) = (R * 299 + G * 587 + B * 114)/1000
         Next 
      Next 
      
      ;高斯模糊
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Convol.f = 0
            For j = 0 To 3-1
               For i = 0 To 3-1
                  Convol + DimPixels(X+i, Y+j) * DimKernel(i, j)
               Next 
            Next 
            If Convol < 000 : Convol = 000 : EndIf 
            If Convol > 255 : Convol = 255 : EndIf  
            DimGauss(X+1, Y+1) = Convol
         Next 
      Next      
      
      ;Sobel_索贝尔算子
      ConvolX.f = 0
      ConvolY.f = 0
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            ConvolX = DimGauss(X+2, Y+0) + DimGauss(X+2, Y+1)*2 + DimGauss(X+2, Y+2)
            ConvolX - DimGauss(X+0, Y+0) - DimGauss(X+0, Y+1)*2 - DimGauss(X+0, Y+2)
            ConvolY = DimGauss(X+0, Y+0) + DimGauss(X+1, Y+0)*2 + DimGauss(X+2, Y+0)
            ConvolY - DimGauss(X+0, Y+2) - DimGauss(X+1, Y+2)*2 - DimGauss(X+2, Y+2)
            Result.f = (Abs(ConvolX)+Abs(ConvolY))
;             Result.f = ConvolX*ConvolX + ConvolY*ConvolY
;             Result.f = Pow(Result, 0.5)
            If Result < 000 : Result = 000 : EndIf 
            If Result > 255 : Result = 255 : EndIf  
            DimConvol(X+1, Y+1)\Result = Result
            DimConvol(X+1, Y+1)\Angles = Degree(ATan(ConvolY/(ConvolX)))
         Next 
      Next 
      
      
      ;Canny_坎尼边缘检测
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Angles.f = DimConvol(X+1, Y+1)\Angles
            Result.f = DimConvol(X+1, Y+1)\Result
            ;垂直边缘--梯度方向为水平方向-3*3邻域内左右方向比较
            If Abs(Angles) < 22.5 Or Abs(Angles) > 157.5 
               If Result >= DimConvol(X+1-1, Y+1)\Result And 
                  Result >= DimConvol(X+1+1, Y+1)\Result
                  DimResult(X+1, Y+1) = Result
               EndIf 

            ;水平边缘--梯度方向为垂直方向-3*3邻域内上下方向比较
            ElseIf (Angles >= 67.5 And Angles <=112.5) Or (Angles >= -112.5 And Angles <=-67.5)
               If Result >= DimConvol(X+1, Y+1-1)\Result And 
                  Result >= DimConvol(X+1, Y+1+1)\Result
                  DimResult(X+1, Y+1) = Result
               EndIf                
               
            ;+45°边缘--梯度方向为其正交方向-3*3邻域内右上左下方向比较   
            ElseIf (Angles > 112.5 And Angles <=157.5) Or (Angles >-67.5 And Angles <=-22.5)  
               If Result >= DimConvol(X+1+1, Y+1-1)\Result And 
                  Result >= DimConvol(X+1-1, Y+1+1)\Result
                  DimResult(X+1, Y+1) = Result
               EndIf
               
            ;+135°边缘--梯度方向为其正交方向-3*3邻域内右下左上方向比较  
            ElseIf (Angles >= 22.5 And Angles < 67.5) Or (Angles >= -157.5 And Angles < -112.5)  
               If Result >= DimConvol(X+1-1, Y+1-1)\Result And 
                  Result >= DimConvol(X+1+1, Y+1+1)\Result
                  DimResult(X+1, Y+1) = Result
               EndIf
            EndIf  
         Next 
      Next 
      
      ;Canny_坎尼: 双阈值处理及边缘连接
      HiThreshold.f = 80
      LoThreshold.f = 20
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1
            Result.f = DimResult(X+1, Y+1)
            If Result > HiThreshold
               Kernel_Trace(DimResult(), DimEdges(), LoThreshold, X, Y, ImageW, ImageH)
			   ElseIf Result < LoThreshold
			      DimEdges(X+1, Y+1) = 0
	         EndIf 
         Next 
      Next 	         
	         
      For Y = 0 To ImageH-1
         For X = 0 To ImageW-1 
            Result = DimEdges(X+1, Y+1)
            If Result < 000 : Result = 000 : EndIf 
            If Result > 255 : Result = 255 : EndIf 
            Plot(X, Y, RGB(Result, Result, Result))  
         Next 
      Next 	 
      DrawImage(ImageID(ImageID), 010, 010, ImageW/4, ImageH/4)
      StopDrawing()
   EndIf
EndProcedure




FileName$ = "..\Test3.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, "Canny_坎尼边缘检测", WindowFlags)
CanvasGadget(#cvsScreen, 000, 000, ImageW, ImageH)
Drawing_cvsScreen()

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





I came to the ancient oriental country - China
I will PureBasic called B++
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Operator of Neural Network

Post by Keya »

Hi,
This is not a neural network - it is known as a convolution filter or convolution matrix or kernel (wikipedia). I've provided an example with 60+ filters (using "plug-in" parameters so you don't need a different code base for each operator/filter):
https://www.purebasic.fr/english/viewtopic.php?p=505099

btw, you are using some interesting constants in your code, where do they come from? eg.:

Code: Select all

DimPixel(X+1, Y+1) = (R * 299 + G * 587 + B * 114)/1000
Post Reply