How to calculate a Laplacian mask or any size [duplicate]
Asked Answered
B

2

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.

Baxley answered 17/10, 2013 at 8:46 Comment(3)
Looks to me as simple as n^2 -1 where your matrix is nxnScandura
That 5x5 matrix is not a discrete approximation to the Laplacian. It might have similar properties, but it is not a Laplacian.Walkway
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.Marjorie
S
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

Scandura answered 17/10, 2013 at 8:50 Comment(3)
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.Baxley
@Baxley - 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.Launalaunce
This does not produce a discrete approximation to the Laplacian operator.Walkway
P
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

Propositus answered 17/10, 2013 at 9:17 Comment(2)
No, LoG is not the Laplacian function, that is a 'Laplacian of Gaussian' = Gaussian smoothing with the Laplacian, see your link...Lacy
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.Walkway
S
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

Scandura answered 17/10, 2013 at 8:50 Comment(3)
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.Baxley
@Baxley - 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.Launalaunce
This does not produce a discrete approximation to the Laplacian operator.Walkway

© 2022 - 2024 — McMap. All rights reserved.