DIP Lab Intro | PDF | Matrix (Mathematics) | Matlab
0% found this document useful (0 votes)
55 views

DIP Lab Intro

This document provides an introduction and outline for image processing in MATLAB. It discusses key topics such as coordinate systems, image types, image arithmetic, and working with image sequences. MATLAB is introduced as an environment suited for working with arrays and matrices that can be used for image processing through various toolboxes and functions. The document outlines important concepts like defining variables as arrays and matrices, performing elementary operations and functions, and using graphics capabilities for displaying image data.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

DIP Lab Intro

This document provides an introduction and outline for image processing in MATLAB. It discusses key topics such as coordinate systems, image types, image arithmetic, and working with image sequences. MATLAB is introduced as an environment suited for working with arrays and matrices that can be used for image processing through various toolboxes and functions. The document outlines important concepts like defining variables as arrays and matrices, performing elementary operations and functions, and using graphics capabilities for displaying image data.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 65

Introduction to Image

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.

 It integrates: computing, programming and visualization.

 Visit http://www.mathworks.com for more on MATLAB.

 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”.

 GUI’s (Graphical User Interface) are available for specific tasks.


3
Introduction
 Quick help can be obtained in MATLAB:
help plot, lookfor plot, doc plot, helpwin,
helpdesk.

 Try type ‘doc mean’.

 To get an overview of what you can do using


MATLAB type: demos

4
MATLAB Environment

5
How to Define Variables?

 In Matlab, variables are arrays.


 Try who (list of variables in alphabetical order), whos x (with their sizes and Types)

 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.

 We will be mostly interested in defining:


 Vectors
 Matrices
 Multidimentional Arrays

6
How to Define a Vector?

 Typing a=[1,2,3,4]; or a=[1 2 3 4]; defines a row vector a=[1 2 3


4]
(with 4 elements).

 Typing: a=[1;2;3;4]; defines a column vector

 You can also use the transpose operation: a=[1;2;3;4]’.


 Type t=[0:.2:1] or t=0:.2:1. Defines a row vector t whose elements
range from 0 to 1 and are spaced by 0.2.
 To get rid of a, write clear a. (clear all – clears all variables)

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 also13be 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’

 eye(3) 3×3 identity matrix. 11


 eye(4,5) 4×4 identity plus a column
A  of
2 zeros;
 zeros(3,2) 3×2 matrix padded with zeros;
 ones(3,2) 3×2 matrix of ones;
 randn(n,m) normally distributed n×m 12 matrix;
 rand(n,m) uniformly distributed n×m matrix;
 diag([1,2]) diagonal matrix:  3
1 0
ans  
0 2
 Setting columns or rows = [] is an effective
50 way to delete
them, e.g. A(2,:)= []
1
A 11
 
 Type help gallery for a list of predefined 3
10
matrices.
50
Managing Matrices
 [m,n] = size(A) returns the number of rows and cols of A.
 size(A,1) returns the number of elements in each rows of A. returns the
 size(A,2) number of elements in each cols of A.
 [Y,I] = max(A) returns for each row of A the value of the higher elements; I is the
indices of the corresponding element.
 max(max(A)) returns the value of the higher element of A. For vectors, max(v)
returns the maximal element.
 [Y,I] = returns for each row of A the value of the lower elements; I is the
min(A) indices of the corresponding element.
 Y = sort(A) sorts the cols of A in ascending order. See help sort for more
 find(A == 1) info. returns the linear indices of elements =1.

• 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.

X^p is X to the power p, if p is an integer, X^p is equivalent to X * X * X … X for p-


times.. X must be a square matrix. If the integer is negative, X is inverted first.
X.^p is equivalent to A(I,j)p.

15
Elementary Matrix Operations

16
Relational Operations

need to have the same size.

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.

 In the Matlab Editor,


select all the text and
try ”Smart Indent”.

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 Types in the Toolbox


 Fundamental image types supported by the Image Processing Toolbox

Converting Between Images


 Converting between the image types
 Converting Between Image Classes
 Converting image data from one class to another

Image Arithmetic
 Adding, subtracting, multiplying, and dividing images

Working with Image Sequences


33
Image Processing in Matlab IPTB...
 The basic data structure in MATLAB is the array, an
ordered set of real or complex elements. This object is
naturally suited to the representation of images, real-
valued ordered sets of color or intensity data.

 MATLAB stores most images as two-dimensional arrays


(i.e., matrices), in which each element of the matrix
corresponds to a single pixel in the displayed image.
(Pixel is derived from picture element and usually
denotes a single dot on a computer display.)
34
Basic Concepts
 Read an image: I = imread('pout.tif');
 Display an image: imshow(I), imtool(I)
 Try: whos I, imfinfo('pout.tif'), impixelinfo
 Try these commands:
 figure, imhist(I)

 I2=histeq(I);

 figure, imshow(I2)

 figure, imhist(I2)

 imwrite (I2, 'pout2.png');

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)

For example, the I(2,15) returns the value of the


pixel at row 2, column 15 of the image I.

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

4-adjacency: p, q are 4-adjacent if p is in the set N4(q) 8-

adjacency: p, q are 8-adjacent if p is in the set N8(q)

Note that if p is in N4/8(q), then q must be also in N4/8(p)

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.

Red Green Blue

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.

 MATLAB stores true-color images as an m-by-n-by-3 data


array, with no color map.

 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.

 A true-color array can be of class uint8, uint16, single, or double.

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

 The Image Processing Toolbox represents colors


as RGB values in both True-color and indexed
images.

 The toolbox provides functions to convert between color


spaces.

 The image processing functions themselves assume all


color data is RGB.

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

 Image sequences are collections of images related:


 by time: frames in a movie,
 or by view (spatial location): such as MRI slices.
 An m-by-n-by-p array can store p, 2-D grayscale or binary
images.
 An m-by-n-by-3-by-p array can store p, true-color images.

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

 Many toolbox functions can operate on multi-dimensional


arrays and, consequently, can operate on image sequences.

 For example, if you pass a


multi-dimensional array to the imtransform
function, it applies the same 2-D transformation to all
2-D planes along the higher dimension.

 Some functions, however, do not by default interpret an m-by-n-by-p


or an m-by-n-by-3-by-p array as an image sequence.

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.

 Multi-frame arrays are either:


 m-by-n-by-1-by-p, for grayscale, binary, or indexed images,
 m-by-n-by-3-by-p, for true-color images, where p is the number of frames.

 For example, a multi-frame array containing 5, 480-by-640 grayscale or


indexed images would be 480-by-640-by-1-by-5. An array with 5, 480-
by- 640 truecolor images would be 480-by-640-by-3-by-5.

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)

 To extract frames from a multi-frame image MULTI:


 FRM3 = MULTI(:,:,:,3)

 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

• Lab1- Arithmetic Operations Between Images, Geometric


Transformations And Gray Level Functions
• Lab 2- Histogram Equalization, Thresholding Methods
And Spatial Filtering To Remove The Noise
• Lab 3- Fourier Transform

• Lab 4- Segmentations Tools

• Lab 5- Edge Detection

• Lab 6- Mathematical Morphology

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.

1.Compute the mean of data D.


2.Compute the inverse of A and of B.
3.Compute the transpose of [A + B].
4.Add the (2,3) component of A to the (3,1) component of B.
5.Compute the matrix product A * B.
6.Compute the determinant of A.
7. Plot 3.5 sin(1.6 x) for (-5 < x < +5).

64
81
65

You might also like