Using Matlab, smooth an RGB image using a 15×15 kernel
Solution
Please find required MATLAB code along with necessary details incomments:
clear all; clc
format short
filename=’peppers.png’;
outputfile=’processed_image.tif’;
% read input image file
I=double((imread(filename)));
N=15;
% call the filetering function
img= apply_filter(I,N);
% display the result
subplot(1,2,1)
imshow(uint8(I));
title(‘Original Image’);
subplot(1,2,2)
imshow(uint8(img));
title(‘Smoothed Image’);
% write output image to file
imwrite(img,outputfile)
——————————————————–apply_filter.m
function img= apply_filter(I,N)
I=double(I);
n=N*N;
% Generate a random filter whose samples sums up to 01
filter_val=(diff([0,sort(randi([0,max(size(I))-n],1,n-1)),max(size(I))-n])+ones(1,n))/100;
filter=reshape(filter_val,[N N]);
[m,n,p]=size(I);
iRow=floor(N/2)+1; iCol=floor(N/2)+1;
% apply the filter to image
for k=1:3
I1=I(:,:,k);
for iRow=floor(N/2)+1:m-(floor(N/2)+1)
for iCol=floor(N/2)+1:n-(floor(N/2)+1)
img(iRow,iCol)=sum(sum(filter.*I1(iRow-floor(N/2):iRow+floor(N/2),iCol-floor(N/2):iCol+floor(N/2))));
end
end
FI(:,:,k)=img;
end
img=uint8(FI);
end
==================== SCREENSHOT OF CODE
======================