DIP Lab Intro
DIP Lab Intro
Processing in MATLAB
1
Outline
Introduction Matlab & Image Processing
Variables Coordinate Systems
Arrays Some Basic Concepts
Built-in Variables
Operations and Functions Image Types
Operations Converting Between Image
Linear Algebra Types
Functions Converting Between Image
Graphics Classes
Programming Image Arithmetic
Scripts and Data Management Working with Image
Functions
Sequences
2
Introduction
MATLAB (stands for “Matrix laboratory”) is an environment suited for working with
arrays and matrices.
Several toolboxes (i.e. sets of predefined routines) are available, focusing on different
engineering fields: statistics, signal processing, image processing, wavelets, control,
data acquisition ...
MATLAB can be used both from the command line window and by running scripts
stored in “.m” files.
Data can be saved in files that end in “.mat”.
4
MATLAB Environment
5
How to Define Variables?
No need to define their format (int, real ...) or dimensions in advance, nor to
declare them at the beginning of your program.
MATLAB data types: int8, uint8 (unsigned integer), int16, uint16, double.
It is preferable to do numerical operations on double precision, which takes 8
bytes per value.
6
How to Define a Vector?
7
Comma and Semicolon
“,” Comma: array row separator.
“;” Semicolon: column separator.
8
How to Define a Matrix?
A matrix is a stack of row vectors. Just type A=[1 2 3; 11 12 13]
to create a 2×3 matrix:
1 2
3
To access the (i,j) element of
A a, just type
11 12
A(i,j). For instance A(2, 3) will return ans
= 13.
Elements of a matrix can also13be reassigned, e.g. A(2, 3)=50;
transforms A in to:
1 2 3
A
11
12 50
What is A(6)? A(5)? A(4)?
What is A(2,:)? A(:,2)? A(:)?
Other way of getting the second column of A?
9
10
Defining Matrices
1
Transpose: type A=A’
• Try help elmat to see the full list of Elementary matrices and matrix manipulations.
11
Matrix operations
Matrix operations (transpose, inverse, eigenvalues and eigenvectors)
transpose(A) is (almost) the same as A' which is the complex conjugate transpose.
inv(A) is the inverse matrix.
eig(A) returns eigenvalues and also eigenvectors if two output arguments are used.
>> A=[1 2 3; 3 3 4; 3 4 5]
>> transpose(A)
>> A'
>> inv(A)
>> eig(A)
>> [eigvec, eigval]=eig(A);
12
Matrix operations
A set of linear equations Ax=b can be solved using x=A\b, which
is roughly the same as x=inv(A)*b, except that it is computed
using Gaussian elimination.
Backslash operator \ can also be used for computing the least squares
approximation for over- or underdetermined system of equations.
>> A=[1 2 3; 3 3 4; 3 4 5]
>> b=[0;2;5]
>> A\b
>> x=[0 2 4 6 8]
>> y=[1;3;5;7;9]
>> C=[1 2 3 5 7;1 1 2 3 5]'
>> C\y
>> C*x-y
13
Operations & Functions
can
14
Elementary Operations
The “period” character (.) distinguishes the array operations from the matrix
operations. However, since the matrix and array operations are the same for addition
and subtraction, the character pairs .+ and .- are not used.
A.*B is the element-by-element product of the arrays A and B. A and B must have
the same size, unless one of them is a scalar.
15
Elementary Matrix Operations
16
Relational Operations
17
Logic Operations
18
Linear Algebra
19
Other Math Functions
20
Example: Rounding Functions
fix(X) rounds the elements of X to the nearest integers towards
zero.
round(X) rounds the elements of X to the nearest integers.
ceil(X) rounds the elements of X to the nearest integers towards
infinity.
floor(X) rounds the elements of X to the nearest integers towards
minus infinity.
Example:
fix(X) = [ -1 Applying
-1 the1 previous
1] functions toround(X)
X = [-1.5=-1.49
[ -2 1.49
-1 1.5]
1
results
ceil(X) =in:[ -1 2 ]
-1 2 2 ] floor(X) = [ -2 -2 1
1]
Note that fix behaves as ceil when X < 0 and as floor when X > 0.
21
Simple Graphics
Drawing of a sinusoidal
signal:
22
30
Histogram Charts
The elements of a vector can be displayed with bars or histograms.
The height of the rectangle is equal to the number of elements in that class.
As example:
hist(x) Plots a histogram with 10 intervals (default) for the elements in
vector x.
hist(x,n) Plots a histogram with n intervals for the elements in vector x.
hist(x,y) Plots a histogram with arbitrary intervals. These are given in
vector x.
23
Histogram Charts
Draw a histogram with 15 intervals for the vector x above. It is rather difficult to
see how long the intervals are. Maybe its better to draw a histogram with
6 intervals since we know the between the maximum and minimum value.
>> hist(x,6)
If we don’t know the dataset, we can define the intervals that we are interested to
have in the histogram. Suppose we want the integer values between 0 and 10.
>> y=0:10;
>> bar(x),grid
www.hig.no/~faouzi
>> title('bar for vector x')
24
Stem Plot
To make this a bit more complete we show some other plotting possibilities. In
Matlab we can also illustrate a discrete sequence (stem plot). This is done as:
>> stem(x), grid
>> title('Stem diagram for vector x')
See figure below. Notice that the values in vector x are plotted versus its index.
www.hig.no/~faouzi
25
Stem Plot
Assume you want to produce a curve from sampled data. If you take sampled
data from a sine curve with an amplitude of 1. It becomes a discrete sequence of
data. The sine curve looks like: y=sin(x);
>> z=0:0.2:10;
>> yy=sin(z)
>> stem(yy),grid,title('Stem plot for a sine curve')
We can consider this to be a continuous sine wave sampled. This is how a
computer system would look upon the signal.
www.hig.no/~faouzi
26
Surfaces
NB. [X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X
and Y, which can be used to evaluate functions of two variables and three-
dimensional mesh/surface plots. The rows of the output array X are copies of the vector
x;columns of the output array Y are copies of the vector y.
27
Statistics Commands
mean(x) Mean value
median(x) Median value
var(x) Variance
std(x) or sqrt(var(x)) Standard deviation (the square root of the variance)
corrcoef(x,y) Correlation:
u=randn(1,10000000); hist(u,100) Normal distribution (with 100 intervals)
Note. Of course the mean value could be any value and width doesn’t need 1. Suppose that
we put two vertical lines at ±1 (standard deviation). This makes 68.3 % of the area. If we put
the vertical lines at ±1.96 the area becomes 95 % of the 28
37
area.
Statistics Commands
Exercise : Read two vectors and try some of these
statistics commands
>> X=1:5; Z=[ 0 1 1; 2 3 2];
% Calculate the mean and median value of X and Z.
>> mean(X), mean(Z)
% The result should become 3 and 1.0000 2.0000 1.5000.
>> median(X), median(Z)
% The result should be: 3 and 4.
% Standard deviation for the vectors.
>> std(X), std(Z)
% the answer is 1.5811 and 1.4142 1.4142 0.7071 respectively
% in the elements of vector Z. % Now let’s plot Z versus X. See figure below
29
Scripts
To create a script,
use the Matlab editor
or any text editor to
create a ”.m” file.
NB. You can use indenting to make reading loops, such as while/end
statements,easier.
30
Functions
31
Flow Control
32
Image Processing in Matlab IPTB
Outline
Images in MATLAB and the Image Processing Toolbox
How images are represented in MATLAB and the Image Processing Toolbox
Image Arithmetic
Adding, subtracting, multiplying, and dividing images
I2=histeq(I);
figure, imshow(I2)
figure, imhist(I2)
35
Basic Concepts...
Resizing
imresize changes the size of the image.
Rotation
imrotate rotates the image by given angle.
Cropping
imcrop extracts a rectangular part of the image.
36
Pixel Coordinate System
I(r, c)
37
Commonly-used Terminology
Neighbors of a pixel p=(i,j)
N8(p)={(i-1,j),(i+1,j),(i,j-1),(i,j+1),
N4(p)={(i-1,j),(i+1,j),(i,j-1),(i,j+1)}
(i-1,j-1),(i-1,j+1),(i+1,j-1),
(i+1,j+1)}
Adjacency
38
50
Image Types and Image classes
MATLAB IPTB defines 4 image types
Binary Logical array containing only 0s and 1s, interpreted as black and white,
(bi-level image) respectively.
Indexed Array of class logical, uint8, uint16, single, or double whose pixel values are
(pseudo-color image) direct indices into a colormap. The colormap is an m-by-3 array of class
double.
Gray-scale (intensity Array of class uint8, uint16, int16, single, or double whose pixel values specify
intensity values. For single or double arrays, values range from [0, 1].
image)
For uint8, values range from [0,255]. For uint16, values range from [0, 65535].
For int16, values range from [-32768, 32767].
True-color (RGB m-by-n-by-3 array of class uint8, uint16, single, or double whose pixel values
image ) specify intensity values.
For single or double arrays, values range from [0,1]. For uint8, values range
from [0, 255]. For uint16, values range from [0, 65535].
www.hig.no/~faouzi 39
Binary Images
Binary image ~ Logical Array of 0 & 1.
40
Indexed Images
Consists of an array “X” and a colormap “map” matrix. The pixel values
in “X” are direct indices into the colormap.
Each colormap matrix row (values in [0,1])
specifies the RGB components of a single color in the
image.
41
Gray-Scale Images
A gray-scale image is a data matrix whose values
represent intensities within some range.
The matrix can be of class uint8, uint16, int16, single, or double.
While grayscale images are rarely saved with a colormap, MATLAB
uses a colormap to display them.
www.hig.no/~faouzi
42
True-color Images
A true-color image is an image in which each pixel is specified by
three values: the red, green and blue components of the
pixel’s color.
For example, the red, green, and blue color components of the pixel
(10,5) are stored in I(10,5,1), I(10,5,2), and I(10,5,3), respectively.
43
True-color Images
44
True-Color Images
Try this:
RGB=reshape(ones(64,1)*reshape(jet(64),1,192),
[64,64,3]); R=RGB(:,:,1);
G=RGB(:,:,2);
B=RGB(:,:,3);
figure, imshow(RGB), title('True Color') figure, imshow(B),
title('Blue')
figure, imshow(G), title('Green') figure, imshow(R), title('Red')
45
Convert Between Image Types
46
60
Indexed Images
[x,map] = imread('trees.tif');
imshow(x,map)
imtool(x, map)
47
Convert Indexed to Intensity Images
image = ind2gray(x,map);
imtool(image)
48
Binary Images 1
1
0
1
0
0
0
0
0
0
1 1 1 0 0
0 1 1 0 0
BW = im2bw(image, 0.5);
% Where 0.5 is the threshold
imtool(BW);
49
Convert Between Image Classes
Converting between classes changes the way MATLAB and the
toolbox interpret the image data.
To be interpreted properly as image data, you need to rescale or offset the
data when you convert it.
To avoid problems, use one of these toolbox functions: im2uint8, im2uint16,
im2int16, im2single, or im2double.
Class converting problems:
Converting to a class that uses fewer bits to represent numbers, generally
implies some loss of the image information.
Not always possible to convert an indexed image from one storage class
to another.
50
Color Space Conversions
51
Image Arithmetic
MATLAB arithmetic operators and the IPTB arithmetic functions use
these rules for integer arithmetic:
Values that exceed the range of the integer type are saturated to that range.
Fractional values are rounded.
Therefore, avoid nesting calls to image arithmetic functions!
Example: calculate the average of two images
I1 = imread('rice.png');
I2 = imread('cameraman.tif');
K = imdivide(imadd(I1,I2), 2); % not recommended (Pb. with
truncations)
When used with uint8 or uint16 data, each arithmetic function rounds
and saturates its result before passing it on to the next operation. This
can significantly reduce the precision of the calculation.
K = imlincomb(.5,I1,.5,I2); % recommended (linear combination of images)
52
70
Image Arithmetic…
Applying an image arithmetic operation on two images A
and B or on a constant c and an image A:
Adding:
Imadd(A, B), imadd(A, c)
Subtracting
imsubtract(A, B), imsubtract(A, c)
Multiplying
Immultiply(A, B), immultiply(A, c)
Dividing
Imdivide(A, B), imdivide(A, c)
53
Working with Image Sequences
54
Working with Image Sequences
Example: load mri whos
mov = immovie(D,map);
movie(mov,3)
NB. immovie(D,map) returns the movie structure array mov from the images
in the multiframe indexed image X with the colormap map.
movie(mov,n) plays the movie n times.
55
Multidimensional Arrays as
Image Sequences
56
IPTB Functions for 4-D Arrays
75
57
IPTB Functions for 4-D Arrays
58
76
Example: Processing Image Sequences
Perform standard deviation filtering on each image in the sequence.
Note that, to use stdfilt with an image sequence, you must use the nhood
argument (logical array), specifying a 2-D neighborhood.
% Create an array of filenames that make up % Process sequence
the image sequence sequenceNew =
stdfilt(sequence,ones(3));
fileFolder = % View results
fullfile(matlabroot,'toolbox','images','imdemos')
figure;
;
for k = 1:numFrames
dirOutput = imshow(sequence(:,:,k));
dir(fullfile(fileFolder,'AT3_1m4_*.tif')); title(sprintf('Original Image # %d',k));
fileNames = {dirOutput.name}'; pause(1);
numFrames = numel(fileNames); imshow(sequenceNew(:,:,k),[]);
I = imread(fileNames{1}); title(sprintf('Processed Image #
%d',k));
% Preallocate the array
pause(1);
sequence = zeros([size(I) End
numFrames],class(I)); NB. ones(n) returns an n-by-n matrix of 1s.
sequence(:,:,1) = I;
% Create image sequence array
for p = 2:numFrames
sequence(:,:,p)
end = imread(fileNames{p}); 59
Multi-Frame Image Arrays
The IPTB includes two functions, immovie and montage, that work with
a specific type of multi-dimensional array called a multi-frame array.
In this array, images, called frames in this context, are concatenated
along the fourth dimension.
60
Multi-Frame Image Arrays
Use the cat command to create a multi-frame array. For example, to store a
group of images (A1, A2, A3, A4, and A5) in a single array.
A = cat(4, A1, A2, A3, A4, A5)
Note:
In a multi-frame image array, each image must be the same size and have the
same number of planes.
In a multi-frame indexed image, each image must also use the same color-map.
61
Conclusions
This was a short introduction to the main features
of MATLAB and the IPTB.
More can be found in:
The embedded help, help
http://www.mathworks.com
http://www.mathworks.com/matlabcentral
Google ...
62
80
Next lab sessions
63
Exercises
In the following, let A = [1 5 4; 2 7 8; 3 2 5] and B = [1 0 0; 0 4 2; 0 2 9]
be
matrices, C = [1 3 3] be a vector and D = [4 7 9 3 12 11 2 8 8] be data.
64
81
65