Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Fundamentals of Computer Vision(CS

495)
Topic : Ch 1 Introduction(DIPUM 3ed)
Dr. S. Hassan Amin
https://sites.google.com/a/nu.edu.pk/sha1/Home
Assist Prof and HOD(Computer Science)
FAST-NU Peshawar
Contents

 This chapter will introduce Matlab and Image


Processing toolbox:
 Digital Image Representation
 Reading Images
Topic 2.1 : Digital Image Representation
 An image is considered as a two dimensional function
f(x,y).
 x,y are spatial coodinates
 Value of f(x,y) is amplitude or intensity of f(x,y) at point
x,y
 Image may be continuous in coordinates x,y as well as
amplitude
 Digital image is obtained by:
 Digitizing coordinates x,y and is referred to as sampling
 Digitizing amplitude values and is known as quantization
Topic 2.1.1 : Coordinate Conventions
Topic 2.1.2: Images as Matrices
 Standard definition of digitized image is:

𝑓 ( 𝑥 , 𝑦 ) =¿
 
Topic 2.2: Reading Images

 Images are read as follows:


im=imread(‘fname’)
e.g.
im=imread(‘chest.jpg’);
Similarly, we can easily find size using :
size(im)
Or
[M,N]=size(im)
Topic 2.2: Reading Images (Cont’d)

 Additional information about image can be obtained as:


whos im
Displaying Images

 Images may be displayed using imshow :


imshow (im)
Or
imshow(im,[low high])
Displays as black all pixels that are less than low, and white
all pixels that are greater than or equal to high
Imshow(im,[])
Set low to minimum value of the image, and high to
maximum value of the image.
Displaying Images

 Matlab replaces current image, when another imshow


command is issued.
 To display multiple images, use :
figure, imshow(im);
Topic 2.4 : Writing Images
 Images are written to a file in current directory using:
imwrite(im,’filename’)
File types supported are automatically recognized from the
extension.
 To find relevant information about image files stored on
disk, you may use:
imfinfo filename
Classes
Image Types
 Four types of images are supported:
 Gray-scale images
 Binary images
 Indexed images
 RGB images
Topic 2.6.1 : Gray-scale images
 A gray-scale image is a data matrix whose values
represent shades of gray.
 Elements of a gray-scale matrix are of class uint8 or
uint16 have integer values in the range [0,255] or
[0,65535].
 Elements may also be of type single or double
Topic 2.6.2 : Binary Images
 A binary image is a logical array of 0s and 1s.
 An array of 0s and 1s may not always be binary image
 A numeric array is converted to binary using logical
function
 Thus if A is a numeric array consisting of 0s and 1s, then
we create a logical array B using the statement:
B = logical(A)
Topic 2.7: Converting between Classes

 Conversion between classes is a common operation:


 Im2uint8
 Im2uint16
 Im2double
 Im2single
 Mat2gray
 Im2bw
 Mat2gray function is a useful function because it converts
image of any class to an array of class double having range
[0,1]
g=mat2gray(A)
Topic 2.8 : Array Indexing
 Matlab supports a number of array indexing schemes.
 Given row vector v:
>> v =[1 3 5 7 9]
 To access second element:
>>v(2)
Ans=
3
 To convert row vector to column vector
>> w = v.’
To access block of elements:
>> v(1:3)
Ans= 1 3 5
Topic 2.8 : Array Indexing (Cont’d)
 We can also access array elements in other sequence
e.g. every second element
>> v(1:2:end)
159
Indexing Matrices
 A small matrix may be coded as:
>> A = [1 2 3;4 5 6;7 8 9]
 A specific element may be accessed as:
>>A(2,3)
Ans =
6
 A sub-matrix can be extracted by specifying a vector of values for
both the row and column indices:
>> T=A([1 2],[1 3])
T=

1 3
4 6
Indexing Matrices (Cont’d)
>>T=A(1:2,1:3)
T=
1 2 3
4 5 6
 To select all rows and a particular column:
>>C3=A(:,3)
ans =
3
6
9
Indexing Matrices (Cont’d)
 Following statement sets all elements of a certain
column to a particular value:

>> B(:,2)=0
B=
1 0 3
4 0 6
7 0 9
Topic 2.8.3 : Indexing with a single
Colon
 To convert the given matrix into a column vector
>>>> A
A=
1 2 3
4 5 6
7 8 9
>> B=A(:)
B=
1
4
7
2
5
8
3
6
9
Topic 2.8.3 : Indexing with a single
Colon (Cont’d)
 To find sum of entire matrix:
>>total=sum(A(:))
Topic 2.8.5 : Linear Indexing
 Linear indexing uses a single subscript to access a matrix or array.
>> A
A=
1 2 3
4 5 6
7 8 9
>> A([3 5 9])

ans =

7 5 9
Topic 2.8.7 : Sparse Matrices
 When a matrix has a large number of 0s, it is advantageous to
express it in sparse form to reduce storage requirements.
>> A=[2 0 0; 0 3 0; 3 0 0]
A=2 0 0
0 3 0
3 0 0
>> S=sparse(A)
S = (1,1) 2
(3,1) 3
(2,2) 3
Topic 2.9 : Some Important Standard
Arrays
 Zeros(M,N)
 Ones(M,N)
 True(M,N)
 False(M,N)
 Magic(M)
 Eye(M)
 Rand(M,N)
 Randn(M,N)
 Note: You get a square matrix if you use only one argument
in any one of the above.
Topic 2.10 : Introduction to M-Function
Programming
 You need to write scripts and functions for any moderately
difficult problem.
 M-file allows you to save your scripts and functions and run
these from command prompt
 The components of a M-file are:
 The function definition line
 The H1 line
 Help text
 The function body
 Comments
Topic 2.10 : Introduction to M-Function
Programming (Cont’d)
 The function definition line has the form:
function [outputs] = name(inputs)

 Example
function [s, p] = sumprod(f, g)
 The H1 line immediately follows function definition
%Its a useful function
Topic 2.10 : Introduction to M-Function
Programming (Cont’d)
 Example

function [w, wmax, wmin] = imblend(f,g)


% calculates the weighted sum of two images
% w is the output image, with wmax being its max and wmin being
its minimum
w1=0.5*f;
w2=0.5*g;
w=w1+w2;
wmax = max(w(:));
wmin = min(w(:));
Topic 2.10.3 : Flow Control
 You need flow-control, branching, loops etc.
 The general syntax for if statement:

if expression1
statement1
elseif expression2
statement2
else
statement3
end
Topic 2.10.3 : Flow Control (Cont’d)
 Example :
if (ndims(A)>2)
error(‘Messeage 1’)
end;
Topic 2.10.3 : Flow Control (Cont’d)
 For statement

for index = start:increment:end


statements
end;

Example :

For q = 0:5:100
x=x+5;
End;
Topic 2.10.3 : Flow Control (Cont’d)
 While syntax
while expression
statements
end
 Example
A =10
B=5
While a
a=a-1;
b=b+1
End;
Topic 2.10.3 : Flow Control (Cont’d)
 Break Statement
 Break terminates the execution of a for or while loop.
 When a break statement is encountered, execution continues with
the next statement outside the loop.
 Continue Statement
 The continue statement passes the control to the next iteration of
the for or while loop in which it appears, skipping any remaining
statements in the body of the loop.
Topic 2.10.3 : Flow Control (Cont’d)
 Switch statement
End

You might also like