Thursday, 15 April 2010

python - Why the orientation of image convolution operators are not intuitive? -



python - Why the orientation of image convolution operators are not intuitive? -

thanks reading , please excuse poor english language since i'm not native speaker.

for example, sobel operator: http://en.wikipedia.org/wiki/sobel_operator

the right side positive , left side negative in gx, while upper positive in gy.

i'm not specialist of image processing. presume people count pixels left-upper of image. since need "flip" kernel during process of convolutions, why gx not mirrored 1 improve regularity when defining operators?

i know technically it's not programming question. it's related programming. example, python's scipy.ndimage provides sobel function. function uses kernel left column positive , right column negative. differs materials image processing can find (including wikipedia articles). there special reasons real implementation differs mathematical definition?

first off, allow me rephrase question slightly:

why scipy.ndimage's version of sobel operator seem reversed definition given on wikipedia?

here's side-by-side comparing show differences. input, we're using bike image in wikipedia article: http://en.wikipedia.org/wiki/file:bikesgray.jpg

import matplotlib.pyplot plt import numpy np import scipy.ndimage ndimage # note if leave unsigned integers we'll have problems # underflow anywhere gradient negative. therefore, we'll cast floats. z = ndimage.imread('bikesgray.jpg').astype(float) # wikipedia definition of x-direction sobel operator... kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=float) sobel_wiki = ndimage.convolve(z, kernel) # scipy.ndimage version of x-direction sobel operator... sobel_scipy = ndimage.sobel(z, axis=1) fig, axes = plt.subplots(figsize=(6, 15), nrows=3) axes[0].set(title='original') axes[0].imshow(z, interpolation='none', cmap='gray') axes[1].set(title='wikipedia definition') axes[1].imshow(sobel_wiki, interpolation='none', cmap='gray') axes[2].set(title='scipy.ndimage definition') axes[2].imshow(sobel_scipy, interpolation='none', cmap='gray') plt.show()

notice values flipped.

the logic behind sobel filter gradient operator (numpy.gradient equivalent convolution [1, 0, -1] except @ edges). definition given in wikipedia gives negative of mathematical definition of gradient.

for example, numpy.gradient gives similar result scipy.ndimage's sobel filter:

import matplotlib.pyplot plt import numpy np import scipy.ndimage ndimage z = ndimage.imread('/home/jofer/bikesgray.jpg').astype(float) sobel = ndimage.sobel(z, 1) gradient_y, gradient_x = np.gradient(z) fig, axes = plt.subplots(ncols=2, figsize=(12, 5)) axes[0].imshow(sobel, interpolation='none', cmap='gray') axes[0].set(title="scipy's sobel") axes[1].imshow(gradient_x, interpolation='none', cmap='gray') axes[1].set(title="numpy's gradient") plt.show()

therefore, scipy.ndimage using convention that's consistent we'd expect mathematical gradient.

quick side note: what's referred "sobel filter" gradient magnitude calculated 2 sobel filters along different axes. in scipy.ndimage, you'd calculate as:

import matplotlib.pyplot plt import scipy.ndimage ndimage z = ndimage.imread('/home/jofer/bikesgray.jpg').astype(float) sobel = ndimage.generic_gradient_magnitude(z, ndimage.sobel) fig, ax = plt.subplots() ax.imshow(sobel, interpolation='none', cmap='gray') plt.show()

it doesn't matter convention utilize in case, matters absolute value of input gradients.

regardless, utilize cases, smoother gradient filter adjustable window (e.g. ndimage.gaussian_gradient_magnitude) improve selection border detection.

python image-processing scipy imagefilter

No comments:

Post a Comment