my aim is to create and visualize the partial derivatives of a image (2D). I´ll do this with the first finite central difference equation wikipedia .
the partial derivative of F with respect to x is
df(x,y)/dx=f(x+1,y)-f(x-1,y)
we can write this as a convolve kernel H=[-1,0,1] and should get the same result by convolve the image with the kernel
dfx=F*H
Here is my source-code:
from numpy import *
from pylab import *
from PIL import *
from scipy import signal as sg
#create artifical image with constant positive slope
myImage=zeros((5,5),dtype=float)
for y in range(5):
for x in range(5):
myImage[y,x]=y+x
at first I create the first central finite difference for x and y by the convolve2d - function from the scipy modul (the short way)
kernel=array([[-1,0,1]]) #create first central finite difference kernel
pdx=sg.convolve2d(myImage,kernel,"same") #dI(x,y)/dx
and now I create the same using 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 see the results:
pdx - kernel 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 - finite difference by loops
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.]])
I´m confused about the results. Why has the kernel method a negative slope ? To get the same results i have to flip the kernel to H=[1,0,-1], but this is mathematically not correct. Can someone help me ?