[SciPy-user] Sobel filter, magnitude and gradient direction

Robert Kern robert.kern at gmail.com
Fri Mar 21 16:37:07 EDT 2008


On Fri, Mar 21, 2008 at 3:09 PM, Daniele Di Mauro <daniele.dm at gmail.com> wrote:
> Hi folks,
> it's the first time i write on this mailing list, i hope i chose the right
> one. Yesterday i started a small project for university and i need some
> help:
> I've to calculate the sobel edge magnitude and gradient direction of an
> image, and i haven't undestood if i'm doing right, btw this is the code:
>
> import Image
> import scipy
> import scipy.ndimage
>
> data = Image.open("image.jpg")
> image_magnitude = scipy.ndimage.filters.generic_gradient_magnitude(data,
> scipy.ndimage.filters.sobel)
> output = Image.fromstring("RGB",(320,210),image_magnitude.tostring())
> output.save("imagem.jpg","JPEG")
>
> I've not look at the gradient direction part yet, but the result it's quite
> strange, i cannot see edges.

The "nd" part of ndimage means that it applies to N-dimensional
images. In your case, you are giving it an array of (320,210,3). It is
treating that last color axis as just another axis. It is not applying
itself across each color channel separately.

>  I tried also this:
>
>  import Image
>  import scipy
>  import scipy.ndimage
>
>  data = Image.open("image.jpg")
>  image_magnitude = scipy.ndimage.filters.sobel(data)
>  output = Image.fromstring("RGB",(320,210),image_magnitude.tostring())
>  output.save("imagem.jpg","JPEG")
>
> but it doesn't convice me either coz it looks quite different from the
> result of gimp built-in version or using gimp matrix convolution. Thanx in
> advance for your help

The default axis is -1, so the Sobel operator is being applied along
the color axis, not one of the X or Y axes. Use the parameter axis=1
and axis=0 to get those, respectively.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco



More information about the SciPy-User mailing list