Open In App

Laplacian of Gaussian Filter in MATLAB

The Laplacian filter is used to detect the edges in the images. But it has a disadvantage over the noisy images. It amplifies the noise in the image. Hence, first, we use a Gaussian filter on the noisy image to smoothen it and then subsequently use the Laplacian filter for edge detection.

Dealing with a noisy image without a Gaussian Filter:

Function used



Example:




% Read the image in MatLab
j=imread("logo.png");
 
% Convert the image in gray scale.
j1=rgb2gray(j);
 
% Generate the noise of size equal to gray image.
n=25*randn(size(j1));
 
% Generate noisy image by adding noise to the grayscale image.
j2=n+double(j1);
 
% Display the original color image.
imtool(j,[]);
 
% Display the gray image.
imtool(j1,[]);
 
% Display the noisy image.
imtool(j2,[]);
 
% Define the Laplacian Filter.
Lap=[0 -1 0; -1 4 -1; 0 -1 0];
 
% Convolve the noisy image with Laplacian filter.
j3=conv2(j2, Lap, 'same');
 
% Display the resultant image.
imtool(abs(j3), []);

Output:  



Figure: Input Noisy image

Figure: Edge detected image

Explanation of Code: 

Dealing with a noisy image using LOG:

Logarithmic transformation of an image is one of the gray-level image transformations. Log transformation of an image means replacing all pixel values, present in the image, with its logarithmic values. Log transformation is used for image enhancement as it expands dark pixels of the image as compared to higher pixel values.

Example:




% Read the image in MatLab
j=imread("logo.png");
 
% Convert the image in gray scale.
j1=rgb2gray(j);
 
% Generate the noise of size equal to gray image.
n=25*randn(size(j1));
 
% Generate noisy image by adding
% noise to the grayscale image.
j2=n+double(j1);
 
% Display the original color image.
imtool(j,[]);
 
% Display the gray image.
imtool(j1,[]);
 
% Display the noisy image.
imtool(j2,[]);
 
% Create the gaussian Filter.
Gaussian=fspecial('gaussian', 5, 1);
 
% Define the Laplacian Filter.
Lap=[0 -1 0; -1 4 -1; 0 -1 0];
 
% Convolve the noisy image
% with Gaussian Filter first.
j4=conv2(j2, Gaussian, 'same');
 
% Convolve the resultant
% image with Laplacian filter.
j5=conv2(j4, Lap, 'same');
 
% Display the Gaussian of noisy_image.
imtool(j4,[]);
 
% Display the Laplacian of
% Gaussian resultant image.
imtool(j5,[]);

 
Output: 

Figure: Input Noisy image

Figure: Edge detected image

Explanation of Code:

 

 


Article Tags :