Basic Filters Implementation in Matlab

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

1: Write a Matlab function to perform spatial filtering on an image using both convolution and

correlation methods. In order to perform these operations, pad your image with 10 rows and
columns at the border of the image, the value of the padding should be 0. Apply an averaging
filter with a kernel size of 7x7 and display the image before and after applying the filter.
Code:
P = imread('C:\Users\pc\Desktop\zain.jfif');

P1 = padarray(P,[1,1],0)

kernel=(1/49)*ones(7);

convImg=imfilter(P1,kernel,'conv');

corrImg=imfilter(P1,kernel,'corr');

subplot(2,3,4); imshow(P1); title('Original')

subplot(2,3,5); imshow(convImg,[]); title(' convolution filter ')

subplot(2,3,6); imshow(corrImg,[]); title(' Correlation filter ')

Output:

2. Apply median filter to the smoothed image (obtained in 1) and display the results.
Code:

P1=rgb2gray(P1)
m=medfilt2(P1,[5 5]);
subplot(1,3,1); imshow(P1); title('Original')
subplot(1,3,2); imshow(m,[]); title('Median Filter Applied')
Original image in gray then median Filter is Applied.

3. Write a MATLAB code to implement the emboss effect in an image of your choice.
Code:
[l w r]=size(P);
new_p=zeros(l,w,r);
kernal=[-1 -1 -1 -1 0;-1 -1 -1 0 1;-1 -1 0 1 1;-1 0 1 1 1;0 1 1 1 1];
%kernal=[-1 -1 0;-1 0 1;0 1 1];
kernal=int16(kernal);
k_size=5;
lw=floor(k_size/2);
imshow(P);
for i=lw+1:1:l-lw
for j=lw+1:1:w-lw
C=P(i-lw:i+lw,j-lw:j+lw,:);
C=int16(C);
C1=C(:,:,1).*kernal;
C2=C(:,:,2).*kernal;
C3=C(:,:,3).*kernal;
new_p(i,j,1)=sum(sum(C1))+128;
new_(i,j,2)=sum(sum(C2))+128;
new_p(i,j,3)=sum(sum(C3))+128;
end
end
new_p=uint8(new_p);
figure;
imshow(new_p);
Output:

Original Image Emboss Filtered Image

You might also like