You are on page 1of 3

Exercise 6

1/ Write the function to calculate the Laplacian of Gaussian (LoG) kernel as follows:

In 2-D, an isotropic (i.e., circularly symmetric) Gaussian has the form:

The Laplacian L(x,y) of an image with pixel intensity values I(x,y) is given by:

(each x and y derivate )

LoG(x,y) = ∆ 2 S=∆2 ( G ( x , y ) ) × Image

( ( )
− x 2− y2 '

∆2(G ( x , y ) )=
1 −x− y
)
2

2 2
e
πσ 2σ

(( ) ( ) )
−x 2− y 2 2 2 −x 2− y 2
1 −1 2σ
2 −2 x −2 y 2σ
2

¿ 2
e + e
πσ σ2 4 σ2

( )
2 2
2 2 −x −y
−1 x +y 2σ
2

¿ 4
1− 2
e =log (x , y )
πσ 2σ
Matlab code:
For Gaussian filter:

For Laplacian filter:

2/ Generate a LoG kernel with Gaussian =1.0, using a size of 77. Apply this kernel to an
image (using convolution) and show the result (the original and filtered images).
7x7 LoG Mask:
1 2 3 4 5 6 7

1 0 0 1 1 1 0 0
2 0 1 3 3 3 1 0
3 1 3 0 -7 0 3 1
4 1 3 -7 -24 -7 3 1
5 1 3 0 -7 0 3 1
6 0 1 3 3 3 1 0
7 0 0 1 1 1 0 0
Matlab Code:
clc;
clear all;
close all;

image = imread('Coff.jpg');
I = rgb2gray(image);

% Making a gaussian Kernel


sigma = 1; %standard deviation of the
distribution
kernel = zeros(7,7); % for a 7x7 kernel
W = 0; % Sum of elements of kernel
after normalization
for i = 1:7
for j= 1:7
sq_dis = (i-4)^2 + (j-4)^2; %Square
distance
kernel(i,j) = exp (-1*(sq_dis)/(2*(sigma)^2));
W = W + kernel(i,j);
end
end
kernel = kernel/W;

% Apply the filter to the image


[m,n]=size(I);
output = zeros(m,n);
Im = padarray(I,[3 3]);

for i=1:m
for j=1:n
temp = Im(i:i+6 , j:j+6);
temp = double(temp);
conv = temp.*kernel;
output(i,j) = sum(conv(:));
end
end

output = uint8(output);

Mask = [0 0 1 1 1 0 0; 0 1 3 3 3 1 0; 1 3 0 -7 0 3 1; 1 3
-7 -24 -7 3 1; 1 3 0 -7 0 3 1; 0 1 3 3 3 1 0; 0 0 1 1 1 0
0];
output2 = imfilter(output,Mask);
%Show original and Gaus filtered image and Laplacian
image
figure(1);
set(gcf,'position',get(0,'Screensize'));
subplot(131),imshow(image),title('Origin');
subplot(132),imshow(output),title('After Gaussian
Filter');
subplot(133),imshow(output2),title('After Laplacian
Filter');

Result:

Reference: https://academic.mu.edu/phys/matthysd/web226/Lab02.htm

You might also like