Search code examples
matlabimage-processingfiltermasklaplacian

How to calculate a Laplacian mask or any size


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.


Solution

  • 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