python - Gradient computation by convolution masks -


i need compute gradient of matrix (3,3), a=array([[1,4,2],[6,2,4],[7,5,1]]).

i use:

from numpy import * dx,dy = gradient(a) >>> dx    array([[ 5. , -2. ,  2. ],    [ 3. ,  0.5, -0.5],    [ 1. ,  3. , -3. ]]) >>> dy array([[ 3. ,  0.5, -2. ],    [-4. , -1. ,  2. ],    [-2. , -3. , -4. ]]) 

i know way computing gradient of matrix convolution mask each direction, results different

from scipy import ndimage mx=array([[-1,0,1],[-1,0,1],[-1,0,1]]) my=array([[-1,-1,-1],[0,0,0],[1,1,1]]) cx=ndimage.convolve(a,mx) cy=ndimage.convolve(a,my) >>> cx array([[-2,  0,  2],    [ 3,  7,  4],    [ 8, 14,  6]]) >>> cy array([[ -8,  -5,  -2],    [-13,  -6,   1],    [ -5,  -1,   3]]) 

where error?

with image small (3x3), centre pixel going subject boundary conditions, making results pretty meaningless.

but anyway, quick @ the documentation numpy.gradient reveals:

the gradient computed using central differences in interior , first differences @ boundaries.

in other words, doesn't use fixed convolution kernel across entire image. sounds (array(i+1,j) - array(i-1,j)) / 2 interior points, , (array(i,j) - array(i-1,j) boundary points.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -