BIOENG 1330/2330 Biomedical Imaging FALL 2015: Sowmya Aggarwal Ker-Jiun Wang University of Pittsburgh
BIOENG 1330/2330 Biomedical Imaging FALL 2015: Sowmya Aggarwal Ker-Jiun Wang University of Pittsburgh
BIOMEDICAL IMAGING
FALL 2015
Lecture 4
09/10/2015
Sowmya Aggarwal
Ker-Jiun Wang
University of Pittsburgh
1
Briefly cover the following
- Matrix/Vector Algebra
- Gaussian Elimination
- Curve Fitting
2
Program Flow Control in MATLAB
1) Logical Operator
Conditional Mathematical Symbol MATLAB Symbol
Relational Operators
Equal = ==
Not equal ≠ ~=
Less than < <
Greater than > >
Less than or equal ≤ <=
Greater than or equal ≥ >=
Logical operators
And AND & or &&
Or OR | or||
Not NOT ~
3
Program Flow Control in MATLAB - Branching – If, for, while, switch
4
Program Flow Control in MATLAB - Branching – If, for, while, switch
5
Program Flow Control in MATLAB - Branching – If, for, while, switch
6
Program Flow Control in MATLAB - Branching – If, for, while, switch
7
Parallel Programming Example - Parfor
Compare with
‘for’ using tic
toc before and
after the loop
Exit loops early – put ‘break’ command within the loop to get out of ‘for’ or ‘while’ loops
break
8
Briefly cover the following
- Matrix/Vector Algebra
- Gaussian Elimination
- Curve Fitting
9
Scalar
• A quantity (variable), described by a single real
number
Image Histogram (Single MRI Slice)
Counts
voxel in an MRI scan
Intensity
Voxel
Linear Algebra & Matrices, MfD 2010
Vector
• Not a physics vector (magnitude, direction)
Intensity Plot (for the same (y,z) coordinates)
EXAMPLE:
Intensity of voxel
Slice # along x - axis
x1
x 2
Then:
VECTOR= A “column” of numbers
representing intensity values along
X-axis xn
3D MRI Image (T1
Linear Space)& Matrices, MfD 2010
Algebra
Matrix Definition – MATLAB Conventions
3) Diagonal Matrix, 4 x 4
(Square Matrix)
MATLAB
A = [1 0 0 0;0 6 0 0;0 0 11 0; 0 0 0 16];
4) Identity Matrix
MATLAB
A = [1 0 0 0;0 1 0 0;0 0 1 0; 0 0 0 1];
13
Matrix Algebra
14
Matrix Algebra
1
A-1 = (adjoint of A)
det 𝐴
A-1A = AA-1 =1
A x = b
15
Reference material: Numerical Recipes in C – The Art of Scientific Computing, William H Press et. el.
Matrix Algebra
1
A-1 = (adjoint of A)
det 𝐴
A-1A = AA-1 =1
A x = b
16
Reference material: Numerical Recipes in C – The Art of Scientific Computing, William H Press et. el.
Matrix Algebra
1
A-1 = (adjoint of A)
det 𝐴
A-1A = AA-1 =1
A x = b
17
Reference material: Numerical Recipes in C – The Art of Scientific Computing, William H Press et. el.
Matrix Algebra
1
A-1 = (adjoint of A)
det 𝐴
A-1A = AA-1 =1
A x = b
18
Reference material: Numerical Recipes in C – The Art of Scientific Computing, William H Press et. el.
19
20
21
MATLAB Function Description
det(A) Computes determinant of a square matrix, A
- Matrix/Vector Algebra
- Gaussian Elimination
- Curve Fitting
23
Matrix Algebra – Solving a System of Equations, Ax = b
24
Matrix Algebra – Solving a System of Equations, Ax = b
25
Matrix Algebra – Solving a System of Equations, Ax = b
26
Matrix Algebra – Solving a System of Equations, Ax = b
Back
Substitution
can begin
here
27
Matrix Algebra – Solving a System of Equations, Ax = b
Gauss
Reduced Jordanian
Row
Echelon form
28
Matrix Algebra – Solving a System of Equations, Ax = b
- Matrix/Vector Algebra
- Gaussian Elimination
- Curve Fitting
31
Curve Fitting – Interactive
Approximate a function (eg. polynomial/spline) that explains the data
Steps:
1) Load some data at the MATLAB® command line.
load hahn1
Useful Tip: Avoid overfitting
2) Open the Curve Fitting app. Enter:
cftool
4) Choose a different model type using the fit category drop-down list, e.g., select Polynomial, Fourier.
Create a fit using the fit function, specifying the variables and a model type (in this case rat23 is the model type).
f = fit( temp, thermex, 'rat23' )
37
Curve Fitting – Command line
MATLAB FUNCTION USAGE Where
COMMAND
Function
y = polyval(a,xnew)
38
39
See this on slideshow
Reference: Wolfram 40
Curve Fitting – Approximate a function (eg. polynomial/spline) that explains the data
MATLAB FUNCTION USAGE Where
COMMAND
41
Curve Fitting – Approximate a function (eg. polynomial/spline) that explains the data
MATLAB FUNCTION USAGE Where
COMMAND
42
MATLAB Demonstration: Apply Laplace Filter to an Image
MATLAB FUNCTION USA Where
COMMAND
GE
fft2 Determines Fourier transform F = fft2(y) y is the 2D real function
ifft2 Determines inverse Fourier y = ifft2(F) F is the fourier transform of y
%apply filter
tic
g = f - imfilter(f, kernel, 'replicate');
toc
%view results
figure, imshowpair(f,g,'montage');
43
44
% Now Apply Transforms to images in Frequency Domain
% fourier transform of the image
padsize= =[546 368];
F = fft2(f,padsize(1),padsize(2));
% convolution process
tic
G = F .* H;
toc
g = real(ifft2(G));
figure, imshowpair(f,g,'montage');
45
Time elapsed in spatial convolution
- 0.195813 seconds
- 0.012137 seconds
Why?
46
Convolution
in Spatial Domain
Convolution
in Frequency
Domain
47
HMM (Hidden Markov model)
48
Generating a hidden state sequence by MHH
model
Observable state: x = x1……xN, Hidden state: = 1, ……, N
Given a HMM, we can generate a sequence of hidden states of length n as
follows:
1 1 1 … 1
1. Start at state 1 according to prob a01 a02 2 2 2 … 2
2. Emit observable state x1 according to 0 … … … …
prob e1(x1) K K K … K
4. … until emitting xn x1 x2 x3 xn
49
Likelihood of a Hidden States Sequence
1 1 1 … 1
Given an observable state sequence x = x1……xN
and a hidden state sequence = 1, ……, N, 2 2 2 … 2
… … … …
K K K … K
To find how likely this scenario is:
(given our HMM)
x1 x2 x3 xK
P(x, ) = P(x1, …, xN, 1, ……, N)
= P(xN | N) P(N | N-1) ……P(x2 | 2) P(2 | 1) P(x1 | 1) P(1)
= a01 a12……aN-1N e1(x1)……eN(xN)
50
The Viterbi Algorithm 1 1 1 1
…
GIVEN x = x1x2……xN 2 2 2 … 2
to maximize P[ x, ] K K K … K
x1 x2 x3 xK
* = argmax P[ x, ]
Maximizes a01 e1(x1) a12……aN-1N eN(xN) Given that we end up in
state k at step i,
maximize product to the
Dynamic Programming! left and right
Vk(i) = max{1… i-1} P[x1…xi-1, 1, …, i-1, xi, i = k]
= Prob. of most likely sequence of states ending at state
i = k
51
The Viterbi Algorithm 1 1 1 1
…
Inductive assumption: 2 2 2 … 2
Given that for all states k, and for a fixed position i, … … … …
Vk(i) = max{1… i-1} P[x1…xi-1, 1, …, i-1, xi, i = k] K K K K
…
What is Vl(i+1)? x1 x2 x3 xK
From definition,
Given that we end up in
Vl(i+1) = max{1… i}P[ x1…xi, 1, …, i, xi+1, i+1 = l ] state k at step i,
= max{1… i}P(xi+1, i+1 = l | x1…xi, 1,…, i) P[x1…xi, 1,…, i] maximize product to the
left and right
= max{1… i}P(xi+1, i+1 = l | i ) P[x1…xi-1, 1, …, i-1, xi, i]
= maxk [P(xi+1, i+1 = l | i=k) max{1… i-1}P[x1…xi-1,1,…,i-1, xi,i=k]]
= maxk [ P(xi+1 | i+1 = l ) P(i+1 = l | i=k) Vk(i) ]
= el(xi+1) maxk akl Vk(i)
52
The Viterbi Algorithm 1 1 1 1
…
Input: x = x1……xN
2 2 2 … 2
Initialization:
V0(0) = 1 (0 is the imaginary first position) 0 … … … …
Vk(0) = 0, for all k > 0 K K K … K
Iteration: x1 x2 x3 xK
Traceback:
N* = argmaxk Vk(N)
i-1* = Ptri (i) 53
The Viterbi Algorithm
Similar to “aligning” a set of states to a sequence
x1 x2 x3 ………………………………………..xN
Computational complexity:
State 1
Time: Space: 2
O(K2N) O(KN) Vj(i)
K
Underflows are a significant problem
P[ x1,…., xi, 1, …, i ] = a01 a12……ai e1(x1)……ei(xi)
These numbers become extremely small – underflow
Solution: Take the logs of all values
[seq,states] = hmmgenerate(100,trans,emis);
estimatedStates = hmmviterbi(seq,trans,emis);
[seq,states] = ...
hmmgenerate(100,trans,emis,...
'Statenames',{'fair';'loaded'});
estimatesStates = ...
hmmviterbi(seq,trans,emis,...
'Statenames',{'fair';'loaded'});
56
Gram-Schmidt procedure
57
Gram-Schmidt procedure
•step1.a
•step1.b
•step2.a
•step2.b
•step3.a
•step3.b
•etc.
58
Gram-Schmidt procedure
• For we have
60
61