[Image-SIG] Implement sobel filter

Claudio claudyus84 at gmail.com
Wed Aug 31 14:03:39 CEST 2011


Hi all,
I'm tring to implement a sobel filter using PIL library with few success.

Now I never used this library before so any help is appreciate.
I wrote the following code that essential is a standard implementation
of the sobel filter:

input img, output dimg:
dx = [-1,  0,  1, -2, 0, 2, -1, 0, 1]
dy = [-1, -2, -1,  0, 0, 0,  1, 2, 1]

ximg = img.filter(ImageFilter.Kernel((3,3), dx, None, 0)) #apply the
kernel in x e y
yimg = img.filter(ImageFilter.Kernel((3,3), dy, None, 0))

dimg = img.copy()
dest = dimg.load()
xdata = ximg.load() #retrieve the x e y data
ydata = yimg.load()

for y in range ( img.size[1]):			# calcolo il gradiente per ogni punto
	for x in range( img.size[0]):
#	     dest[x, y] = sqrt( xdata[x, y] * xdata[x, y] + ydata[x, y] * ydata[x, y])
	     dest[x, y] = abs( xdata[x, y] ) + abs (ydata[x, y])

The result is not what I expected, so I misunderstood some function usage?


More information about the Image-SIG mailing list