Python scipy convolve2d seems wrong

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

#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

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]])     #create first central finite difference kernel
pdx=sg.convolve2d(myImage,kernel,"same")   #dI(x,y)/dx 

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], . - ?

+4
2

:

  • .
  • , scipy.signal

, , , scipy.ndimage.convolve scipy.signal.convolve2d. ndimage , , .


scipy.signal, , .

. , "". : http://en.wikipedia.org/wiki/File:Convolution3.PNG


-, scipy.signal.convolve2d , . scipy.signal.convolve2d, boundary='symm' ( , ... , , convolve2d .)


, ( ) scipy.ndimage.convolve. ( ) scipy.ndimage.convolve1d. . scipy.ndimage.convolve1d(data, [1, 0, -1], axis=0)

+8

f g f (x ') g (x - x') dx '. , ​​. , scipy.ndimage.correlate, f (x ') g (x + x'), ​​ , .

. Convolution, . Python?.

+3

All Articles