0% found this document useful (0 votes)
6 views24 pages

Dip - Lab Manual - 1 - Updated

This document is a lab manual for Digital Image Processing at UET Taxila, focusing on the introduction to the MATLAB environment. It covers basic commands, vector and matrix definitions, and operations, providing examples for creating and manipulating data structures in MATLAB. The manual serves as a guide for students to effectively use MATLAB for technical computing and image processing tasks.

Uploaded by

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

Dip - Lab Manual - 1 - Updated

This document is a lab manual for Digital Image Processing at UET Taxila, focusing on the introduction to the MATLAB environment. It covers basic commands, vector and matrix definitions, and operations, providing examples for creating and manipulating data structures in MATLAB. The manual serves as a guide for students to effectively use MATLAB for technical computing and image processing tasks.

Uploaded by

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

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

DIGITAL IMAGE PROCESSING

LAB MANUAL 1

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Introduction to MATLAB Environment

MATLAB is a high-performance language for technical computing. It integrates computation,


visualization, and programming in an easy-to-use environment where problems and solutions are
expressed in familiar mathematical notation.
The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide
easy access to matrix.

Starting and Quitting MATLAB


 To start MATLAB, double-click the MATLAB shortcut icon on your Windows desktop.
 You will know MALTAB is running when you see the special " >> " prompt in the
MATLAB Command Window.
 To end your MATLAB session, select Exit MATLAB from the File menu in the desktop, or
type quit (or exit) in the Command Window, or with easy way by click on close button
in control box.
Desktop Tools
1- Command Window: Use the Command Window to enter variables and run
functions and M-files.
2- Command History: Statements you enter in the Command Window are logged
in the Command History. In the Command History, you can view previously run
statements, and copy and execute selected statements.
3- Current Directory Browser: MATLAB file operations use the current directory
reference point. Any file you want to run must be in the current directory or on
the search path.
4- Workspace: The MATLAB workspace consists of the set of variables (named
arrays) built up during a MATLAB session and stored in memory.

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Current Directory Browser Command Window

Workspace

Command
History

5- Editor/Debugger Window: Use the Editor/Debugger to create and debug M-


files.

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

1-2 Basic Commands

• clear Command: Removes all variables from workspace.

• clc Command: Clears the Command window and homes the cursor.

• help Command: help <Topic> displays help about that Topic if it exist.

• lookfor Command: Provides help by searching through all the first lines of
MATLAB help topics and returning those that contains a key word you specify.
• edit Command: enable you to edit (open) any M-file in Editor Window. This
command doesn’t open built-in function like, sqrt. See also type Command.
• more command: more on enables paging of the output in the MATLAB
command window, and more off disables paging of the output in the
MATLAB command window.

Notes:
• A semicolon " ; " at the end of a MATLAB statement suppresses printing of results.

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

• If a statement does not fit on one line, use " . . . ", followed by Enter to indicate that the
statement continues on the next line. For example:
>> S=sqrt (225)*30*...

(20*sqrt (100))

• If we don’t specify an output variable, MATLAB uses the variable ans (short for answer), to
store the last results of a calculation.
• Use Up arrow and Down arrow to edit previous commands you entered in Command Window.
• Insert " % " before the statement that you want to use it as comment; the statement will appear
in green color.

Example 1:
>> a=3
>> a=3; % can you see the effect of semicolon " ; "
>> a+5 % assign the sum of a and 5 to ans
>> b=a+5 % assign the sum of a and 5 to b
>> clear a
>> a % can you see the effect of clear command
>> clc % clean the screen

2- Introduction to Vectors in Matlab

This is the basic introduction to Matlab. Creation of vectors is included with a few basic
operations. Topics include the following:

1. Defining a vector
2. Accessing elements within a vector

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

2.1 Defining a Vector


 Matlab is a software package that makes it easier for you to enter matrices and vectors, and
manipulate them.
 The interface follows a language that is designed to look a lot like the notation used in linear
algebra.
 Almost all of Matlab's basic commands revolve around the use of vectors.
 A vector is defined by placing a sequence of numbers within square braces:

>> v = [3 1 7 -21 5 6 4]

v=

3 1 7 -21 5 6 4

 This creates a row vector which has the label "v".


 The first entry in the vector is a 3 and the second entry is a 1.
 Note that Matlab printed out a copy of the vector after you hit the enter key.
 If you do not want to print out the result put a semi-colon at the end of the line:

>> v = [3 1 7 -21 5 6 4];


>>

If you want to view the vector just type its label:

>>v

v=

3 1 7 -21 5 6 4

Notice:
 though, that this always creates a row vector.
 If you want to create a column vector you need to take the transpose of a row vector.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 The transpose is defined using an apostrophe ("'"):

>> v = [3 1 7 -21 5 6 4]'

v=

3
1
7
-21
5
6
4

Or you can use the transpose operator ( .’ )

>> w=v.’

w=

3
1
7
-21
5
6
4

 To access block of elements, we use MATLAB’s colons notation,


 e.g to access the first three elements of v we write

>>v(1:3)

ans=

3 1 7

 Similarly to access the second through fourth elements of v we write

>>v(2:4)
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

ans=

1 7 -21

 Similarly to access all elements from some specific location say the 3rd through the last
element

>>v(3:end)

ans=

7 -21 5 6 4

 Produces a column vector

>>v(:)

 Produces a row vector

>>v(1:end)

 Indexing is not restricted to contiguous locations.


 Combine the colon operator and end to achieve a variety of effects, such as extracting
every k-th element or flipping the entire vector.

>>v(1:2:end) % Extract all the odd elements


ans =
16 9 2 7
>>v(end:-1:1) % Reverse or Flip the order of elements
ans =
14 7 11 2 4 9 5 16
>>v(end:-2:1) % show odd elements only after Reverse or Flip

ans=

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

4 5 7 3

You can even do arithmetic using end.


 v(2:end-1) % Extract the second through the next-to-last elements
ans =
5 9 4 2 11 7

Creating Large Vectors

 A common task is to create a large vector with numbers that fit a repetitive pattern.
 Matlab can define a set of numbers with a common increment using colons.
 For example, to define a vector whose first entry is 1, the second entry is 2, the third is
three, up to 8 you enter the following:

>> v = [1:8]

v=

1 2 3 4 5 6 7 8

 If you wish to use an increment other than one that you have to define the start number,
the value of the increment, and the last number.
 For example, to define a vector that starts with 2 and ends in 4 with steps of .25 you enter
the following:

>> v = [2:.25:4]

v=

Columns 1 through 7

2.0000 2.2500 2.5000 2.7500 3.0000 3.2500 3.5000

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Columns 8 through 9

3.7500 4.0000

2.2 Accessing elements within a vector

You can view individual entries in this vector.

 For example to view the first entry just type in the following:

>>v(1)

ans =

 This command prints out entry 1 in the vector. Also notice that a new variable called ans has
been created. Any time you perform an action that does not include an assignment MATLAB
will put the label ans on the result.
 A vector can be used as an index into another vector.
 For example we can pick the first, third sixth and seventh elements of v using the command

>>v = [3 1 7 -21 5 6 4]
>>v([1 3 6 7])

v=
3 7 6 4

Introduction to Matrices in Matlab


A basic introduction to defining and manipulating matrices is given here. It is assumed that you
know the basics on how to define and manipulate vectors using matlab.
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Defining Matrices
 Defining a matrix is similar to defining a vector.
 To define a matrix, you can treat it like a column of row vectors (note that the spaces are
required!):
>> A = [ 1 2 3; 3 4 5; 6 7 8]
A=

1 2 3
3 4 5
6 7 8

 You can also treat it like a row of column vectors:

>> B = [ [1 2 3]' [2 4 7]' [3 5 8]']

B=

1 2 3
2 4 5
3 7 8

 If you lose track of what variables you have defined, the whos command will let you know
all of the variables you have in your work space.

>>whos
Name Size Bytes Class
A 3x3 72 double array
B 3x3 72 double array
v 1x5 40 double array
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Grand total is 23 elements using 184 bytes

You can work with different parts of a matrix, just as you can with vectors. Again, you have to be
careful to make sure that the operation is legal.

 To extract the element in the 2nd row and 3rd column:

>> A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(2,3)
ans = 5

 The colon operator is used in the matrix indexing to select a two dimensional block of
elements out of matrix:

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>C= A( : , 3) % this statement picks the third column of the matrix
ans=
C=
3
5
8

 The colon operator can also be used in the matrix indexing to extract the row of the
matrix
>>A = [ 1 2 3; 3 4 5; 6 7 8];
>> C= A (2 , :) % this statement picks the 2nd row of the matrix
ans=
C=2 4 5

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 Array Index out of bound


>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(1:2,3:4)
??? Index exceeds matrix dimensions.

 Slicing matrix A

>>A = [ 1 2 3; 3 4 5; 6 7 8]
>>A(1:2,2:3)
A=
1 2 3
3 4 5
6 7 8

ans =
2 3
4 5

 Transposing the sub matrix

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(1:2,2:3)'
ans =
2 4
3 5

 To create a matrix B equal to A but with its last column set to 0’s we write

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>B= A;
>> B (: , 3)=0
B=

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

1 2 0
2 4 0
3 7 0

>> A = [ 1 2 3; 3 4 5; 6 7 8];
>>A(end , end)
ans =
8

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>D= A ([1 3] , [2 3])
D=
2 3
7 8

 Matrix addressing can also be used in the following way


>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>E= logical ([1 0 0; 0 0 1; 0 0 0])
E=
1 0 0
0 0 1
0 0 0
 Now if we write
>>A(E)
ans =
1
5

 The use of a colon is useful in case to find the sum of all elements of a matrix
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

>>A = [ 1 2 3; 3 4 5; 6 7 8];
>>s= sum (A(:))
s =39

 Some important standard arrays are:

A = [ 1 2 3; 3 4 5; 6 7 8]
>> A = 5 * ones (3,3)
A=
5 5 5
5 5 5
5 5 5

 magic (n ) returns an n-by-n matrix constructed from the integers 1 through n^2 with equal
row and column sums.
 The order n must be a scalar greater than or equal to 3 in order to create a valid magic square.

>> magic (3)


ans =
8 1 6
3 5 7
4 9 2

Basic Matrix Functions

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Command Description

sum(x)  The sum of the elements of x.


>> x=[ 1 2 3; 3 4 5; 6 7 8];  For matrices, sum(x) is a row vector with the
>> sum(x) sum over each column.
ans =  sum (x,dim) sums along the dimension dim.
10 13 16
>>sum(sum(x))
ans = 39
>> sum(x,2) ans= 6
12 21

size(x)  return the size (dimensions) of matrix x.

>>x=[ 1 2 3; 3 4 5; 6 7 8]
>> size(x)
ans = 3 3

return the length (number of elements) of vector


length(v) 
v.
>> v=[1 2 3];
>> length(v)
ans = 3

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

numel(x)  returns the number of elements in array x.


>> v =[55 63 34];
>> numel(v)
ans = 3
>> x=[1 2
4 5
4 8 ];
>> numel(x)
ans = 6

single quote ( ' )  Matrix transpose flips a matrix about its main
>> x=[1 2 3 diagonal and it turns a row vector into a column
vector.
4 5 6
7 8 9];
>> x'
ans =
1 4 7
2 5 8
3 6 9
>> v=[1 2 3];
>> v'
ans =
1
2
3

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

max(x)  Find the largest element in a matrix or a vector.


>> x=[1 2 3
4 5 6];
>> max(x)
>> ans= 4 5 6
>> max(max(x))
ans = 6

min (x)  Find the smallest element in a matrix or a


vector.
>> x=[1 2 3
5 5 6];
>>min(x)
ans = 1 2 3
>> min(min(x))
ans = 1

magic(N)  produce N Magic square.


 This command produces valid magic squares for
>> magic(3) all N>0 except N=2.
ans =
8 1 6
3 5 7

4 9 2
inv(x)  produce the inverse of matrix x.
>> x=[1 4;
5 8];
>> inv(x)

ans =

-0.6667 0.3333 0.4167


-0.0833

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

diag(x)  Return the diagonal of matrix x.


 if x is a vector then this command produce a
>> x=[ 1 2 3 diagonal matrix with diagonal x.
4 5 6
7 8 9];
>> diag(x)
ans =
1
5
9
>> v=[1 2 3];
>> diag(v)
ans =
1 0 0
0 2 0
0 0 3

median(x)  The median value of the elements of x.


x=[ 4 6 8  For matrices, median (x) is a row vector
10 9 1 with the median value for each column.
4 2 5];
>> median(x)
ans =
7 6 5
>> median(x,2)  median(x,dim) takes the median along the
dimension dim of x.
ans =
6
9
5
>> median(median(x))
ans = 6
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

sort(x,DIM,MODE) Sort in ascending or descending order.


>> x = [3 7 5  - For vectors, sort(x) sorts the elements of x
0 4 2]; in ascending order.
>> sort(x,1)
ans =  For matrices, sort(x) sorts each column of
0 4 2 x in ascending order.
3 7 5
>> sort(x,2)
ans = DIM= 1 by default
3 5 7
0 2 4 MODE= 'ascend' by default
>> sort(x,2,'descend')
ans =
7 5 3
4 2 0

tril(x)  Extract lower triangular part of matrix x.


>> x=[5 1 8
4 7 3
2 5 6];
>> tril(x)
ans =
5 0 0
4 7 0
2 5 6

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

triu(x)  Extract upper triangular part of matrix x.


>> x=[5 1 8
4 7 3
2 5 6];
>> triu(x)
ans =
5 1 8
0 7 3  Create a matrix is to use a function, such
0 0 6 as ones, zeros, or rand. For example,
create a 5-by-1 column vector of zeros.
>>z = zeros(5,1)
z =
0
0
0
0
0

Note :

 Arithmetic operations on arrays are done element-by-element.


 This means that addition and subtraction are the same for arrays and matrices, but that
multiplicative operations are different.
 MATLAB uses a dot ( . ) or decimal point, as part of the notation for multiplicative array
operations.
Example1: Find the factorial of 5
Solution :
>>x=2:5
>>prod(x)
x=
2 3 4 5

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Example2: if x = [1,5,7,9,13,20,6,7,8], then

a) replace the first five elements of vector x with its maximum value.
b) reshape this vector into a 3 x 3 matrix.

solution :
a) >> x(1:5)=max(x)
b) >> y(1,:)=x(1:3);
>> y(2,:)=x(4:6);
>> y(3,:)=x(7:9);
>> y

Example3: Generate the following row vector b=[1, 2, 3, 4, 5, . . . . . . . . . 9,10],


then transpose it to column vector.

solution :

>> b=1:10
b=
1 2 3 4 5 6 7 8 9 10
>> b=b';

Digital Image Processing 6th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Task 1
1- Use edit command to edit the dct function, then try to edit sin function. State
the difference.
2- Use help command to get help about rand function.
3- Enter a=3; b=5; c=7, then clear the variable b only

Task 2
4- If x=[1 4; 8 3], find :
a) the inverse matrix of x .
b) the diagonal of x.
c) the sum of each column and the sum of whole matrix x.
d) the transpose of x.

5- If x= [2 8 5; 9 7 1], b=[2 4 5] find:


a) find the maximum and minimum of x.
b) find median value over each row of x.
c) add the vector b as a third row to x.

6- If x=[ 2 6 12; 15 6 3; 10 11 1], then

a) replace the first row elements of matrix x with its average value.
b) reshape this matrix into row vector.

7- Generate a 4 x 4 Identity matrix.

8- Generate the following row vector b=[5, 10, 15, 20 . . . . . . . . . 95, 100], then
find the number of elements in this vector.

Task 3:
Defining a matrix A = [ 1 8 3;5 2 0 ; 4 1 9]
1. extract the element in the 2nd row and 3rd column
2. picks the 3rd row of the matrix
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

3. picks the 2nd column of the matrix


4. create a matrix B equal to A but with its last column set to 0’s
5. find the sum of all elements of a matrix

Digital Image Processing 6th Term-SE UET Taxila

You might also like