-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageFusionDemo.m
232 lines (185 loc) · 7.63 KB
/
imageFusionDemo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
function imageFusionDemo()
% function imageFusionDemo()
%
% This function provides the Image Fusion Demo.
%
% Image fusion is achieved with within-subject image registration. In
% this demonstration, the T1-weighted (T1w) scan of a subject is being
% fused with the corresponding T2-weighted (T2w) image.
%
% Under this scenario, the set up of the corresponding registration task
% consists of:
%
% 1. Designating the fixed and moving images
%
% fixed image <- the T2w image
%
% moving image <- the T1w image
%
% 2. Choosing the appropriate class of geometric transformations
%
% For the purpose of fusing two images from the same image, linear
% transformations are often adequate.
%
% 3. Choosing the appropriate similarity measure
%
% For the purpose of fusing two images with different contrasts, such
% as T1w and T2w, information-theoretic measures, such as mutual
% information, is typically superior to similarity measures that
% compare image intensities directly, such as the sum-of-squared-
% difference (SSD).
%
% This demo makes use of the linear image registration tool FLIRT
% shipped with FSL.
%
%
% Author: Gary Hui Zhang ([email protected])
%
%
%% set up FSL
FSLDIR = setupFSL();
%% change to the demo's Data folder and set up the data path
% remember the current folder
originalDIR = pwd();
% change to the Data folder
toDataDIR();
% set the original IXI data folder
IXIoriginalDIR = 'IXIoriginal';
% set the preprocessed IXI data folder
IXIpreprocessedDIR = 'IXIpreprocessed';
%% set up the directory for this demo
imageFusionDIR = 'imageFusion';
%% set up the subject ID of the IXI data
IXIsubjIDs = {'IXI002-Guys-0828', 'IXI025-Guys-0852'};
%% reslice the T1w image in the same voxel space as the T2w image
%
% this is to allow the mis-alignment between the two images, before the
% registration, to be visualised; this is otherwise not possible, because
% the two images have different voxel spaces (different voxel sizes and
% dimensions).
%
% this is achieved by applying the identity transformation, which is
% provided in the format compatible with FLIRT. in FLIRT, linear
% transformations are represented as 4-by-4 matrix and saved as a text
% file, with an optional suffix 'mat', which should not to be confused
% with the mat file used by Matlab.
%
% Also in FLIRT, fixed image is referred to as the reference image, with
% moving image as the input image.
%
% file name for the identity transformation
identityFilename = fullfile(FSLDIR, 'etc', 'flirtsch', 'ident.mat');
% for each subject
for i = 1:length(IXIsubjIDs)
% fixed image file name with full path
fixedImageFilename = fullfile(IXIoriginalDIR, [IXIsubjIDs{i} '-T2']);
% moving image file name with full path
movingImageFilename = fullfile(IXIpreprocessedDIR, [IXIsubjIDs{i} '-T1']);
% file name for the resliced moving image
outputImageFilename = fullfile(imageFusionDIR, [IXIsubjIDs{i} '-T1']);
% set up the command string to apply the identity transformation to
% the moving image
cmd = ['flirt -in ' movingImageFilename ' -ref ' fixedImageFilename ' -applyxfm -init ' identityFilename ' -out ' outputImageFilename ' -v '];
% print out the command string
disp(cmd);
% execute the command
unix(cmd);
end
%% estimate the transformation that aligns the moving image to the fixed
%
% the central task of image registration is to find the geometric
% transformation that best aligns the moving image to the fixed image, as
% deemed by some choice similarity measure chosen to assess the quality of
% alignment.
%
% in this demo, we will consider first the rigid transformations, which has
% a degree of freedom (d.o.f.) 6.
%
% in FLIRT, this is specified by specifying the d.o.f.
dof = '6';
%
% in FLIRT, the similarity measures are known as the cost. The
% default choice is correlation ratio (corratio).
%
% For this demo, we will make use of mutual information.
similarity = 'mutualinfo';
% for each subject
for i = 1:length(IXIsubjIDs)
% fixed image file name with full path
fixedImageFilename = fullfile(IXIoriginalDIR, [IXIsubjIDs{i} '-T2']);
% moving image file name with full path
movingImageFilename = fullfile(IXIpreprocessedDIR, [IXIsubjIDs{i} '-T1']);
% file name for the estimated linear transformation
transformFilename = fullfile(imageFusionDIR, [IXIsubjIDs{i} '-T1toT2.mat']);
% set up the command string to execute the registration
cmd = ['flirt -in ' movingImageFilename ' -ref ' fixedImageFilename ' -cost ' similarity ' -searchcost ' similarity ' -dof ' dof ' -omat ' transformFilename ' -v '];
% print out the command string
disp(cmd);
% execute the command
unix(cmd);
end
%% transform the moving image with the estimated transformation
%
% the resulting moving image should now be aligned with the fixed image,
% achieving image fusion, i.e., at each voxel, representing some anatomical
% location, the corresponding T1w and T2w intensity values can be found.
%
for i = 1:length(IXIsubjIDs)
% fixed image file name with full path
fixedImageFilename = fullfile(IXIoriginalDIR, [IXIsubjIDs{i} '-T2']);
% moving image file name with full path
movingImageFilename = fullfile(IXIpreprocessedDIR, [IXIsubjIDs{i} '-T1']);
% file name for the estimated linear transformation
transformFilename = fullfile(imageFusionDIR, [IXIsubjIDs{i} '-T1toT2.mat']);
% file name for the transformed moving image
outputFilename = fullfile(imageFusionDIR, [IXIsubjIDs{i} '-T1toT2']);
% set up the command string to transform the moving image
cmd = ['flirt -in ' movingImageFilename ' -ref ' fixedImageFilename ' -applyxfm -init ' transformFilename ' -out ' outputFilename];
% print out the command string
disp(cmd);
% execute the command
unix(cmd);
end
%% visualise the geometric transformation with grid volumes
%
% grid volumes can be used to visualise the characteristic of geometric
% transformations. it works by creating grid volumes that match the voxel
% space of the moving image and transform the grid volumes with the
% transformation estimated to align the moving image to the fixed.
%
% in this demo, we will use it to visualise the rigid transformation
% produced to fuse the IXI subject - IXI002-Guys-0828.
%
% fixed image file name with full path
fixedImageFilename = fullfile(IXIoriginalDIR, [IXIsubjIDs{1} '-T2']);
% moving image file name with full path
movingImageFilename = fullfile(IXIpreprocessedDIR, [IXIsubjIDs{1} '-T1']);
% create the grid volumes that match the voxel space of the moving image
createGrids(movingImageFilename);
% there are three grid volumes - one for visualising the effect of a
% transformation along one of the three orthogonal planes:
%
% 1. ${prefix}-gridx.nii.gz - sagittal plane
%
% 2. ${prefix}-gridy.nii.gz - coronal plane
%
% 3. ${prefix}-gridz.nii.gz - axial plane
%
% in this demo, as an example, we will look along the sagittal plane, which
% turns out to be the most interesting for this subject
%
gridImageFilename = fullfile('grids', [IXIsubjIDs{1} '-T1-gridx']);
% file name for the estimated linear transformation
transformFilename = fullfile(imageFusionDIR, [IXIsubjIDs{1} '-T1toT2.mat']);
% file name for the transformed moving image
outputFilename = fullfile(imageFusionDIR, [IXIsubjIDs{1} '-T1toT2-gridx']);
% set up the command string to transform the moving image
cmd = ['flirt -in ' gridImageFilename ' -ref ' fixedImageFilename ' -applyxfm -init ' transformFilename ' -out ' outputFilename];
% print out the command string
disp(cmd);
% execute the command
unix(cmd);
%% back to the original folder
cd(originalDIR);
%% end of function
end