Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

INDEX OF THE LAB ASSIGNMENTS

S. No. Date of TITLE OF THE EXPERIMENT Date of Remarks


performing Presenting
the
experiment

Types of Variables.
1.
Display of the graphs in functions.
2.
Read the two images and perform the
3. arithmetic operations on them.
Display the histogram of image and
4. perform histogram equalization.
Compute the correlation coefficient of
5. two matrices of equal size.
Practical LAB: 1

Types of Variables:

MATLAB provides three basic types of variables:


1) Local Variables
2) Global Variables
3) Persistent Variables

Local Variables: Each MATLAB function has its own local


variables. These are separate from those of other functions (except
for nested functions), and from those of the base workspace.
Variables defined in a function do not remain in memory from one
function call to the next, unless they are defined as global or
persistent.
Global Variables: If several functions, and possibly the base
workspace, all declare a particular name as global, then they all
share a single copy of that variable. Any assignment to that
variable, in any function, is available to all the other functions
declaring it global.
Persistent Variables: Characteristics of persistent variables
are
✔ You can declare and use them within M-file functions only.

✔ Only the function in which the variables are declared is allowed


access to it.
✔ MATLAB does not clear them from memory when the function
exits, so their value is retained from one function call to the
next.

Naming Variables:

MATLAB variable names must begin with a letter, which may be


followed by any combination of letters, digits, and underscores. MATLAB
distinguishes between uppercase and lowercase characters, so A and a are
not the same variable.
Although variable names can be of any length, MATLAB uses only
the first N characters of the name, (where N is the number returned by the
function namelengthmax), and ignores the rest. Hence, it is important to
make each variable name unique in the first N characters to enable
MATLAB to distinguish variables.
N = namelengthmax
N=
63

The genvarname function can be useful in creating variable names that are
both valid and unique.

Verifying a Variable Name: You can use the isvarname function to make
sure a name is valid before you use it. isvarname returns 1 if the name is
valid, and 0 otherwise.
isvarname 8th_column
ans =
0 % Not valid - begins with a number
1
Avoid Using Function: Names for VariablesWhen naming a variable, make
sure you are not using a name that is already used as a function name. If you
define a variable with a function name, you won't be able to call that
function until you either remove the variable from memory with the clear
function, or invoke the function using builtin.
For example, if you enter the following command, you will not be
able to use the MATLAB disp function until you clear the variable with clear
disp. disp = 50;

To test whether a proposed variable name is already used as a function name,


use
which -all name
Guidelines to Using Variables: The same guidelines that apply to
MATLAB variables at the command line also apply to variables in M-files:

✔ You do not need to type or declare variables used in M-files (with the
possible exception of designating them as global or persistent).
✔ Before assigning one variable to another, you must be sure that the
variable on the right-hand side of the assignment has a value.
✔ Any operation that assigns a value to a variable creates the variable, if
needed, or overwrites its current value, if it already exists.

List of Predefined Variables:

Loops:
A loop is a way of repeatedly executing a section of code. It is so
important to know how to write them that several common examples of how
they are used will be given here. The two kinds of loops we will use are the
for loop and the while loop. We will look at for loops first, then study while
loops a bit later in the logic section.

For Loop:
The for loop looks like this:

for n=1:N
.
.
.
End

which tells Matlab to start n at 1, then increment it by 1 over and over until it
counts up to N, executing the code between for and end for each new value
of n.
The for statement permits any matrix to be used instead of 1:n. The variable
just consecutively assumes the value of each column of the matrix.
For example,
s = 0;
for c = A
s = s + sum(c);
end
computes the sum of all entries of the matrix A by adding its column sums.

While Loop:
The general form of a while loop is:

while relation

statements

end

The statements will be repeatedly executed as long as the relation remains


true. For example, for a given number a, the following will compute and
display the smallest nonnegative integer n such that 2n >=a:

n = 0;
while 2^n < a
n = n + 1;
end
n
If Logic:
The general form of a simple if statement is:

if relation

statements

end

The statements will be executed only if the relation is true. Multiple


branching is also possible, as is illustrated by

if n < 0
parity = 0;
5
elseif rem(n,2) == 0
parity = 2;
else
parity = 1;
end
In two-way branching the elseif portion would, of course, be omitted.

Basic program in c:

Main program
x = 1;
y = 2;
w = 3;
fprintf (‘In the main program, ‘);
Fprintf (‘x = %g, y = %g, w = %g, \n\n’, x, y, w );
answer = example3 (x, y, w);
fprintf (‘Back in the main program, ‘);
fprintf (‘x = %g, y = %g, w = %g, \n\n’, x, y, w );
function z = example3 (a,b,c)
fprintf (‘Inside function example3./n/);
fprintf (‘Upon entering function example3, ‘);
printf (‘a = %g, b = %g, c = %g./n/n’ , a, b, c);
a = a / 10 ;
b= b / 10 ;
c = c /10 ;
z = a+b+c;
fprintf (‘After performing a few calculations, ‘);
fprintf (‘a = %g, b = %g, c = %g, z = %g\n’ , a, b, c, z);
fprintf (‘Leaving function example 3 \n\n’);
Output:
In the main program, x = 1, y = 2, w = 3
Inside function example 3.
Upon entering function example 3, a = 1 , b= 2, c = 3.
After performing a few calculations, a = 0.1, b = 0.2, c = 0.3, z =
0.6
Leaving function example 3.
Back in the main program, x = 1, y = 2, w = 3.
Practical LAB: 2

Display of the graphs f=cos(2*pi*x) and g= sin(2*pi*x)


where x varies uniformly in interval [0,1]

x=linspace(0,1,50);
f=cos(2*pi*x);
plot(x,f,'+');
g=sin(2*pi*x);
hold on
plot(x,g,'r+')
hold off
title('plotting of "f" and "G" function')
xlabel('X ===>')
ylabel('f & g===> ')
legend('==>f','==>g')

x=linspace(0,1,30);
F=1./(1+x.^2);
plot(x,F,'k+');
G=exp(x.^3);
hold on
plot(x,G,'go')
hold off
title('Plotting of "f" and "G" function ( by Harjas Bakshi)')
xlabel('X ===>')
ylabel('F & G===> ')
legend('==>F','==>G')
Vector which generates the M x N matrix of zeros

>> x=zeros(3,4)

x=

0 0 0 0
0 0 0 0
0 0 0 0

Vector which generates the M x N matrix of ones


>> x=ones(3,4)

x=

1 1 1 1
1 1 1 1
1 1 1 1

Vector which generates the M x N matrix of uniformly distributed numbers


b/w [0,1]
>> x=rand(3,4)

x=

0.9501 0.4860 0.4565 0.4447


0.2311 0.8913 0.0185 0.6154
0.6068 0.7621 0.8214 0.7919

a=[1 2 3;4 5 6;7 8 9], b=[9 8 7;6 5 4; 3 2 1]

a=

1 2 3
4 5 6
7 8 9

b=

9 8 7
6 5 4
3 2 1

>> c=a+b

c=

10 10 10
10 10 10
10 10 10

>> c=a-b

c=

-8 -6 -4
-2 0 2
4 6 8

>> c=a*b

c=

30 24 18
84 69 54
138 114 90
Practical LAB: 3

Read the two images and perform the arithmetic operations on them

I1=imread('D:\Documents and Settings\Harjas\Desktop\Image0357.jpg')


I2=imread('D:\Documents and Settings\Harjas\Desktop\02102009005.jpg')
I1_resize=imresize(I1,[600 600])
I2_resize=imresize(I2,[600 600])
I_add=imadd(I1_resize,I2_resize);imshow(I_add)
I_sub=imsubtract(I1_resize,I2_resize); imshow(I_sub)
I_mul=immultiply(I1_resize,I2_resize); imshow(I_mul)
I_div=imdivide(I1_resize,I2_resize); imshow(I_div)
I1_gray=rgb2gray(I1); imshow(I1_gray)
I1_mono=im2bw(I1); imshow(I1_mono)
I_crop=imcrop(I1,[0,0,200,200]); imshow(I_crop)
I_rotate=imrotate(I1,60); imshow(I_rotate)
Practical LAB: 4

Display the histogram of image and perform histogram


equalization:

>> a=imread('D:\Documents and Settings\Harjas\Desktop\Image007.jpg');

>> imshow(a); % figure :1 %

>>b=rgb2gray(a);

>>imshow(b); %figure: 2 %

>> imhist(b) %figure: 3 %

>>c=histeq(b)

>>imhist(c) %figure: 4 %

Figure 1: Original image a Figure 2: Gray image b of a


Figure 3: histogram of image b (B/W of a)

Figure 3: histogram equalization of image b stored in c


Detecting the edge of an image
Using
Canny, sobel, Log, Prewitt, zerocross methods

>>a=imread('D:\Documents and Settings\Harjas\Desktop\Image0357.jpg')


>>b=rgb2gray(a)

>>b_canny=edge(b,'canny');
>>imshow(b_canny) %figure: 1 %

>>b_sobel=edge(b,'sobel')
>>imshow(b_sobel) %figure: 2 %

>>b_LoG=edge(b,'LoG')
>>imshow(b_LoG) %figure: 3 %

>>b_Prewitt=edge(b,'Prewitt')
>>imshow(b_Prewitt) %figure: 5 %

>>b_zerocross=edge(b,'zerocross')
>>imshow(b_zerocross) %figure: 4 %

Fig 1: canny method Fig. 2: sobel method


Fig 3: LoG method Fig. 2: Prewitt method

Fig 1: zerocross method


>>a=imread('D:\Documents and Settings\Harjas\Desktop\Image0357.jpg')
>>h=fspecial('motion',30,45)
>>c = imfilter(a,h)
>>imshow(c)

i=imread('D:\Documents and Settings\Harjas\Desktop\Image0357.jpg');


j=imnoise(i,'salt & pepper',0.05);
k=rgb2gray(j);
l=medfilt2(k);
imview(k),imview(l) % Figure1 & figure 2
Find the color composition of the picture in the 11 row and 14 column,
&
Plot the image as a figure

>> a=imread('D:\Documents and Settings\Harjas\Desktop\Image0357.jpg')

>>a(11,14,:)

ans(:,:,1) = 148
ans(:,:,2) = 134

ans(:,:,3) = 121

>>image(a(11,14,:))

>>image(a)
Practical LAB: 5

Compute the correlation coefficient of two matrices of equal


size:

>> a=[1,2,3;4,5,6;7,8,9];

>> b=[9,8,7;6,5,4;3,2,1];

>> r=corr2(a,b)

r = -1
Practical LAB: 6

Laplacian Filter:

>> f=imread(‘moon.tif’);
>> w=fspecial(‘laplacian’,0);

%OR w=[0 1 0;1 -1 1;0 1 0];


%This will generate image (b) with ALL pixels positive. Check workspace

>> g1=imfilter(f,w,’replicate’);
>> figure(1),imshow(g1,[]);

%g1 is of class uint8.


%need to convert to double first

>> f2=im2double(f);
%Effect of laplacian mask, grayish edge line, featureless background

>> g2=imfilter(f2,w,’replicate’);
%Laplacian filtered image using double formats

>> figure(2),imshow(g2,[]);

%Restore gray tone lost by using laplacian via subtracting laplacian from
%the original image (substract since center coefficient is negative)
% to correct featureless background – add the original and laplacian filtered
image. center of laplacian mask -ve (use minus)

>> g=f2-g2;
>> figure(3),imshow(g);

You might also like