-1
$\begingroup$

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

For input:

lena image with 3x3 mean filter

I get output:

my Laplacian filter implementation

I used the mask

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);
[mask_rows, mask_columns] = size(M);

for i = 1+(mask_rows-1)/2:(rows - (mask_rows-1)/2)
    for j = 1+(mask_columns-1)/2:(columns - (mask_columns-1)/2)
        tmpMat = I(i-(mask_rows-1)/2:i+(mask_rows-1)/2,j-(mask_columns-1)/2:j+(mask_columns-1)/2);
        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?

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

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

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

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?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.