'Difference between scipy.ndimage.gaussian_gradient_magnitude & gaussian_filter function

Anyone know what's the difference between scipy.ndimage.gaussian_gradient_magnitude [1] and scipy.ndimage.gaussian_filter[2] function?

I imagine that gaussian_gradient_magnitude(img, 1) & gaussian_filter(img, sigma= 1, order = 1) should return same results, but this is just not the case.

Thanks.



Solution 1:[1]

scipy.ndimage.gaussian_gradient_magnitude computes the magnitude of the gradient, which is the vector containing the partial derivatives along each axis. scipy.ndimage.gaussian_filter can compute those partial derivatives.

For a 2D image (img is a 2D NumPy array),

gm = scipy.ndimage.gaussian_gradient_magnitude(img, 1)

is the same as

dx = scipy.ndimage.gaussian_filter(img, sigma= 1, order = (0,1))
dy = scipy.ndimage.gaussian_filter(img, sigma= 1, order = (1,0))
gm = numpy.sqrt(dx**2 + dy**2)

But note that both alternatives above only produce correct results if the array is a floating-point type (ndimage fails to properly promote the type of the output array to contain the values it computes; in particular, derivatives can have negative values, so the partial derivative images must be of a signed type to make sense).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1