Python implementation of the laplacian of gaussian edge detection

Shan picture Shan · Feb 26, 2014 · Viewed 19.7k times · Source

I am looking for the equivalent implementation of the laplacian of gaussian edge detection.

In matlab we use the following function

   [BW,threshold] = edge(I,'log',...)

In python there exist a function for calculating the laplacian of gaussian. It is not giving the edges back definitely.

  scipy.ndimage.filters.gaussian_laplace

Any pointer to online implementation or the code

Thanks

Answer

ycyeh picture ycyeh · Feb 28, 2014

What matlab edge() do should be

  1. Compute LoG
  2. Compute zero crossings on LoG
  3. Compute a threshold for local LoG difference
  4. Edge pixels = zero crossing && local difference > threshold

The LoG filter of scipy only does step 1 above. I implemented the following snippet to mimic step 2~4 above:

import scipy as sp
import numpy as np
import scipy.ndimage as nd
import matplotlib.pyplot as plt
from skimage import data    

# lena = sp.misc.lena() this function was deprecated in version 0.17
img = data.camera()  # use a standard image from skimage instead
LoG = nd.gaussian_laplace(img , 2)
thres = np.absolute(LoG).mean() * 0.75
output = sp.zeros(LoG.shape)
w = output.shape[1]
h = output.shape[0]

for y in range(1, h - 1):
    for x in range(1, w - 1):
        patch = LoG[y-1:y+2, x-1:x+2]
        p = LoG[y, x]
        maxP = patch.max()
        minP = patch.min()
        if (p > 0):
            zeroCross = True if minP < 0 else False
        else:
            zeroCross = True if maxP > 0 else False
        if ((maxP - minP) > thres) and zeroCross:
            output[y, x] = 1

plt.imshow(output)
plt.show()

This of course is slow and probably not idiomatic as I am also new to Python, but should show the idea. Any suggestion on how to improve it is also welcomed.