8

I would like to know how to calculate a Laplacian mask of an arbitrary odd size kernel (2nd derivative). For example, I know a 3x3 would be:


1  1  1
1 -8  1
1  1  1

And a 5x5 mask would be:


1  1  1  1  1
1  1  1  1  1
1  1 -24 1  1
1  1  1  1  1
1  1  1  1  1

However, this is all I know. I don't know how these were calculated. I believe that all 2nd derivative masks always have a different center number surrounded by 1s. My question is, how would I calculate the center number for nxn where n is odd? (e.g. 7x7, 15x15, etc.) I am planning on implementing this in Matlab. I appreciate any help I can get.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
terry
  • 83
  • 1
  • 1
  • 4
  • Looks to me as simple as `n^2 -1` where your matrix is nxn – Dan Oct 17 '13 at 08:48
  • That 5x5 matrix is **not** a discrete approximation to the Laplacian. It might have similar properties, but it is not a Laplacian. – Cris Luengo Sep 11 '19 at 13:09
  • you better ask this kind of question on math websites rather than here, laplacian is calculated based on the Taylor series expansion. you calculate for distances dx of more than 1, if you want wider kernels. – leo Dec 30 '19 at 19:54

2 Answers2

8

The Laplacian function looks like this:

Source: homeepages.inf.ed.ac.uk/rbf/HIPR2/log.htm

and is described by:

enter image description here

σ here determines the spread of the inverted bell. The digital mask is a discrete approximation of this function. And therefore for smaller values of window size (n) and σ, you get a large negative number surrounded by 1s all over. But as you increase the window size and σ, that's not going to be the case.

To calculate the digital mask correctly, you should use the function given above. The center pixel of your odd sized square (nxn) will be your origin.

For reference: http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm

Papouh
  • 504
  • 1
  • 3
  • 12
  • 7
    No, LoG is not the Laplacian function, that is a 'Laplacian of Gaussian' = Gaussian smoothing with the Laplacian, see your link... – Tobias Apr 27 '16 at 11:23
  • 1
    This is indeed the Laplace of Gaussian, not a discrete approximation to the Laplacian. However, if you need a Laplacian with a larger support, then this is the correct way to do that. – Cris Luengo Sep 11 '19 at 13:11
5

Here's a simple way:

function mask = LapMask(n)
    mask = ones(n);
    mask(ceil((n^2)/2)) = 1 - n^2;
end

I'll leave it to you to add the error checking making certain that n is odd

Dan
  • 45,079
  • 17
  • 88
  • 157
  • Hey, thanks a lot! I can't believe it's that simple. I thought I had to use a large, complicated formula or use repeated convolution on vectors/matrices. – terry Oct 17 '13 at 19:04
  • 3
    @terry - It may still be that you do need to use a different formula. This is just one discrete approximation to the 2D Laplacian, but it is what you asked for. For example `fspecial('laplacian',alpha)` gives different results depending on what value of alpha you specify. – chappjc Oct 17 '13 at 19:33
  • This does **not** produce a discrete approximation to the Laplacian operator. – Cris Luengo Sep 11 '19 at 13:10