Digital Image Processing Matlab Basics
Digital Image Processing Matlab Basics
Matlab Basics
Matlab
It allows one to perform numerical calculations, and visualize the results
without the need for complicated and time consuming programming.
Highly modular.
Enables OO Programming.
Getting Help
>> help help (explain how to get help)
>> helpbrowser / doc (open the online Matlab documentation)
>> help images (list of all commands in the Image Processing Toolbox)
>> demo (supply various guide videos)
>> lookfor read (display list of functions with ‘read’ in the name or help
text)
>> type imread (display contents of file)
>> help imread (function name + % block)
>> doc imread (function documentation in help browser)
Using [tab] is useful to auto-complete function
names and variables
Matlab Basics
Digital image representation :
2D function f(x,y) -> finite discrete quantities
Coordinate Conventions:
img(r,c)
r–rows (height)
c–cols (width)
>> size(img)
img(1,1)
Image Types
• Intensity images
scaled to represent intensities (uint8 – [0,255], double [0,1])
• Binary images
logical array of 0s and 1s
• Indexed images
Look up table [x, map]
• RGB images
truecolor, array of (m*n*3)
>> impixelinfo
display intensity value of individual pixel interactively
>> A = [1 2 3; 2 3 4; 3 4 5];
>> A = 0.0005
Vector indexing
row vector (1xN)
>> v = [1 3 5 7]; (elements separated by space or comma (,))
>> v(2) = 3;
column vector (MX1)
>> w = [1;3;5;7]; (elements separated semi-comma (;))
>> w = v’ (transpose operation)
w=
1
3
5
7
To Access blocks of elements we use colon notation
>> v(2:4)
ans =
357
>> v(1:end) end is the last element in the vector
>> v(:) produce a column vector
>> v(1:2:end) enables steps (jumps)
>> v(end:-2:1) steps can be negative as well
Vector can be used as an index into another vector
>> v([1 3 4])
ans =
1 5 7
Matrix Indexing
Image – 2D array, matrix
Matrix can be represented as a sequence of row vectors
>>A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
To access an element, 2 indexes are used – row index and column index
>> A(2,3) 6
>> A(:,3)
3
6
9
>> A(2,:)
456
>> a(1:2,1:3)
123
456
>> B = A;
>> B(:,3) = 0
B=
1 2 0
4 5 0
7 8 0
Using vectors to index into a matrix provide a powerful tool for element selection
A([1 3], [2 3])
2 3
8 9
.Matrix Indexing Cont
Image – 2D array, matrix
A matrix is also represented as a long vector
>> A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
To access sequential elements in a matrix a single index can also be used:
>> A(1:9)
A=
1 4 7 2 5 8 3 6 9
The size of the output matrix is the size of the index matrix!
14
Matrix Addressing
A very useful approach is to use logical matrix as an index
to the matrix
>> D = logical([1 0 0; 0 0 1; 0 0 0])
=D
0 0 1
1 0 0
0 0 0
A(D) >>
= ans
1
6
The use of column operation on a matrix produce a single
column vector from the matrix (on a column by column
basis). It is very useful for image operations like sum or max
>> s = sum(f(:)) (equivalent to: sum(sum(f)))
15
Operators
Arithmetical operators have their algebraic meaning:
>> A = [1 2 3; 4 5 6; 7 8 9];
>> A + A
ans =
2 4 6
8 10 12
14 16 18
>> A * [1 1 1]
??? Error using ==> times
Matrix dimensions must agree
>> A * [1 1 1]’
ans =
6
15
24
16
.Operators cont
Vector multiplication depends on the order of the two
vectors
>> [1 2 3] * [2 3 4]
??? Error using ==> times
Matrix dimensions must agree
>> [1 2 3] * [2 3 4]’
ans =
20
>>[1 2 3]’ * [2 3 4]
ans =
2 3 4
4 6 8
6 9 12
17
.Operators cont
Element by element operators - .* .^ ./
>> [1 2 3] .* [2 3 4]
ans =
2 6 12
>> [1 2 3] .^ 2
ans =
1 4 9
>> [1 2 3] ./ (2:2:6)
ans =
0.5000 0.5000 0.5000
18
Array dimensions
Matlab arrays can be of any dimensions
It is useful to operate on specific dimension of the
array, for example:
>> height = size(i,1);
19
Standard Arrays
Generating simple array enables trying out simple
ideas and test the syntax of a function during
development
>> zeros(m,n)
>> ones(m,n)
>> true(m,n)
>> false(m,n)
>> magic(m)
>> rand(n)
>> randn(n)
>> pascal(n)
20
Additional Operators
Arithmetic operators (numeric computations)
◦ matrix arithmetic (linear algebra A*B)
◦ array arithmetic (element by element A.*B)
+, -, ./, .^,:..
Relational operators (compare)
◦ Compare corresponding elements of arrays of equal
dimensions (<, >,<=, >=, ==, ~=) or an array to scalar
Logical operators can operate both on logical
and numeric data (and: &, or: |, not: ~)
true: logical 1 or non-zero numeric quantity
false: logical or numerical 0
logical functions : any, all
21
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
22
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
23
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1, :);
>> imshow(s);
24
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
25
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
26
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
27
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
28
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
29
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
>> i(200:300, 200:400) = 0;
>> imshow(i);
30
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
>> i(200:300, 200:400) = 0;
>> imshow(i);
31
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
>> i(200:300, 200:400) = 0;
>> imshow(i);
>> imshow(i/2);
32
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
>> i(200:300, 200:400) = 0;
>> imshow(i);
>> imshow(i/2);
33
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
>> i(200:300, 200:400) = 0;
>> imshow(i);
>> imshow(i/2);
>> imshow((i>0.8).*i);
34
Examples - Matrix indexing
>> i = imread('sowrds0040.bmp');
>> i = rgb2gray(double(i)/255);
>> imshow(i)
>> s = i(end:-1:1,:);
>> imshow(s);
>> a = i(200:300,200:400);
>> imshow(a);
>> t = i(1:2:end, 1:2:end);
>> imshow(t);
>> i(200:300, 200:400) = 0;
>> imshow(i);
>> imshow(i/2);
>> imshow((i>0.8).*i);
35
M-Files
M-Files can be one of two:
Scripts – A series of commands that are performed on the
global scope.
No input and output variables.
36
M-Function Programming
Components of m files:
Function definition line
function [out1 out2] = name(in1, in2, in3)
37
M-Function Programming
Components of m files (cont.):
38
Flow control
if, else, elseif, end
switch, case, otherwise, end
return
try...catch…end
1D indexing
Convert for / while loops to equivalent vector or matrix
operations
x = 0:k-1 >>
>> for x = 1:k
;ff = 5*sin(x/(2*pi)) >>
ff(x) = 5*sin((x-1)/(2*pi));
end
40
Code optimization –
vectorizing loops
2D indexing
meshgrid – convert rows vectors to arrays C and R
that can be used for evaluating function with two
variables
>> for r = 1:10
>> for c = 1:10 meshgrid(1:c, 1:r) = ]C, R[ >>
>> b(r,c) = r.^2+ c.^2 ;h = R.^2 + C.^2 >>
>> end
>> end
41
Code Optimization –
Pre-allocating large arrays
Simple way to improve code execution is to
pre-allocate the size of the arrays in the
program.
>> f = zeros(1024);
42
Cell arrays and Structures
Cell array is multidimensional array whose
elements are copies of other arrays
>> c = {‘gauss’,[1 0;0 1], 3}
>> c{1}
ans =
gauss
Structures are similar to cell arrays (allow grouping
of a collection of dissimilar data) but they
addressed by fields rather than by numbers
>> params.nimgs = 100;
>> params.jump = 2;
>> params.baseStr = ‘testImg’
43
Arguments
Matlab arguments are always passed by value
Checking whether an argument exist
>> exist(a,’var’)
Checking number of arguments to the
functions
>> nargin, nargout, nargchk
Getting variable number of arguments
>>varargin, varargout
44
Gui
>> guide (Graphic User Interface Development
Environment)
Start the GUI Layout Editor. Guide create
◦ fig file: complete description of the gui elements
and their arrangements
45
?Any Questions