how is Laplacian filter calculated?
Asked Answered
F

2

8

I don't really follow how they came up with the derivative equation. Could somebody please explain in some details or even a link to somewhere with sufficient math explanation?

image

Laplacian filter looks like

enter image description here

Fiester answered 29/11, 2018 at 17:57 Comment(2)
2nd row is for x-derivative and 2nd column is y-derivative, so when you add both of them then it becomes above matrix i.e. f(x+1,y) + f(x-1,y) -2f(x,y) + f(x,y+1) + f(x,y-1) - 2f(x,y)Favianus
I know that, my question is why this represents after all the second derivative of Laplacian operator?Fiester
S
21

Monsieur Laplace came up with this equation. This is simply the definition of the Laplace operator: the sum of second order derivatives (you can also see it as the trace of the Hessian matrix).

The second equation you show is the finite difference approximation to a second derivative. It is the simplest approximation you can make for discrete (sampled) data. The derivative is defined as the slope (equation from Wikipedia):

Equation of derivative

In a discrete grid, the smallest h is 1. Thus the derivative is f(x+1)-f(x). This derivative, because it uses the pixel at x and the one to the right, introduces a half-pixel shift (i.e. you compute the slope in between these two pixels). To get to the 2nd order derivative, simply compute the derivative on the result of the derivative:

f'(x) = f(x+1) - f(x)
f'(x+1) = f(x+2) - f(x+1)

f"(x) = f'(x+1) - f'(x)
      = f(x+2) - f(x+1) - f(x+1) + f(x)
      = f(x+2) - 2*f(x+1) + f(x)

Because each derivative introduces a half-pixel shift, the 2nd order derivative ends up with a 1-pixel shift. So we can shift the output left by one pixel, leading to no bias. This leads to the sequence f(x+1)-2*f(x)+f(x-1).

Computing this 2nd order derivative is the same as convolving with a filter [1,-2,1].

Applying this filter, and also its transposed, and adding the results, is equivalent to convolving with the kernel

[ 0, 1, 0       [ 0, 0, 0       [ 0, 1, 0
  1,-4, 1    =    1,-2, 1    +    0,-2, 0
  0, 1, 0 ]       0, 0, 0 ]       0, 1, 0 ] 
Sheetfed answered 29/11, 2018 at 18:33 Comment(7)
thanks. Could you please provide from where you get the equation?Fiester
So going through wikipedia pages it seems that they are taking the "Second-order central" which explains the + and - signs inside the function!Fiester
@wisdom: h is the step size. It is set to 1 in the discrete case. The derivative is with respect to x. The first order discrete derivative introduces a 1/2-pixel shift right, therefore the second first-order derivative is chosen with a one pixel shift left, leading to a 2nd order derivative without shift. I'll add some text to the answer to explain this.Sheetfed
@CrisLuengo, would you please extend your solution to the 5x5 kernel?Ulibarri
@h.nodehi What is "the 5x5 kernel"? The Laplacian is typically implemented as this 3x3 kernel, or another 3x3 kernel with -8 in the middle, or as the Laplacian of Gaussian, which should be quite a bit larger to be meaningful.Sheetfed
@CrisLuengo My mistake! Yes! I mean the discrete Laplacian of Gaussian kernels. I need to know how the discrete kernels are calculated, or at least what is the central element's value for each size (order). E.g., the central element for a 9x9 LoG kernel is -40. I know that the kernels are not unique, but I need to know how to calculate the common kernels.Ulibarri
@h.nodehi "the central element for a 9x9 LoG kernel is -40" This depends on what the sigma is, what arbitrary scaling is applied to allow for quantization, and how it's quantized. If I make a 9x9 LoG in my software (sigma=1), then the central element has a value of -0.3186. I suggest you write a new question, if it hasn't been asked before. Here is some more information about computing the LoG: https://mcmap.net/q/1322864/-a-faster-approach-to-laplacian-of-gaussianSheetfed
L
0

This is calculated using the Taylor Serie applied up to the second term as follows. You can calculate an approximation of a function anywhere 'near' a with a precision that will increase as more terms are added. Since the expression contains the second derivative, with a trick we can derive an expression that calculates the second derivative in a from the function values 'near' a. Which is what we want to do with the image.

(1) Let's develop Taylor series to approximate f(a+h) where h is an arbitrary value,

f(a+h)=f(a)+f'(a)h + (f''(a)h^2)/2 

(2) Now same development to approximate f(a-h)

f(a-h)= f(a)+f'(a)(-h) + (f''(a)(-h)^2)/2 

add those 2 expressions, this will remove the f'(a) , leading to

f(a+h) + f(a-h) = 2*f(a) +  (f''(a)h^2)

reorder the terms

f''(a) = [f(a+h) + f(a-h) -2*f(a)] / h^2

This generic formula is also valid when h=1 ( 1 pixel far from a )

f''(a) = f(a+1) + f(a-1) -2*f(a) (*1)

Since Laplacian is the derivative of 2 functions, you can approximate it as a sum of 2 partial derivative approximations

Let's study the X-axis. A kernel is meant to be used using the convolution operator. looking at the equation (*1) carefully

f''(a) = f(a+1) *1 + f(a-1) *1 + *f(a)*-2 (*1)

this is the expression of the convolution of image [f(a-1) f(a) f(a+1)] by kernel [1 -2 1]

The same thing occurs along the Y axis. So rewriting using (x,y) coordinates.

f(a,b) = [f(a-1,b) f(a,b) f(a+1,b)] X [1 -2 1] + [f(a,b-1) f(a,b) f(a,b+1)]
X [1 -2 1]

Where X is the CONVOLUTION operator, NOT is the matrix product operator.

which is the product of image of size 3*3 centered on f(a,b) by the 2 dimensional kernel {{0,1,0},{1,-4,1},{0,1,0}}

Limbert answered 27/12, 2023 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.