-
Notifications
You must be signed in to change notification settings - Fork 2
/
bisNonLinearImageRegistration.cpp
469 lines (363 loc) · 17.5 KB
/
bisNonLinearImageRegistration.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* 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 "bisNonLinearImageRegistration.h"
#include "bisApproximateDisplacementField.h"
#include "bisImageAlgorithms.h"
#include <sstream>
#include <time.h>
bisNonLinearImageRegistration::bisNonLinearImageRegistration(std::string s) : bisAbstractImageRegistration(s)
{
std::shared_ptr<bisComboTransformation> tmpc(new bisComboTransformation(this->name+":combo"));
this->internalTransformation=tmpc;
this->internalTransformation->identity();
this->class_name="bisNonLinearImageRegistration";
this->hasInitialTransformation=0;
this->append_mode=1;
}
bisNonLinearImageRegistration::~bisNonLinearImageRegistration()
{
this->lastSmoothness=-1.0;
this->lastSimilarity=-1.0;
}
void bisNonLinearImageRegistration::setInitialTransformation(std::shared_ptr<bisAbstractTransformation> tr)
{
this->initialTransformation=tr;
this->hasInitialTransformation=1;
}
void bisNonLinearImageRegistration::generateFeedback(std::string input)
{
if (this->enable_feedback)
std::cout << input << " (" << this->lastSimilarity << "," << this->lastSmoothness << ")" << std::endl;
}
void bisNonLinearImageRegistration::generateFeedback2(std::string input)
{
if (this->enable_feedback)
std::cout << input << std::endl;
}
// Optimizer Stuff
float bisNonLinearImageRegistration::computeValue(std::vector<float>& position)
{
this->currentGridTransformation->setParameterVector(position);
bisImageAlgorithms::resliceImage(this->level_target.get(),this->temp_target.get(),this->currentGridTransformation.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->currentGridTransformation.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);
this->lastSimilarity=mv;
if (this->lambda>0.0)
{
this->lastSmoothness=this->currentGridTransformation->getTotalBendingEnergy();
mv+=this->lambda*this->lastSmoothness;
}
return mv;
}
float bisNonLinearImageRegistration::computeValueFunctionPiece(bisAbstractTransformation* tr,int bounds[6],int cp)
{
short* weight1_ptr=0,*weight2_ptr=0;
if (this->use_weights>0)
weight1_ptr=this->level_reference_weight->getImageData();
if (this->use_weights==2)
weight2_ptr=this->temp_target_weight->getImageData();
int level_dimensions[3]; level_target->getImageDimensions(level_dimensions);
// Backup
this->internalHistogram->backup();
// Remove Part
this->internalHistogram->weightedFillHistogram(this->level_reference->getImageData(),
this->temp_target->getImageData(),
weight1_ptr,
weight2_ptr,
use_weights,
-1.0, // factor
0, // reset
level_dimensions,
bounds);
// Reslice into Part
bisImageAlgorithms::resliceImageWithBounds(this->level_target.get(),this->part_temp_target.get(),tr,bounds,1,0.0);
this->internalHistogram->weightedFillHistogram(this->level_reference->getImageData(),
this->part_temp_target->getImageData(),
weight1_ptr,
weight2_ptr,
use_weights,
1.0, // factor
0, // reset
level_dimensions,
bounds);
// Compute Metric
float mv=(float)this->internalHistogram->computeMetric(this->metric);
// Restore histogram
this->internalHistogram->restore();
if (this->lambda>0.0)
mv+=this->lambda*this->currentGridTransformation->getBendingEnergyAtControlPoint(cp);
return mv;
}
float bisNonLinearImageRegistration::computeGradient(std::vector<float>& params,std::vector<float>& grad)
{
int dim_ref[3]; level_reference->getImageDimensions(dim_ref);
float spa_ref[3]; level_reference->getImageSpacing(spa_ref);
float window_size=1.0;
return this->currentGridTransformation->computeGradientForOptimization(params,grad,
this->current_step_size,
dim_ref,spa_ref,window_size,
this);
}
int bisNonLinearImageRegistration::checkInputParameters(bisJSONParameterList* plist)
{
bisAbstractImageRegistration::checkInputParameters(plist);
this->internalParameters->setFloatValue("cps",bisUtil::frange(plist->getFloatValue("cps",20.0f),0.1f,50.0f));
this->internalParameters->setFloatValue("cpsrate",bisUtil::frange(plist->getFloatValue("cpsrate",2.0f),1.0f,2.0f));
this->internalParameters->setFloatValue("lambda",bisUtil::frange(plist->getFloatValue("lambda",0.0f),0.0f,1.0f));
this->internalParameters->setFloatValue("windowsize",bisUtil::frange(plist->getFloatValue("windowsize",1.0f),1.0f,2.0f));
this->internalParameters->setBooleanValue("appendmode",plist->getBooleanValue("appendmode",1));
if (this->enable_feedback)
this->internalParameters->print("Fixed Parameters prior to running Non Linear","+ + + ");
this->lambda=this->internalParameters->getFloatValue("lambda",0.0f);
this->windowsize= this->internalParameters->getFloatValue("windowsize",1.0f);
return 1;
}
// ------------------------------------------------------------------------------------------
bisSimpleImage<float>* bisNonLinearImageRegistration::computeDisplacementField(bisAbstractTransformation* xform)
{
int dim_ref[3]; level_reference->getImageDimensions(dim_ref);
float spa_ref[3]; level_reference->getImageSpacing(spa_ref);
int newdim[3]; float newspa[3];
for (int ia=0;ia<=2;ia++) {
newspa[ia]=this->current_cps[ia]/3.0;
newdim[ia]=int( ((dim_ref[ia]+1)*spa_ref[ia])/newspa[ia]+0.5)-1;
}
this->generateFeedback2("++ ");
std::cout << "+ + Computing displacement field to fit. Dim=" <<newdim[0] << "," << newdim[1] << "," << newdim[2] <<
", spa=" << newspa[0] << "," << newspa[1] << "," << newspa[2] << std::endl;
return xform->computeDisplacementField(newdim,newspa);
}
void bisNonLinearImageRegistration::approximateDisplacementField(bisSimpleImage<float>* dispfield,bisGridTransformation* newgrd,int fast)
{
// Compute the displacement field from old
float spa_ref[3]; level_reference->getImageSpacing(spa_ref);
std::unique_ptr<bisJSONParameterList> params(new bisJSONParameterList());
params->setFloatValue("lambda",0.1);
params->setFloatValue("tolerance",spa_ref[0]*0.02);
params->setIntValue("inverse",0);
if (fast)
{
params->setIntValue("levels",1);
params->setFloatValue("resolution",1.0);
params->setIntValue("steps",2);
params->setIntValue("iterations",5);
params->setFloatValue("stepsize",0.125);
}
else
{
params->setIntValue("levels",2);
params->setFloatValue("resolution",1.0);
params->setIntValue("steps",3);
params->setIntValue("iterations",10);
params->setFloatValue("stepsize",0.125);
}
std::unique_ptr<bisApproximateDisplacementField> reg(new bisApproximateDisplacementField("approx"));
reg->run(dispfield,newgrd,params.get());
}
// ------------------------------------------------------------------------------------------
void bisNonLinearImageRegistration::initializeLevelAndGrid(int lv,int numlevels)
{
std::cout << "+ + I n i t i a l i z i n g L e v e l " << lv << std::endl;
if (lv==numlevels && this->hasInitialTransformation)
{
// If this is the lowest resolution we need to figure out how to handle internal
int islinear=this->initialTransformation->isLinear();
std::cout << "+ + \t we have an initial transformation, linear=" << islinear << std::endl;
if (islinear==1) {
// If it is linear, we are happy no big deal just copy its
std::cout << "+ + \t this is a pure linear transformation " << std::endl;
bisMatrixTransformation* linear=(bisMatrixTransformation*)(this->initialTransformation.get());
bisUtil::mat44 m; linear->getMatrix(m);
linear->printSelf();
this->internalTransformation->setInitialTransformation(m);
} else if (islinear == -1) {
std::cout << "+ + \t this is a combo transformation " << std::endl;
bisComboTransformation* in=(bisComboTransformation*)(this->initialTransformation.get());
bisUtil::mat44 m; in->getInitialTransformation(m);
bisUtil::printMatrix(m,"linear component");
this->internalTransformation->setInitialTransformation(m);
}
}
if (this->append_mode)
{
bisAbstractImageRegistration::initializeLevel(lv,this->internalTransformation.get());
}
else
{
bisUtil::mat44 m;
this->internalTransformation->getInitialTransformation(m);
std::unique_ptr<bisMatrixTransformation> temp_linear(new bisMatrixTransformation("temp_linear"));
temp_linear->setMatrix(m);
bisAbstractImageRegistration::initializeLevel(lv,temp_linear.get());
}
std::unique_ptr<bisSimpleImage<short> > tmp(new bisSimpleImage<short>(this->name+":part_temp_target_image"));
this->part_temp_target=std::move(tmp);
this->part_temp_target->copyStructure(this->level_reference.get());
float cps=this->internalParameters->getFloatValue("cps",20.0);
float rate=this->internalParameters->getFloatValue("cpsrate",2.0);
cps=cps*powf(rate,lv-1.0f);
int dim_ref[3]; level_reference->getImageDimensions(dim_ref);
float spa_ref[3]; level_reference->getImageSpacing(spa_ref);
float grid_ori[3] = { 0,0,0};
this->current_dim[2]=1;
this->current_cps[2]=1.0;
int maxdim=2;
if (dim_ref[2]<2)
maxdim=1;
for (int ia=0;ia<=maxdim;ia++)
{
float imagesize=(dim_ref[ia]-1)*spa_ref[ia]+1;
int numcp=int(imagesize/cps+0.5);
if (numcp<4)
numcp=4;
this->current_dim[ia]=numcp;
this->current_cps[ia]=imagesize/(this->current_dim[ia]-1.05f);
float outsz=(numcp-1)*this->current_cps[ia];
float offset=outsz-imagesize;
grid_ori[ia]=-0.5f*offset;
}
std::stringstream strss;
strss << this->name << "_grid_" << lv;
// Do we need to approximate the grid
if (lv !=numlevels && append_mode==0)
{
// Fit the previous grid
std::cout << "+ + ================================================" << std::endl;
std::cout << "+ + Fitting previous grid" << std::endl;
std::unique_ptr<bisSimpleImage<float> > dispfield(this->computeDisplacementField(this->currentGridTransformation.get()));
this->currentGridTransformation->initializeGrid(this->current_dim,this->current_cps,grid_ori,1);
this->approximateDisplacementField(dispfield.get(),this->currentGridTransformation.get(),1);
std::cout << "+ + ================================================" << std::endl;
}
else
{
// First create a new grid
std::shared_ptr<bisGridTransformation> tmp_g(new bisGridTransformation(strss.str()));
this->currentGridTransformation=std::move(tmp_g);
this->currentGridTransformation->initializeGrid(this->current_dim,this->current_cps,grid_ori,1);
// Then check if need to fit it
if (lv==numlevels && this->hasInitialTransformation)
{
int islinear=this->initialTransformation->isLinear();
if (islinear == -1)
{
// We have a combo
bisComboTransformation* initial=(bisComboTransformation*)(this->initialTransformation.get());
// store this for now
bisUtil::mat44 oldlinear; initial->getInitialTransformation(oldlinear);
std::unique_ptr<bisMatrixTransformation> ident(new bisMatrixTransformation);
ident->identity();
initial->setInitialTransformation(ident.get());
std::unique_ptr<bisSimpleImage<float> > dispfield(this->computeDisplacementField(initial));
this->approximateDisplacementField(dispfield.get(),this->currentGridTransformation.get(),0);
initial->setInitialTransformation(oldlinear);
}
}
// Add the grid to the back of the internalTransformation Combo
this->internalTransformation->addTransformation(this->currentGridTransformation);
}
}
// Set Parameters and Run
void bisNonLinearImageRegistration::run(bisJSONParameterList* plist)
{
this->checkInputParameters(plist);
this->generateFeedback2("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
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);
this->append_mode=this->internalParameters->getBooleanValue("appendmode",1);
// Also cps, cpsrate, windowsize, lambda ...
if (this->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 << "+ + similarity metric=" << metric << ", append_mode=" << this->append_mode << std::endl; // TOADD cps, cpsrate, windowsize
}
time_t timer1,timer2;
time(&timer1);
for (int level=numlevels;level>=1;level=level-1)
{
strss.clear();
std::stringstream strss2;
strss2 << "+ + Beginning to compute n o n l i n e a r registration at level=" << level << ", numsteps=" << numsteps << ", tolerance=" << tolerance;
this->generateFeedback2(strss2.str());
this->generateFeedback2("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
this->initializeLevelAndGrid(level,numlevels);
this->generateFeedback2("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
float spa[3]; this->level_reference->getImageSpacing(spa);
int numdof=this->currentGridTransformation->getNumberOfDOF();
this->current_step_size=stepsize*powf(2.0f,float(numsteps-1))*spa[0];
this->generateFeedback2("+ + ");
std::stringstream strss3;
strss3 << "+ + \t\t Beginning level=" << level << " resolution=" << spa[0] << " numdof=" << numdof << " current_step=" << this->current_step_size;
std::stringstream strss4;
strss4 << "+ + \t\t transformation: dim=(" << this->current_dim[0] << "*" << this->current_dim[1] << "*" << this->current_dim[2] << "), ";
strss4 << "spa=(" << this->current_cps[0] << "," << this->current_cps[1] << "," << this->current_cps[2] << ") ";
this->generateFeedback2(strss3.str());
this->generateFeedback2(strss4.str());
this->generateFeedback2("+ + ");
// Set stepsize
std::unique_ptr<bisOptimizer> optimizer(new bisOptimizer(this));
std::vector<float> position(numdof);
// Get current state ...
this->currentGridTransformation->getParameterVector(position);
this->totaltime=0.0;
for (int step=numsteps;step>=1;step=step-1)
{
if (this->enable_feedback)
std::cout << "+ + In step = " << step << ". Iterations = " << iterations << ", optimization=" << optimization <<", current=" << this->current_step_size << "." << std::endl;
if (optimization==1)
optimizer->computeGradientDescent(position,iterations,tolerance);
else
optimizer->computeConjugateGradient(position,iterations,tolerance);
this->current_step_size=this->current_step_size/2.0f;
}
this->generateFeedback2("+ + ");
this->generateFeedback2("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
}
time(&timer2);
std::stringstream strss_final;
this->totaltime=difftime(timer2,timer1);
strss_final << "+ + Stats : total_time " << this->totaltime;
this->generateFeedback2(strss_final.str());
this->generateFeedback2("+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +");
}