Implement Laplacian of Gaussian Filter

2k Views Asked by At

I want to implement the laplacian of gaussian filter for my image.I test this 2 method which give me completely different answer. Please tell me which I made mistake.In the first method I implement the LOG filter from it's function and in the second I use opencv functions. Here is my code for the first method:

img = np.array(Image.open('Bi00069.bmp').convert('I;16')) 
ddepth = cv2.CV_16U
kernel_size = 3 
def gkern(l,sig):

    ax = np.linspace(-(l - 1) / 2., (l - 1) / 2., l)
    xx, yy = np.meshgrid(ax, ax)
    kernel = (-1)/(np.pi*sig**4)*(1-(xx**2+yy**2)/(2*sig**2))* 
    np.exp(-0.5 * (np.square(xx) + np.square(yy)) / np.square(sig))

    return kernel / np.sum(kernel)
img1 = signal.convolve2d(img,gkern(5,5),mode='same', 
boundary='fill', fillvalue=0)

plt.imshow(G_img2,cmap='gray')

and my result is: enter image description here

and the second implementation is:

src = cv2.GaussianBlur(img, (3, 3), 5,cv2.BORDER_DEFAULT)
img2 = cv2.Laplacian(src, ddepth, ksize=kernel_size)
plt.imshow(img2,cmap='gray')

and here is my result: enter image description here

I think based on the results the second image should be correct. But I don't know what's wrong with my first implementation.Please help me.

0

There are 0 best solutions below