Developing Laplacian Filter and apply it to an image

Greetings
I would like to get some help and advice in knowing how to apply the Laplacian Filter to a particular image, I want to get help in knowing how to apply it by developing an algorithm that would replicate the process, not by using the embedded MATLAB function ('laplacian') into it and having it magically work. I want to know the mathematics and logic behind it of how it works when being applied to an image.
Thanks

 Accepted Answer

It's the mean difference between a pixel and it's neighbors. So you'd convolve with an array [-1 1] in each of the 8 directions. Since each direction is unique, the convolution is separable and you can just add up all the kernels. So you'd get a 3x3 window with 8 -1's all the way around the perimeter, and 8 1'd in the middle. In other words, the 3x3 matrix would have -1 all the way around except a value of 8 in the center:
kernel = -1 * ones(3);
kernel(2,2) = 8; % Now kernel = [-1,-1,-1; -1,8,-1; -1,-1,-1]
output = conv2(double(inputImage), kernel, 'same');

9 Comments

Specifically why did you put have an array of -1s and the number 8 in the middle?
It's the same as if you'd done 8 convolutions with the kernel [-1, 1] oriented/aimed in the 8 directions: 1) north , 2) north east, 3) east, 4) southeast, 5) south, 6) southwest, 7) west, and 8) northwest. You can either convolve eight times with differently pointed kernels and add them together, or you can convolve once with a kernel that points in all directions at once, which is just the sum of the 8 kernels. If you do the latter, you'll have -1 all the way around the perimeter, and 8 in the middle.
If an image was placed under this, what would it look like. Say like a picture of the moon, before the code does its work what should the picture of the moon look like afterwards.
The moon would look like the moon. Afterwards the moon would look like an edge detection image. Mostly 0 with small faint edges. Very harsh and flat looking.
Hi IA,
Im just wondering why the center, the number is 8. Why it is not 6 or 10?
thanks
Because you want the output to be the average (or sum) of gradients in each direction. So essentially that kernel gives you the result as if you had applied each of these gradient windows individually to the image (to produce 8 separate output images) and summed up the resulting images:
-1 0 0
0 1 0 Filter #1
0 0 0
0 -1 0
0 1 0 Filter #2
0 0 0
0 0 -1
0 1 -1 Filter #3
0 0 0
0 0 0
0 1 -1 Filter #4
0 0 0
0 0 0
0 1 0 Filter #5
0 0 -1
0 0 0
0 1 0 Filter #6
0 -1 0
0 0 0
0 1 0 Filter #7
-1 0 0
0 0 0
-1 1 0 Filter #8
0 0 0
So, because convolution is a linear filter, you can either do each separately and add up the resulting output images, OR sum up the kernels and apply the summed kernel to the image just once. They're equivalent. If you sum up all 8 kernels, you will get -1 all the way around and 8 in the middle:
-1 -1 -1
-1 8 -1 Combined Filter
-1 -1 -1
So, in essence, this gives you the average gradient -- the gradient averaged over all directions.
Hi thanks for the answer, anyway is the Filter #3 is correct as
0 0 -1
0 1 -1 Filter #3
0 0 0
?
No, it should be
0 0 -1
0 1 0 Filter #3
0 0 0
Copy and paste error I guess.
ok thanks for your explanation.. it really helps!! :)

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!