Vous êtes sur la page 1sur 8

Lab Report 1

DIGITAL IMAGE PROCESSING








Submitted By: Muhammad Shan Anwer Malik
Registration No: SP11-BCE-034
Submitted To: Sir Faisal Najeeb


Objective:
By the end of this lab students will be able to know the basics of digital
images in matlab and the arithmetic operations on sequences which can be
performed on digital images.
In-Lab Tasks:

Task-1:
Make a function that takes the square matrix as input and exchange its
diagonal entry
with the center row of matrix and the center row of matrix as it diagonal.
Consider a
case of a matrix of 5*5 whose center row is the 3
rd
row and entries in the
diagonal are
1,7,13,19,25.


Original matrix=



Replace the center row with entries in the diagonal and replace the entries in
the center
row with the entries in the diagonal.
Solution:

Matlab code:

function [ abc ] = myfunc( a )
b=a(3,:);
x=1;
for j=1:5
for k=1:5
if j==k
temp=a(j,k);
a(j,k)=b(1,x);
a(3,x)=temp;
x=x+1;
end
end
end
abc=a;

end
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Result:

original

o =

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

swapped

o =

11 2 3 4 5
6 12 8 9 10
1 7 13 19 25
16 17 18 14 20
21 22 23 24 15

>>

Task-2:
Create, perhaps using for-loops, a synthetic image that has a 1 in the
(1,1) location, and a 255 in the (128,128) location, and i + j - 1 in the i, j
location. This we'll refer to as the diagonal gray'' image. Can you
manage to do this without using for-loops?
Display the image using (well assume you placed your image in a matrix
named a) image(a); colormap(gray). (Dont worry if this doesnt work
exactly the way you expect. Colormaps can be tricky!)

Solution:

Matlab code:

for i=1:128
for j=1:128
if (i==1)&&(j==1)
a(i,j)=1;
elseif (i==128)&&(j==128)
a(i,j)=255;
else
a(i,j)=i+j-1;
end
end
end
colormap(gray)
image(a)






Result:






20 40 60 80 100 120
20
40
60
80
100
120
Post-Lab Tasks: Time
Allowed:- 1 week

Task-1:

Make a square image of size= 256 * 256. Use your Roll number or Registration
number to fill it diagonally with black pixels and white pixels.

Solution:

Matlab code:
clc;
clear all;
close all;
loop=1;
for i=1:256
loop=loop+i-1;
while loop<=256
for j=loop:loop+9
if loop<=256
I(i,j)=0;
end
end
loop=loop+9;
for k=loop:loop+49
if loop<=256
I(i,k)=1;
end
end
loop=loop+49;
end
loop=1;
end
%*******************************************%
for i=1:256
loop=loop+i-1;
while loop<=256
for k=loop:loop+49
if loop<=256
I(k,i)=1;
end
end
loop=loop+49;
for j=loop:loop+9
if loop<=256
I(j,i)=0;
end
end
loop=loop+9;
end
loop=1;
end
imagesc(I);
axis([1 256 1 256]);
colormap(gray);

Result:
















Task-2:

Write an m function with following specifications:

function H = imcircle(R, M, N)
imcircle generates a circle (inside the rectangle) of radius R centered on a
rectangle of height M and width N. H is a binary image with 1s on the circle and
0s elsewhere. R must be an integer >= 1.inside a rectangle.
Your program must check the validity of R and also it should check to make sure
that the specified circle fits in the given rectangle dimensions.

Hint: Review functions: meshgrid and floor, and equation of circle i.e x
2
+ y
2
= r
2.

Solution:

Matlab code:

function:
function H=imcircle(R,M,N); %where R=radius of circle, M=height of rectangle,
N=width of rectangle

if R<=M && R<=N
for i=1:N
for j=1:M
if norm([(N/2)-i (M/2)-j])<=R
H(i,j)=1;
else
H(i,j)=0;
end
end
end
else
display('Wrong');
end
end

mfile:

clc;
clear all;
close all;
M=80;
N=50;
R=10;
H=imcircle(R,M,N);
imagesc(H);
colormap(gray);




Result:

Vous aimerez peut-être aussi