How to calculate a Laplacian mask or any size

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


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