From Salt & Pepper Noise to Laplacian Filtering: A Comprehensive Guide
Introduction:
With the use of a mathematical operation, an image can be modified through the process of “filtering.” Image enhancement, noise reduction, edge detection, and feature extraction are just a few of the many uses for image filtering, which is a crucial part of image processing. Three different image filtering methods — the median filter, mean filter, and Laplacian filter — will be covered in this blog article.
Code Explanation:
1. Median Filter:
A non-linear filter called the median filter is used to eliminate noise from images. The median filter’s fundamental principle is to replace every pixel with the median value of its surrounding pixels. When reducing salt-and-pepper noise, which appears as white and black dots in an image, the median filter is extremely helpful.
The “imnoise” function is used to add salt-and-pepper noise to image before applying the median filter. The noised image is next zero-padded. Each pixel of the padded image is iterated over using a loop, and a 3x3 window is made around each pixel. Using the median function, the median of the pixels in the window is determined, and the result is saved in the corresponding pixel of the median-filtered image. Lastly, the “imshow” function is used to display the median-filtered image.
%*******************************median filter******************************
original_img=imread('');
noised_img=imnoise(original_img,'salt & pepper',rand(1));
padded_img=zeros(size(noised_img)+2);
medianfiltered_img=zeros(size(original_img));
for x=1:size(original_img,1)
for y=1:size(original_img,2)
padded_img(x+1,y+1)=original_img(x,y); %Padding the original matrix
end
end
for i=2:(size(padded_img)-1)
for j=2:(size(padded_img)-1)
w=padded_img(i-1:i+1,j-1:j+1);
med=median((w(:)));
medianfiltered_img(i-1,j-1)=round(med);
end
end
2. Mean Filter:
A linear filter called the mean filter is used to eliminate noise from images. The fundamental principle of the mean filter is to substitute each pixel with the average value of its surrounding pixels. Gaussian noise, which manifests as random changes in an image’s brightness, may be effectively removed with the mean filter.
With the” imnoise” function, speckle noise is first added to the original image before applying the mean filter. The zeros function is then used to pad the speckle noised image with zeros. Each pixel of the padded image is iterated over using a loop, and a 3x3 window is made around each pixel. The mean function is used to compute the average of the pixels in the window, and the result is saved in the corresponding pixel of the mean-filtered image. Lastly, the “imshow” function is used to display the mean-filtered image.
%************************meanfilter***************************************
speckle_noised=imnoise(original_img,'speckle',.2); %adding noise
padd_img=zeros(size(speckle_noised)+2);
for x=1:size(original_img,1)
for y=1:size(original_img,2)
padd_img(x+1,y+1)=original_img(x,y);
end
end
meanfilter_img=zeros(size(original_img),'uint8');
for i=2:(size(padd_img)-1)
for j=2:(size(padd_img)-1)
w=padd_img(i-1:i+1,j-1:j+1);
mn=mean((w(:)));
meanfilter_img(i-1,j-1)=ceil(mn);
end
end
3. Laplacian Filter:
A linear filter called the Laplacian is used to sharpen edges in images. The Laplacian filter’s main goal is to draw attention to areas of an image where the intensity changes quickly. When looking for edges in an image, the Laplacian filter is quite helpful.
The Laplacian filter’s code begins by randomly selecting a window size and building a filter kernel with that size. The imfilter function is then used to blur the original image, and the outcome is transformed to double precision. Each pixel of the distorted image is iterated through in a loop, and a 3x3 window is made around each pixel. The Laplacian filter kernel is used to calculate the Laplacian of the pixels in the window, and the resulting value is deducted from the original image. Lastly, the “imshow” function is used to display the normalized image.
%*******************************laplacian filter*************
windowWidth = round(10*rand(1));
kernel = ones(windowWidth) / windowWidth ^ 2;
blurredImage = imfilter(original_img, kernel);
blurredImage=double(blurredImage);
F1=[0 1 0;1 -4 1; 0 1 0];
filteredimage=zeros(size(blurredImage));
for i=1:size(blurredImage,1)-2
for j=1:size(blurredImage,2)-2
filteredimage(i,j)=sum(sum(F1.*blurredImage(i:i+2,j:j+2)));
end
end
normalize_img=original_img-(uint8(filteredimage));
Conclusion:
Three different types of image filtering methods — the median filter, mean filter, and Laplacian filter — were covered in this blog article. The mean filter is a linear filter used to eliminate Gaussian noise, the Laplacian filter is a linear filter used to enhance edges in an image, and the median filter is a non-linear filter used to remove salt-and-pepper noise. Each filter’s code was thoroughly examined, along with its uses. We can improve the quality of images and gain relevant information from them by applying these filtering techniques.