# Matlab Laplacian filter implementation

I implemented a Laplacian filter for the Lena image, but I get an unexpected output.

For input:

I get output:

M =

0    -1     0
-1     5    -1
0    -1     0

I is my lena image rows and columns are I.rows and I.columns

And this is my Matlab function:

function IM = customMaskImage(I, rows, columns, M)
IM = zeros(rows,columns);

IM(i,j) = sum(sum(customMatConv(tmpMat, 3, 3, M)));
end
end

end

customMatConv.m is:

function IM = customMatConv(I, rows, columns, M)
IM = zeros(rows,columns);

for i = 1:rows
for j = 1:columns
IM(i,j) = I(i,j) * M(i,j);
end
end

end

What am I doing wrong?

• What does the function customMatConv do ? – itismeghasyam Oct 21 '17 at 16:56
• Sorry for that customMatConv does same as A.*B which means customMatConv(A,B, A.rows, A.columns) = A.*B – user31485 Oct 21 '17 at 17:02

You should use conv2() or imfilter() with your filter.
Unless you want to implement it by hand,

• It is my homework so, I should not use Matlab functions without undestanding it's inner workings. – user31485 Oct 21 '17 at 22:12

The first wrong doing is the $5$in the middle of the kernel:

0    -1     0
-1     5    -1
0    -1     0

A second derivative "cannot" have its coefficients sum to a non-zero value. The two standard $3\times 3$ Laplacians are:

0    -1     0
-1     4    -1
0    -1     0

and

-1    -1     -1
-1     8    -1
-1    -1     -1

The second issue could be a format one: is your input cast to single or double float, as integer operations may be troublesome?