[Image-SIG] Implement sobel filter

Guilherme Polo ggpolo at gmail.com
Wed Aug 31 15:50:01 CEST 2011


2011/8/31 Claudio <claudyus84 at gmail.com>:
> 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?

"not what I expected" is too vague. Maybe you have a 8 bits image and
the convolution only uses 8 bits to store the results and then you get
weird results ?


-- 
-- Guilherme H. Polo Goncalves


More information about the Image-SIG mailing list