Jasper Zhou
Jasper Zhou

Reputation: 527

How to get first and second derivative matrix of an image

I am trying to compute the gradient orientation of an image, in the first part, I need to compute the first-order derivative of an image (in both horizontal and vertical direction), so I applied Gaussian filter in scipy module in order to get it, but I got an error "AttributeError: 'int' object has no attribute 'shape'"

I am using python version 3.7.0 and the opencv version is 3.4.2.

The function documentation is here:scipy.ndimage.filters.gaussian_filter

g_x = np.zeros(image_new.shape) 
ndimage.filters.gaussian_filter(image_new, 2*np.sqrt(2), (0,1), 1 ,g_x )

Is this correct? or how to compute the first derivative(and second derivative) of an image.

Upvotes: 3

Views: 4246

Answers (1)

ravikt
ravikt

Reputation: 1058

To get the first derivative of the image, you can apply gaussian filter in scipy as follows.

from scipy.ndimage import gaussian_filter, laplace

image_first_derivative = gaussian_filer(image, sigma=3)

If sigma is a single number, then derrivative will calculated in all directions. To specify the direction pass the sigma as sequence.

x-direction with sigma = (11, 0)

Above is the first derivative of the image taken in x-direction with sigma=(11,0). The below image is derivative taken in y-direction with sigma=(0, 11)

y-direction with sigma = (0, 11) you can choose the value of sigma accordingly. For calculating second derivative laplacian operator can be used.

image_sec_derivative = laplace(image)

Upvotes: 4

Related Questions