My goal is to create and visualize private derived images (2D). I will do this with the first final central wikipedia difference equation
.
the partial derivative of F with respect to x is
DF (x, y) / dx = e (x + 1, y) -f (x-1, y)
we can write this as a convoluted core H = [- 1,0,1] and get the same result by convolution of the image with the core
DFX = P * H
Here is my source code:
from numpy import *
from pylab import *
from PIL import *
from scipy import signal as sg
myImage=zeros((5,5),dtype=float)
for y in range(5):
for x in range(5):
myImage[y,x]=y+x
first, I create the first center final difference for x and y with the convolve2d function from the scipy module (shortcut)
kernel=array([[-1,0,1]])
pdx=sg.convolve2d(myImage,kernel,"same")
and now i create the same with loops
H,W=myImage.shape[0:2]
pdx=zeros((H,W),dtype=float)
for y in range(1,H-1):
for x in range(1,W-1):
pdx.itemset(y,x,im.item(y,x+1)-im.item(y,x-1))
Let's look at the results:
pdx - nuclear method by convolve2d
array([[-1., -2., -2., -2., 3.],
[-2., -2., -2., -2., 4.],
[-3., -2., -2., -2., 5.],
[-4., -2., -2., -2., 6.],
[-5., -2., -2., -2., 7.]])
pdx - final difference in cycles
array([[ 0., 0., 0., 0., 0.],
[ 0., 2., 2., 2., 0.],
[ 0., 2., 2., 2., 0.],
[ 0., 2., 2., 2., 0.],
[ 0., 0., 0., 0., 0.]])
. ?
, H = [1,0, -1], .
- ?