-
Notifications
You must be signed in to change notification settings - Fork 2
/
bisLinearImageRegistration.cpp
332 lines (253 loc) · 11.5 KB
/
bisLinearImageRegistration.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* License
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._ It is released under the terms of the GPL v2.
----
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
See also http: www.gnu.org/licenses/gpl.html
If this software is modified please retain this statement and add a notice
that it had been modified (and by whom).
Endlicense */
#include "bisLinearImageRegistration.h"
#include "bisImageAlgorithms.h"
#include "sstream"
#include <time.h>
bisLinearImageRegistration::bisLinearImageRegistration(std::string s) : bisAbstractImageRegistration(s)
{
this->class_name="bisLinearImageRegistration";
std::string n=this->name+":internal";
std::unique_ptr<bisLinearTransformation> tmp(new bisLinearTransformation(n));
this->internalTransformation=std::move(tmp);
this->internalTransformation->identity();
std::string n2=this->name+":initial";
std::unique_ptr<bisMatrixTransformation> tmpM(new bisMatrixTransformation(n2));
this->initialTransformation=std::move(tmpM);
this->initialTransformation->identity();
}
bisLinearImageRegistration::~bisLinearImageRegistration()
{
}
bisSimpleMatrix<float>* bisLinearImageRegistration::getOutputMatrix()
{
return this->internalTransformation->getSimpleMatrix("lin_reg_matrix");
}
bisSimpleVector<float>* bisLinearImageRegistration::getTransformationParameterVector()
{
bisSimpleVector<float>* output=new bisSimpleVector<float>("lin_reg_vector");
output->allocate(29);
bisUtil::mat44 m;
this->internalTransformation->getMatrix(m);
int index=0;
for (int ia=0;ia<=3;ia++) {
for (int ib=0;ib<=3;ib++) {
output->getData()[index]=m[ia][ib];
index=index+1;
}
}
std::vector<float> p(12);
this->internalTransformation->storeParameterVector(p,1);
for (int ia=0;ia<=11;ia++)
output->getData()[ia+16]=p[ia];
output->getData()[28]=(float)this->internalTransformation->getMode();
return output;
}
void bisLinearImageRegistration::setInitialTransformation(bisMatrixTransformation* initial)
{
bisUtil::mat44 m; initial->getMatrix(m);
this->initialTransformation->setMatrix(m);
}
// Optimizer Stuff
float bisLinearImageRegistration::computeValue(std::vector<float>& position)
{
this->internalTransformation->setParameterVector(position,1);
bisImageAlgorithms::resliceImage(this->level_target.get(),this->temp_target.get(),this->internalTransformation.get(),1,0.0);
short* weight1_ptr=0,*weight2_ptr=0;
if (this->use_weights>0)
{
weight1_ptr=this->level_reference_weight->getImageData();
if (this->use_weights==2)
{
bisImageAlgorithms::resliceImage(this->level_target_weight.get(),this->temp_target_weight.get(),this->internalTransformation.get(),1,0.0);
weight2_ptr=this->temp_target_weight->getImageData();
}
}
this->internalHistogram->weightedFillHistogram(this->level_reference->getImageData(),
this->temp_target->getImageData(),
weight1_ptr,
weight2_ptr,
use_weights,
1.0,
1, // reset
this->level_dimensions,
this->level_bounds);
float mv=(float)this->internalHistogram->computeMetric(this->metric);
/* count=count+1;
if (count>=13 && count<=20)
{
std::vector<float> p(12);
this->internalTransformation->storeParameterVector(p,1);
bisUtil::mat44 m; this->internalTransformation->getMatrix(m);
std::cout << count << " [";
for (int j=0;j<p.size();j++)
std::cout << p[j] << " ";
std::cout << "] [";
for (int row=0;row<=2;row++) {
for (int col=0;col<=3;col++) {
std::cout << m[row][col] << " ";
}
}
std::cout << "] = " << mv << std::endl;
}*/
return mv;
}
float bisLinearImageRegistration::computeGradient(std::vector<float>& params,std::vector<float>& grad)
{
int numdof=this->internalTransformation->getNumberOfDOF();
// std::cout << "sizes=" << params.size() << " " << grad.size() << " dof=" << numdof << std::endl;
for (int i=0;i<numdof;i++)
grad[i]=0.0;
float GradientNorm = 0.000001f;
for (int i=0;i<numdof;i++)
{
float orig=params[i];
params[i]=orig+this->current_step_size;
float a=this->computeValue(params);
params[i]=orig-this->current_step_size;
float b=this->computeValue(params);
params[i]=orig;
float g=-0.5f*(b-a)/this->current_step_size;
grad[i]=g;
GradientNorm+=g*g;
}
GradientNorm = (float)sqrt(GradientNorm);
// std::cout << "\t grad=";
for (int i=0;i<numdof; i++) {
grad[i]=grad[i]/GradientNorm;
//std::cout << grad[i] << " ";
}
// std::cout << std::endl;
return GradientNorm;
}
int bisLinearImageRegistration::checkInputParameters(bisJSONParameterList* plist)
{
bisAbstractImageRegistration::checkInputParameters(plist);
int mode=bisUtil::irange(plist->getIntValue("mode",0),0,6);
int dim[3];
this->reference->getImageDimensions(dim);
if (dim[2]<2) {
// Force use of 2D mode
if (mode==0)
mode=4;
if (mode==1 || mode==2)
mode=5;
if (mode==3)
mode=6;
}
this->internalParameters->setIntValue("mode",mode);
this->internalParameters->setBooleanValue("centeronrefonly",plist->getBooleanValue("centeronrefonly",0));
if (this->enable_feedback)
this->internalParameters->print("Fixed Parameters prior to running Linear","+ + + ");
return 1;
}
// Set Parameters and Run
void bisLinearImageRegistration::run(bisJSONParameterList* plist)
{
// plist->print("Unfixd Parameters","+ + + ");
this->checkInputParameters(plist);
this->generateFeedback("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
std::stringstream strss;
strss.precision(5);
int numlevels= this->internalParameters->getIntValue("levels");
int numsteps= this->internalParameters->getIntValue("steps");
float stepsize= this->internalParameters->getFloatValue("stepsize");
int optimization=this->internalParameters->getIntValue("optimization");
int iterations=this->internalParameters->getIntValue("iterations");
float tolerance=this->internalParameters->getFloatValue("tolerance",0.001f);
int centeronrefonly=this->internalParameters->getBooleanValue("centeronrefonly",0);
int mode=this->internalParameters->getIntValue("mode",0);
int initial_mode=0;
if (mode>3)
initial_mode=4;
if (enable_feedback) {
std::cout << "+ + Retrieved parameters: nlevels=" << numlevels << " numsteps=" << numsteps << " stepsize=" << stepsize << std::endl;
std::cout << "+ + optimization=" << optimization << " iterations=" << iterations << " tolerance=" << tolerance << std::endl;
std::cout << "+ + mode=" << mode << ", initial=" << initial_mode << ", similarity metric=" << metric << std::endl;
}
// Initialize Transformation
this->internalTransformation->setMode(initial_mode);
this->internalTransformation->identity();
this->internalTransformation->setPreMatrixTransformation(this->initialTransformation.get());
time_t timer1,timer2;
time(&timer1);
for (int level=numlevels;level>=1;level=level-1)
{
strss.clear();
std::stringstream strss2;
strss2 << "+ + Beginning to compute l i n e a r registration at level=" << level << ", numsteps=" << numsteps << ", tolerance=" << tolerance << " centeronrefonly=" << centeronrefonly;
this->generateFeedback(strss2.str());
this->generateFeedback("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
this->initializeLevel(level);
if (level==numlevels)
{
// Set Shifts now that the first level is initialized ...
int dim_ref[3]; level_reference->getImageDimensions(dim_ref);
float spa_ref[3]; level_reference->getImageSpacing(spa_ref);
int dim_trg[3]; level_target->getImageDimensions(dim_trg);
float spa_trg[3]; level_target->getImageSpacing(spa_trg);
if (centeronrefonly) {
this->generateFeedback("+ + + + +");
this->generateFeedback("+ + + + + Shifting Based on reference only ");
this->generateFeedback("+ + + + +");
this->internalTransformation->setShifts(dim_ref,spa_ref,dim_ref,spa_ref);
}
else {
this->internalTransformation->setShifts(dim_ref,spa_ref,dim_trg,spa_trg);
}
}
this->generateFeedback("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
float spa[3]; this->level_reference->getImageSpacing(spa);
int numdof=this->internalTransformation->getNumberOfDOF();
std::stringstream strss3;
this->current_step_size=stepsize*powf(2.0f,float(numsteps-1))*spa[0];
strss3 << "+ + \t\t Beginning level=" << level << " resolution=" << spa[0] << " numdof=" << numdof << " current_step=" << this->current_step_size;
this->generateFeedback("+ + ");
this->generateFeedback(strss3.str());
this->generateFeedback("+ + ");
// Set stepsize
std::unique_ptr<bisOptimizer> optimizer(new bisOptimizer(this));
std::vector<float> position(numdof);
// true here refers to scale*100.0. Get Initial Parameters from transformation
this->internalTransformation->storeParameterVector(position,1);
this->totaltime=0.0;
for (int step=numsteps;step>=1;step=step-1)
{
if (enable_feedback)
std::cout << "+ + In step = " << step << ". Iterations = " << iterations << ", optimization=" << optimization <<", current=" << this->current_step_size << "." << std::endl;
if (optimization==0)
optimizer->computeSlowClimb(position,this->current_step_size,iterations);
else if (optimization==1)
optimizer->computeGradientDescent(position,iterations,tolerance);
else
optimizer->computeConjugateGradient(position,iterations,tolerance);
this->current_step_size=this->current_step_size/2.0f;
}
if (level==2)
this->internalTransformation->setMode(mode);
this->generateFeedback("+ + ");
this->generateFeedback("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
}
time(&timer2);
std::stringstream strss_final;
this->totaltime=difftime(timer2,timer1);
strss_final << "+ + Stats : total_time " << this->totaltime;
this->generateFeedback(strss_final.str());
this->generateFeedback("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
}