COMSATS University Islamabad
Attock Campus
Department of Electrical & Computer Engineering
Program: BCE-8 spring 2021
Lab Sessional I
Course: Digital Image Processing Lab Instructor: Engr. Ayesha Sadiq
Time Allowed: 1hour 15 minutes Marks: 30
Name: Arslan Arif Reg. No: FA17-BCE-017
Note: Each question carries 10 marks.
Instructions:
1. You have to submit your answer document & code file via both MS Teams & email.
2. Save document file with your Name and Registration number.
3. No late answers would be accepted.
Email ID: ayeshasadiq@cuiatk.edu.pk
Question 1: [CLO 1, PLO 1]
(a) Write a generic MATLAB function to calculate the N4 (p), N8 (p), 4-Adjacnecy and 8
adjacency of a pixel [2, 2] entered by the user.
The gray level criteria for adjacency of a pixel is V= {1, 2}
Code:
function [adjacent] = sessional(im,neighborhood,
pixel_loc,graylevelcriteria)
x=pixel_loc(1,1);
y=pixel_loc(1,2);
[r c]=size(im);
pixel_gray_level=im(x,y);
[px py]=find(graylevelcriteria==pixel_gray_level);
if (neighborhood==4)
x1=x-1;
x2=x+1;
y1=y-1;
y2=y+1;
if ((x<=0 || x>r) || (y<=0 || y>c))
adjacent='Accessing Neighborhood not part of an image';
else
neighbors=[im(x1,y) im(x2,y) im(x,y1) im(x,y2) ];
adjacent=0;
for i=1:4
[px py]=find(graylevelcriteria==neighbors(:,i));
if (length(px)>0)
adjacent(:,i)=1;
else
adjacent(:,i)=0;
end
end
end
else if (length(px)<=0)
adjacent='Pixel is not in Specified Criteria';
end
end
if (neighborhood==8)
x1=x-1;
x2=x+1;
y1=y-1;
y2=y+1;
if ((x<=0 || x>r) || (y<=0 || y>c))
adjacent='Accessing Neighborhood not part of an image';
else
neighbors=[im(x1,y) im(x2,y) im(x,y1) im(x,y2) im(x1,y1) im(x2,y2)
im(x1,y2) im(x2,y1)];
adjacent=0;
for i=1:8
[px py]=find(graylevelcriteria==neighbors(:,i));
if (length(px)>0)
adjacent(:,i)=1;
else
adjacent(:,i)=0;
end
end
end
end
end
Main Code:
clc; %clearing commands
clear all;
close all;
A=[0 1 1; 0 2 0; 0 0 1] %taking matrix A
V=[1,2] % taking row vector
in=input('Enter the choice(4 or 8); N4, N8 : '); %enter user choice
N=sessional(A,in,[2,2],V) %calling function
Output:
Question 2: [CLO 2, PLO 2 ]
Write a function in MATLAB for the intensity level slicing of an Image.
Code:
a=imread('download.JPG');
b=rgb2gray(a);
a1=double(b);
imshow(b)
[m n]=size(a1);
x=120;
y=180;
for i= 1:m
for j= 1:n
if (a1(i,j)>=x & a1(i,j)<=y)
b(i,j)=255;
else
b(i,j)=0;
end
end
end
figure
imshow(b);
imshow(uint8(b));
Output:
Question 3: [CLO 3, PLO 3]
Generate a matrix of 200x200 pixels using “ones function and apply the following
transformations.
f ( x , y )=f ( x , y )∗(−1 )x + y
Where x and y is the index of row and column respectively and show the results.
Code:
clc
clear all
close all
x=ones(200,200);
for i=1:200
for j=1:200
tform(i,j)=x(i,j)*(-1)^(i*j);
end
end