Professional Documents
Culture Documents
dip_lab_programs.doc
dip_lab_programs.doc
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:
Naming Variables:
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;
✔ 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.
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
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
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
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
x=
1 1 1 1
1 1 1 1
1 1 1 1
x=
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
>>b=rgb2gray(a);
>>imshow(b); %figure: 2 %
>>c=histeq(b)
>>imhist(c) %figure: 4 %
>>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 %
>>a(11,14,:)
ans(:,:,1) = 148
ans(:,:,2) = 134
ans(:,:,3) = 121
>>image(a(11,14,:))
>>image(a)
Practical LAB: 5
>> 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);
>> g1=imfilter(f,w,’replicate’);
>> figure(1),imshow(g1,[]);
>> 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);