0% found this document useful (0 votes)
39 views8 pages

Digital Image Processing Assignment #02

The document provides code implementations of image filtering algorithms including Roberts, Prewitt, Sobel, and Canny filters in MATLAB. For each filter, the code calculates the filtered output image by performing convolution of the image with the respective filter kernel without using built-in functions. Sample output images showing the filtered result and original are displayed for comparison.

Uploaded by

Sarah Latif Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Digital Image Processing Assignment #02

The document provides code implementations of image filtering algorithms including Roberts, Prewitt, Sobel, and Canny filters in MATLAB. For each filter, the code calculates the filtered output image by performing convolution of the image with the respective filter kernel without using built-in functions. Sample output images showing the filtered result and original are displayed for comparison.

Uploaded by

Sarah Latif Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Digital Image Processing

Assignment #02
Task:
Implement the following filters in MATLAB without using MATLAB build-in functions:
Canny
Sobel
Roberts
Prewitt

Robert Filter:

clc;
clear
clc
close all
a=imread('C:\Users\user\Desktop\Matlab\came
raman.tif');
b=im2double(a);
[m,n]=size(a);
%ROBERT
L(1:m,1:n)=0;
for i=1:m-2
for j=1:m-2
L(i,j)=-1*b(i,j)+0+0+1*b(i+1,j+1);
end
end
M(1:m,1:n)=0;
for i=1:m-2
for j=1:m-2
M(i,j)=0-1*b(i,j+1)+1*b(i+1,j)+0;
end
end
figure;
subplot(2,2,1)
imshow(L)
title('Robert Gx');
subplot(2,2,2)
imshow(M)
title('Robert Gy');
N=M+L;
subplot(2,2,3)
imshow(N)
title('Robert Gx+Gy');
subplot(2,2,4)
imshow(b)
title('Original Image');

Output:
Prewit Filter:

clc;
clear
clc
close all
original =imread('C:\Users\user\Desktop\Matlab\cameraman.tif');
dblimg=im2double(original);
[m,n]=size(original);
%PREWIT
N(1:m,1:n)=0;
for i=1:m-2
for j=1:m-2
N(i,j)=-1*dblimg(i,j)-1*dblimg(i,j+1)-
1*dblimg(i,j+2)+0+0+0+1*dblimg(i+2,j)
+1*dblimg(i+2,j+1)+1*dblimg(i+2,j+2);
end
end
O(1:m,1:n)=0;
for i=1:m-2
for j=1:m-2
O(i,j)=-1*dblimg(i,j)+0+1*dblimg(i,j+2)-1*dblimg(i+2,j)
+0+1*dblimg(i+1,j+2)-1*dblimg(i+2,j)+0+1*dblimg(i+2,j+2);
end
end
figure;
subplot(2,2,1)
imshow(N)
title('Prewit Gx');
subplot(2,2,2)
imshow(O)
title('Prewit Gy');
Z=N+O;
subplot(2,2,3)
imshow(Z)
title('Prewit Gx+Gy');
subplot(2,2,4)
imshow(dblimg)
title('Original Image');

Output:
Sobel Filter:

clear
clc
close all
original =imread('C:\Users\user\Desktop\Matlab\cameraman.tif');
dblImg=im2double(original);
[m,n]=size(original);
P(1:m,1:n)=0;
for i=1:m-2
for j=1:m-2
P(i,j)=-1*dblImg(i,j)-2*dblImg(i,j+1)-
1*dblImg(i,j+2)+0+0+0+1*dblImg(i+2,j)
+2*dblImg(i+2,j+1)+1*dblImg(i+2,j+2);
end
end
R(1:m,1:n)=0;
for i=1:m-2
for j=1:m-2
R(i,j)=-1*dblImg(i,j)+0+1*dblImg(i,j+2)-2*dblImg(i+1,j)
+0+2*dblImg(i+1,j+2)-1*dblImg(i+2,j)+0+1*dblImg(i+2,j+2);
end
end
figure;
subplot(2,2,1)
imshow(P)
title('Sobel Gx');
subplot(2,2,2)
imshow(R)
title('Sobel Gy');
Y=P+R;
subplot(2,2,3)
imshow(Y)
title('Soble Gx+Gy');
subplot(2,2,4)
imshow(dblImg)
title('Original Image');

Result:
Canny Filter:

clear all
close all
clc
img = imread('C:\Users\user\Desktop\Matlab\cameraman.tif');
I=double(img); %read image
In=I;
mask1=[1, 0, -1;1, 0, -1;1, 0, -1];
mask2=[1, 1, 1;0, 0, 0;-1, -1, -1];
mask3=[0, -1, -1;1, 0, -1;1, 1, 0];
mask4=[1, 1, 0;1, 0, -1;0, -1, -1];

mask1=flipud(mask1);
mask1=fliplr(mask1);
mask2=flipud(mask2);
mask2=fliplr(mask2);
mask3=flipud(mask3);
mask3=fliplr(mask3);
mask4=flipud(mask4);
mask4=fliplr(mask4);

for i=2:size(I, 1)-1


for j=2:size(I, 2)-1
neighbour_matrix1=mask1.*In(i-1:i+1, j-1:j+1);
avg_value1=sum(neighbour_matrix1(:));

neighbour_matrix2=mask2.*In(i-1:i+1, j-1:j+1);
avg_value2=sum(neighbour_matrix2(:));

neighbour_matrix3=mask3.*In(i-1:i+1, j-1:j+1);
avg_value3=sum(neighbour_matrix3(:));

neighbour_matrix4=mask4.*In(i-1:i+1, j-1:j+1);
avg_value4=sum(neighbour_matrix4(:));

%using max function for detection of final edges


I(i, j)=max([avg_value1, avg_value2, avg_value3,
avg_value4]);

end
end
% figure, imshow(uint8(I));
figure;
subplot(1,2,1)
imshow(uint8(I))
title('Canny Filter');
subplot(1,2,2)
imshow(img)
title('Original Image');

Result:

You might also like