Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Laplacian mask/kernel used in the scipy.ndimage.filter.laplace()?

A simple horizontal/vertical Laplace mask has 4 in the center of the kernel (left side of the figure). Similarly, a Laplace mask sensitive to diagonal features has 8 in the center of the kernel (right side in the figure bellow). What mask is scipy using, and can I choose which one to use?

enter image description here

like image 341
icypy Avatar asked Sep 24 '15 18:09

icypy


1 Answers

A simple check would be to declare a 2D array of zeroes except for one coefficient in the centre which is set to 1, then apply the laplace function to it. A property with filtering is that if you submit an image with a single 1, the output would be the actual filter itself centered at the location of where the 1 is - look up impulse response... or more specifically, the Point Spread Function.

If you do this, then you'll see what it looks like after you run through the laplace method:

In [13]: import numpy as np

In [14]: import scipy.ndimage.filters

In [15]: A = np.zeros((5,5))

In [16]: A[2,2] = 1

In [17]: B = scipy.ndimage.filters.laplace(A)

In [18]: A
Out[18]:
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

In [19]: B
Out[19]:
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1., -4.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

As such, it's the first kernel that is being used, but note the sign change. The centre coefficient is positive while the others are negative.


However, if you really want to know what's going on underneath the hood, check out the docs on the function: http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.filters.laplace.html - There's a link to the source of where the function is defined:

https://github.com/scipy/scipy/blob/v0.16.0/scipy/ndimage/filters.py#L396

The relevant code you need to look at is here:

def derivative2(input, axis, output, mode, cval):
    return correlate1d(input, [1, -2, 1], axis, output, mode, cval, 0)
return generic_laplace(input, derivative2, output, mode, cval)

Basically, a 1D kernel of [1, -2, 1] is being applied to each dimension independently as done by the correlate1d function... so the rows first, followed by the columns. This in effect computes the first mask that you see in your question.

like image 198
rayryeng Avatar answered Nov 01 '22 13:11

rayryeng