# [Numpy-discussion] Sobel question

Bob Klimek klimek at grc.nasa.gov
Fri Nov 12 13:23:02 CST 2004

```Peter Verveer wrote:

> Thanks for your interest! I hope it is useful for you...

I think it will be, especially the segmentation and object measurements.
I'm apparently running into a learing curve so please bear with me.

> The Sobel filter is always a derivative along  a single direction. A
> negative value is used to specifiy an axis relative to the rank of the
> array, so for a 2D array, axis=-1 is equivalent to axis=1, ...

In my tests axis=-1 and axis=1 produce different results. See code
below. And of course when I scale the results to a range of 0 to 255 and
convert to an image, the image is different also. Is this a bug or am I
missing something?

import numarray
import numarray.nd_image as ND

a = numarray.zeros([10, 10]) + 50
a = a.astype(numarray.UInt8)
for y in range(3,7):
for x in range(3,7):
a[y, x] = 200

a = a.astype(numarray.Float32)  # convert to float

h1 = ND.sobel(a, 1)
h2 = ND.sobel(a, -1)

print 'min:', min(numarray.ravel(h1)), ' max:', max(numarray.ravel(h1))
print 'min:', min(numarray.ravel(h2)), ' max:', max(numarray.ravel(h2))

> If you want a gradient magnitude using Sobel derivatives, you can use

This looks good except I can't get it to work. Can you show me a little
code fragment? I've gotten the following code to work but your way might
be faster.

vert = ND.sobel(a, 0)
horz = ND.sobel(a, 1)
mag = numarray.sqrt((horz * horz) + (vert * vert))

Regards,
Bob

```