Download presentation
Presentation is loading. Please wait.
Published byDomenic Stevenson Modified over 9 years ago
1
Computers in Medicine Bone visualization and analyses - An introduction to Matlab - Presented by: Kathryn Stok ETH Zurich kas@ethz.ch Prepared by: Steven Boyd University of Calgary
2
Objectives (I) Provide an overview of Matlab – What is Matlab? – Basic commands – Accessing online help – Writing M-files – Programming style – Create plots
3
Objectives (II) Work through examples – Reading an image file – Probe the data – Filtering – Create a histogram – Thresholding – Counting voxels – Calculating 2nd moment of area
4
Why bone visualization? Carries the body → it should be strong But in many cases, bone strength is impaired. – congenital deformities – osteoporosis – fracture – fatigue (stress fracture)
5
Bone architecture
6
Bone structure – femoral head Young Normal Osteoporotic
7
Bone structure – lumbar spine Young Normal Osteoporotic
8
Why bone visualization? Bone (structure) analyses and visualization are of great importance in – Diagnosis – Treatment – Prevention
9
Distal radius 36 yr male64 yr female
10
Why Matlab? High-performance language for technical computing. – Matrix Laboratory Advantages – Easy to use: –No dimensioning required. –No compiling –Good for prototyping, testing – Functionality is greatly expanded by toolboxes. – Many fields of application. Disadvantages: – Uses much memory (slow for large applications) – Not good for final software design
11
What is matlab? Matlab language – matrix/array language – control flow (if, for, while) – functions Working environment – workspace for computing, importing, and exporting data – text editor for creating M-files
12
This Matlab course It covers: Using the development environment. Syntax of the language. Basic matrix operations. Basic graphics features. Writing Matlab scripts and functions (m-files) It does not cover: Any of the toolboxes including Simulink. c-mex and f-mex files, Matlab compilers.
13
Possibilities… Develop tools for: – Image processing and basic analysis. – Visualization: 2D and 3D. – Simulation: Simulate bone loss.
14
Matlab Environment
15
Matrices Albrecht Dürer Magic square
16
Matrices semicolon Entering matrices – explicitly
17
Matrices Sum, transpose
18
Matrices Diagonal (diag)
19
Matrices Subscripts: A(i,j) row column
20
Matrices Colon operator ' : '
21
Matrices Colon operator ' : ' Matlab is 1-based
22
Matrix multiplication ( * )
23
Matrix multiplication (.* )
24
Variables Scalar, vector, matrix
25
Variables Check workspace – command line – window
26
Numbers Integer Real Exponential Imaginary -3 0.0001 1.60210e-20 5 + 2i
27
Operators: Arithmetic +Addition -Subtraction *Multiplication (or.* element-wise) /Division ^Power (or.^ element-wise) ' Transpose ()Brackets for evaluation order A = [ 4 1 3] A.^2 = [16 1 9] A.*2 = [ 8 2 6]
28
Operators: Relational <Less than <=Less than or equal to >Greater than >=Greater than or equal to ==Equal to ~=Not equal to
29
Operators: Logical &AND |OR ~NOT Type help ops to see: the arithmetic,relational and logical functions.
30
Matrices Entering matrices – explicitly – load from external file – your own M-files – built-in function Albrecht Dürer
31
Generating matrices Load data file
32
Generating matrices M-files
33
Generating matrices Built-in functions
34
Help Help Window – pop-up window Type help
35
Online help
38
Useful commands (up arrow) – recall previous command – p recalls previous command starting with ‘p’ clear – clear workspace – clear A Z Q clears only A, Z, and Q. size – size(A) returns size of matrix A
39
Multidimensional matrices page k, Z row i, X column j, Y
40
Multidimensional matrices Generate a 3D array manually
41
Advanced indexing Matrices are always stored as columns – subscripts (i,j,k) – array dimension [d 1 d 2 d 3 ] – if A is of dimension 3 x 3 x 3 then A(3,3,3) = A(27) = 8 3 2 7 5 1 4 6 8 6 2 6 1 4 9 3 6 7 5 6 7 9 5 6 1 2 8 3 5 4 2 1 6 7 1 8 3 5 4 2 1 6 7 1 8 6 1 3 2 4 6 6 9 7 6 1 3 2 4 6 6 9 7 5 9 1 6 5 2 7 6 8 5 9 1 6 5 2 7 6 8 i j k
42
Advanced indexing Matrices are always stored as columns – subscripts (i,j,k) – array dimension [d 1 d 2 d 3 ] – if A is of dimension 3 x 3 x 3 then A(3,3,3) = A(27) = 8 offset = i + (j-1)(d 1 ) + (k-1)(d 2 )(d 1 ) 3 2 7 5 1 4 6 8 6 2 6 1 4 9 3 6 7 5 6 7 9 5 6 1 2 8 3 5 4 2 1 6 7 1 8 3 5 4 2 1 6 7 1 8 6 1 3 2 4 6 6 9 7 6 1 3 2 4 6 6 9 7 5 9 1 6 5 2 7 6 8 5 9 1 6 5 2 7 6 8 i j k
43
Characters and Text s = ' myimage ' – 7 character array (string) num2str() - convert a number to string
44
User input Keyboard input a_number = input('Enter number'); a_string = input('Enter filename', 's'); [filename,path] = uigetfile('*.*'); Using GUI
45
Graphics: plot
46
Graphics: plot(s) ‘hold on’ allows more plots to be added
47
Graphics: contours
48
Graphics: subplots
49
Graphics: labels and titles
50
… or use the Figure window !
51
Graphics: mesh
52
Flow control if, else, and elseif switch while for
53
if, else, and elseif if logical_expression statements end if (rem(a,2) == 0) disp(‘a is even’) end if (n<0) disp(‘Input must be positive’) elseif (rem(n,2) == 0) disp(‘a is even’) else disp(‘a is odd’) end
54
switch switch expression case value1 statements case value2 statements otherwise statements end switch (input_num) case (-1) disp(‘negative one’) case (0) disp(‘zero’) case (1) disp(‘positive one’) otherwise disp(‘othervalue’) end
55
while, for while expression statements end n = 1; while (prod(1:n) < 1e10) n = n + 1; end for index = start:inc:end statements end for i = 2:6 x(i) = 2*x(i-1); end for i = 1:m for j = 1:n A(i,j) = 1/(i + j - 1); end
56
Vectorization Efficiency! – Example: Table of logarithms –Find log10 of numbers between 0 and 10. x = 0; for k = 1:1001 y(k) = log10(x); x = x + 0.01; end x = 0:0.01:10; y = log10(x); Looped codeVectorized code AVOID LOOPS!
57
Scripts and functions Scripts – No input arguments nor output arguments. – Operate on data in the workspace – Useful for automating a series of steps that will be performed many times Functions – Can accept input arguments and return output arguments – Variables are local to the function – Useful for extending the MATLAB language beyond the built-in functions
58
Script example Comment lines Computations Graphics output Create m-files: – type edit – use the interface
59
Script example Hit return to advance plot
60
Script Example
61
Function example Function definition Help comments Error check Calculations function [y] = average(x) keyword output argument function name input argument
62
Function example semi-colon
63
M-files Tips: – use descriptive variable names –e.g. number_students = 20 – functions end with “_fcn” –e.g. an_example_fcn(number_students) – indent if, for, while statements 3-4 spaces – add comments using ‘%’ – develop with smaller portions of data – debug by pasting line-by-line from M-file
64
Online tutorial http://www.imrtweb.ethz.ch/matlab/ Or google “mathworks tutorial” Fundamentals Exercises Examples
65
Part II: Practical Programming Image analysis on 2D section – Read image data (micro-CT) – Plot the image – Examine image – Filter the image – Create a histogram – Threshold (distinguish between bone and marrow) – Calculate porosity – Centroid, principal axes
66
Read 2D Image Want to import the image from micro-CT Write a program to: – clear workspace – set filename – load file – Image data to visualize: /Phalanx/hp2d_34-95_380.bmp
67
Set up main program (a script file) comments clear workspace close all figure windows file to load load it Note: You need to write the 2D image reading function.
68
Read in 2D image Read the file Convert data
69
Display input data
72
Examine image min, max, mean
73
Examine image min, max, mean
74
Image Processing Filter Thresholding – histogram Porosity = 1 - (bone area / total area) Moment of area – First moment of area – Centroid – Second moment of area
75
Filtering – example 1
76
Filtering – example 2 filtered original imagefiltered noisy image original imagenoisy image
77
Filtering the data Apply Gaussian filtration – Shape of filter depends on: window size [n1 n2] and
78
Filtering Window= 3 = 1.2 Filter
79
Filtering Window= 5 = 1.2 Filter
80
Filtering Window= 15 = 1.2 Filter
81
Filtering Window= 15 = 7 Filter
82
Filtering Window= 15 = 100 Filter
83
Filtering: Convolution
84
Filtering: Edge Effects edge data nulled Important to begin with image dimensions large enough to account for edge effects
85
Filtering Use built-in functions: – fspecial – imfilter – (smooth3)
86
Thresholding Good to try different thresholds to see the effect. Segmentation of bone – divides bone from other tissue
87
Thresholding threshold2D = zeros matrix Find all indices (i,j) where element(i,j) > threshold threshold2D(i,j) = 1
88
Thresholding
89
Threshold = 90Threshold = 120
90
Histogram – lookfor and help
91
Histogram hist works on vectors, not 2D matrices – need reshape function to vectorize
92
Porosity Porosity = 1 - (Bone volume divided by total volume) Need total bone volume (area) – Create mask: –filter image to remove holes –threshold filtered image – Count total “mask” pixels – Count total “bone” pixels within mask
93
Porosity Create mask: original image filtered image threshold filtered image Image 3 super-imposed on image 1 123
94
Porosity Porosity = 1 - Mask pixels Bone pixels within mask Thresholded image (bone) Mask image Image of bone within the mask
95
Image analysis – determine porosity Original Segmenting the bone FilteredThresholded Creating the mask FilteredThresholded Find bone within mask Porosity = 1 - Area bone Area mask
96
Erosion, dilation ErodeErode = 2 Erode = 4 DilateDilate = 2 Dilate = 4
97
Part of MAIN program
98
Stress in Pure Bending Mechanical stiffness – First moment of area – Centroid of area – Second moment of area
99
First moment of an area First moment of area with respect to the X axis: First moment of area with respect to the Y axis: Can be positive, negative, or zero depending on the axis location (mm 3 )
100
Centroid of an area Centroid of area defined as: Knowing the first moment:
101
First moment of area
102
Second moment of area Stress resulting from bending: σ = My/I Resistance against bending. – Take I x, I y, I xy about centroid C – Or use the parallel axis theorem: I x = I xc + Ad 2
103
Second moment of area - rotation Transformation of 2nd moments:
104
Second moment of area - principal axes Two values of for which I xy = 0. Can calculate the max/min 2nd moments: maximum associated with I x minimum associated with I y
105
Principal axes & Transformation
106
3D: Fracture healing rightleft
107
2D: Pompe Disease RadiusTibia
108
Mouse knees NTRTCTRL
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.