forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itkPICSLAdvancedNormalizationToolKit.hxx
1463 lines (1331 loc) · 58.7 KB
/
itkPICSLAdvancedNormalizationToolKit.hxx
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Advanced Normalization Tools
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _itkPICSLAdvancedNormalizationToolKit_hxx_
#define _itkPICSLAdvancedNormalizationToolKit_hxx_
// disable debug warnings in MS compiler
#ifdef _MSC_VER
#pragma warning(disable: 4786)
#endif
#include "itkPICSLAdvancedNormalizationToolKit.h"
#include "itkHistogramMatchingImageFilter.h"
#include "itkSpatialMutualInformationRegistrationFunction.h"
#include "itkIdentityTransform.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkLabeledPointSetFileReader.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkRecursiveGaussianImageFilter.h"
#include "itkResampleImageFilter.h"
// Image similarity metrics
#include "itkAvantsMutualInformationRegistrationFunction.h"
#include "itkSyNDemonsRegistrationFunction.h"
#include "itkProbabilisticRegistrationFunction.h"
#include "itkCrossCorrelationRegistrationFunction.h"
#include "itkExpectationBasedPointSetRegistrationFunction.h"
// #include "itkRobustDemonsRegistrationFunction.h"
// #include "itkRobustOpticalFlow.h"
// #include "itkSectionMutualInformationRegistrationFunction.h"
// #include "itkJensenTsallisBSplineRegistrationFunction.h"
#include "vnl/vnl_math.h"
#include "ANTS_affine_registration2.h"
namespace itk
{
template <unsigned int TDimension, class TReal>
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::PICSLAdvancedNormalizationToolKit()
{
this->InitializeCommandLineOptions();
}
template <unsigned int TDimension, class TReal>
void
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::ParseCommandLine( int argc, char * *argv )
{
this->m_Parser->Parse( argc, argv );
typename ParserType::OptionListType unknownOptions =
this->m_Parser->GetUnknownOptions();
if( unknownOptions.size() )
{
std::cout << std::endl << "WARNING: Unknown options" << std::endl;
typename ParserType::OptionListType::const_iterator its;
for( its = unknownOptions.begin(); its != unknownOptions.end(); its++ )
{
if( (*its)->GetShortName() != '\0' )
{
std::cout << " " << '-' << (*its)->GetShortName() << std::endl;
}
else
{
std::cout << " " << "--" << (*its)->GetLongName() << std::endl;
}
}
std::cout << std::endl;
}
std::string printhelp_long = this->m_Parser->GetOption( "help" )->GetFunction( 0 )->GetName();
unsigned int help_long =
this->m_Parser->template Convert<unsigned int>( printhelp_long );
if( help_long )
{
this->m_Parser->PrintMenu( std::cout, 7, false );
std::exception();
exit( EXIT_SUCCESS );
}
std::string printhelp_short = this->m_Parser->GetOption( 'h' )->GetFunction( 0 )->GetName();
unsigned int help_short =
this->m_Parser->template Convert<unsigned int>( printhelp_short );
if( help_short )
{
this->m_Parser->PrintMenu( std::cout, 7, true );
std::exception();
exit( EXIT_SUCCESS );
}
}
template <unsigned int TDimension, class TReal>
void
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::InitializeTransformAndOptimizer()
{
if( !this->m_TransformationModel )
{
this->m_TransformationModel = TransformationModelType::New();
}
if( !this->m_RegistrationOptimizer )
{
this->m_RegistrationOptimizer = RegistrationOptimizerType::New();
}
std::string temp = this->m_Parser->GetOption( "output-naming" )->GetFunction( 0 )->GetName();
this->m_TransformationModel->SetNamingConvention( temp );
this->m_TransformationModel->InitializeTransform();
this->m_RegistrationOptimizer->SetParser( this->m_Parser );
this->m_RegistrationOptimizer->SetSimilarityMetrics( this->m_SimilarityMetrics );
}
template <unsigned int TDimension, class TReal>
void
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::RunRegistration()
{
/** parse the command line and get input objects */
this->ReadImagesAndMetrics();
// std::exception();
/** initializes the transformation model and the optimizer */
this->InitializeTransformAndOptimizer();
/** Get the mask if there is one */
typename OptionType::Pointer option = this->m_Parser->GetOption( "mask-image" );
if( option && option->GetNumberOfFunctions() )
{
typedef ImageFileReader<ImageType> ReaderType;
std::string maskfn = this->m_Parser->GetOption( "mask-image" )->GetFunction( 0 )->GetName();
if( maskfn.length() > 4 )
{
typename ReaderType::Pointer maskImageFileReader = ReaderType::New();
maskImageFileReader->SetFileName( maskfn.c_str() );
maskImageFileReader->Update();
ImagePointer maskImage = this->PreprocessImage( maskImageFileReader->GetOutput() );
this->m_RegistrationOptimizer->SetMaskImage(maskImage);
}
}
typename TransformationModelType::AffineTransformPointer aff_init, aff, fixed_aff_init;
// added by songgang
// try initialize the affine transform
typename OptionType::Pointer initialAffineOption = this->m_Parser->GetOption( "initial-affine" );
if( initialAffineOption && initialAffineOption->GetNumberOfFunctions() )
{
std::string initial_affine_filename = initialAffineOption->GetFunction( 0 )->GetName();
std::cout << "Loading affine registration from: " << initial_affine_filename << std::endl;
ReadAffineTransformFile<typename TransformationModelType::AffineTransformType>(initial_affine_filename, aff_init);
}
else
{
std::cout << "Use identity affine transform as initial affine para." << std::endl;
std::cout << "aff_init.IsNull()==" << aff_init.IsNull() << std::endl;
}
std::string fixed_initial_affine_filename = std::string( "" );
typename OptionType::Pointer fixedInitialAffineOption = this->m_Parser->GetOption( "fixed-image-initial-affine" );
if( fixedInitialAffineOption && fixedInitialAffineOption->GetNumberOfFunctions() )
{
fixed_initial_affine_filename = fixedInitialAffineOption->GetFunction( 0 )->GetName();
std::cout << "Loading affine registration from: " << fixed_initial_affine_filename << std::endl;
fixed_aff_init = TransformationModelType::AffineTransformType::New();
ReadAffineTransformFile<typename TransformationModelType::AffineTransformType>(fixed_initial_affine_filename,
fixed_aff_init);
std::cout
<<
" FIXME! currently, if one passes a fixed initial affine mapping, then NO affine mapping will be performed subsequently! "
<< std::endl;
typename OptionType::Pointer fixedImageInitialAffineRegImageOption = this->m_Parser->GetOption(
"fixed-image-initial-affine-ref-image" );
if( fixedImageInitialAffineRegImageOption && fixedImageInitialAffineRegImageOption->GetNumberOfFunctions() )
{
std::string refheader = fixedImageInitialAffineRegImageOption->GetFunction( 0 )->GetName();
std::cout << " Setting reference deformation space by " << refheader << std::endl;
typedef ImageFileReader<ImageType> ReaderType;
typename ReaderType::Pointer fixedImageFileReader = ReaderType::New();
fixedImageFileReader->SetFileName( refheader.c_str() );
fixedImageFileReader->Update();
ImagePointer temp = fixedImageFileReader->GetOutput();
this->m_RegistrationOptimizer->SetReferenceSpaceImage(temp);
}
}
else
{
std::cout << "Use identity affine transform as initial fixed affine para." << std::endl;
std::cout << "fixed_aff_init.IsNull()==" << fixed_aff_init.IsNull() << std::endl;
}
typename OptionType::Pointer useNNOption = this->m_Parser->GetOption( "use-NN" );
if( useNNOption && useNNOption->GetNumberOfFunctions() &&
this->m_Parser->template Convert<bool>( useNNOption->GetFunction( 0 )->GetName() ) )
{
this->m_RegistrationOptimizer->SetUseNearestNeighborInterpolation(true);
}
else
{
this->m_RegistrationOptimizer->SetUseNearestNeighborInterpolation(false);
}
std::string continue_affine = this->m_Parser->GetOption( "continue-affine" )->GetFunction( 0 )->GetName();
if( fixed_initial_affine_filename != "" )
{
continue_affine = std::string("false");
}
if( continue_affine == "true" )
{
std::cout << "Continue affine registration from the input" << std::endl; // << aff_init << std::endl;
OptAffineType affine_opt;
// InitializeAffineOption()
{
affine_opt.transform_initial = aff_init;
std::string temp = this->m_Parser->GetOption( "number-of-affine-iterations" )->GetFunction( 0 )->GetName();
affine_opt.number_of_iteration_list = this->m_Parser->template ConvertVector<int>(temp);
affine_opt.number_of_levels = affine_opt.number_of_iteration_list.size();
temp = this->m_Parser->GetOption( "affine-metric-type" )->GetFunction( 0 )->GetName();
if( temp == "MI" )
{
affine_opt.metric_type = AffineWithMutualInformation;
}
if( temp == "MSQ" )
{
affine_opt.metric_type = AffineWithMeanSquareDifference;
}
if( temp == "CCH" )
{
affine_opt.metric_type = AffineWithHistogramCorrelation;
}
if( temp == "CC" )
{
affine_opt.metric_type = AffineWithNormalizedCorrelation;
}
if( temp == "GD" )
{
affine_opt.metric_type = AffineWithGradientDifference;
}
temp = this->m_Parser->GetOption( "MI-option" )->GetFunction( 0 )->GetName();
std::vector<int> mi_option = this->m_Parser->template ConvertVector<int>(temp);
affine_opt.MI_bins = mi_option[0];
affine_opt.MI_samples = mi_option[1];
temp = this->m_Parser->GetOption( "rigid-affine" )->GetFunction( 0 )->GetName();
std::string temp2 = this->m_Parser->GetOption( "do-rigid" )->GetFunction( 0 )->GetName();
affine_opt.is_rigid = (
( temp == "true" ) || ( temp2 == "true" ) ||
( temp == "1" ) || ( temp2 == "1" ) );
temp = this->m_Parser->GetOption( "affine-gradient-descent-option" )->GetFunction( 0 )->GetName();
std::vector<double> gradient_option = this->m_Parser->template ConvertVector<double>(temp);
affine_opt.maximum_step_length = gradient_option[0];
affine_opt.relaxation_factor = gradient_option[1];
affine_opt.minimum_step_length = gradient_option[2];
affine_opt.translation_scales = gradient_option[3];
// std::cout << affine_opt;
temp = this->m_Parser->GetOption( "use-rotation-header" )->GetFunction( 0 )->GetName();
affine_opt.use_rotation_header = (temp == "true");
std::cout << "affine_opt.use_rotation_header = " << affine_opt.use_rotation_header << std::endl;
temp = this->m_Parser->GetOption( "ignore-void-origin")->GetFunction( 0 )->GetName();
affine_opt.ignore_void_orgin = (temp == "true");
std::cout << "affine_opt.ignore_void_orgin = " << affine_opt.ignore_void_orgin << std::endl;
}
aff = this->m_RegistrationOptimizer->AffineOptimization(affine_opt);
}
else
{
std::cout << "Use fixed initial affine para." << std::endl;
if( aff_init.IsNull() )
{
aff_init = TransformationModelType::AffineTransformType::New();
aff_init->SetIdentity();
}
if( fixed_aff_init.IsNull() )
{
fixed_aff_init = TransformationModelType::AffineTransformType::New();
fixed_aff_init->SetIdentity();
}
aff = aff_init;
}
// std::cout << aff << std::endl;
this->m_TransformationModel->SetAffineTransform(aff);
this->m_TransformationModel->SetFixedImageAffineTransform(fixed_aff_init);
this->m_RegistrationOptimizer->SetAffineTransform(aff);
this->m_RegistrationOptimizer->SetFixedImageAffineTransform(
this->m_TransformationModel->GetFixedImageAffineTransform() );
/** Second, optimize Diff */
this->m_RegistrationOptimizer->DeformableOptimization();
std::cout << " Registration Done " << std::endl;
this->m_TransformationModel->SetDisplacementField(this->m_RegistrationOptimizer->GetDisplacementField() );
this->m_TransformationModel->SetInverseDisplacementField(this->m_RegistrationOptimizer->GetInverseDisplacementField() );
}
template <unsigned int TDimension, class TReal>
typename PICSLAdvancedNormalizationToolKit<TDimension, TReal>::ImagePointer
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::PreprocessImage( ImagePointer image )
{
image = this->ReplaceProblematicPixelValues( image, 0 );
return image;
}
template <unsigned int TDimension, class TReal>
typename PICSLAdvancedNormalizationToolKit<TDimension, TReal>::ImagePointer
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::ReplaceProblematicPixelValues( ImagePointer image, PixelType value)
{
ImageRegionIterator<ImageType> It( image, image->GetRequestedRegion() );
for( It.GoToBegin(); !It.IsAtEnd(); ++It )
{
PixelType pixel = It.Get();
if( vnl_math_isinf( pixel ) || vnl_math_isnan( pixel ) )
{
It.Set( value );
}
}
typedef itk::MinimumMaximumImageFilter<ImageType> MinMaxFilterType;
typename MinMaxFilterType::Pointer minMaxFilter = MinMaxFilterType::New();
minMaxFilter->SetInput( image );
minMaxFilter->Update();
double min = minMaxFilter->GetMinimum();
double shift = -1.0 * static_cast<double>( min );
double scale = static_cast<double>( minMaxFilter->GetMaximum() );
scale += shift;
scale = 1.0 / scale;
typedef itk::ShiftScaleImageFilter<ImageType, ImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetInput( image );
filter->SetShift( shift );
filter->SetScale( scale );
filter->Update();
// when the image is a constant image, the scale filter will fail
// replace again everything into $pixel
ImagePointer image2 = filter->GetOutput();
ImageRegionIterator<ImageType> It2( image2, image2->GetRequestedRegion() );
for( It2.GoToBegin(); !It2.IsAtEnd(); ++It2 )
{
PixelType pixel = It2.Get();
if( vnl_math_isinf( pixel ) || vnl_math_isnan( pixel ) )
{
It2.Set( value );
}
}
return image2;
}
/**
* Standard "PrintSelf" method
*/
template <unsigned int TDimension, class TReal>
void
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::PrintSelf( std::ostream& os, Indent indent) const
{
Superclass::PrintSelf( os, indent );
}
template <unsigned int TDimension, class TReal>
void
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::ReadImagesAndMetrics()
{
/**
* Read in all images and preprocess them before
* storing them in their corresponding image lists.
*/
this->m_SimilarityMetrics.clear();
typedef ImageFileReader<ImageType> ReaderType;
bool useHistMatch = this->m_Parser->template Convert<bool>( this->m_Parser->GetOption(
"use-Histogram-Matching" )->GetFunction()->GetName() );
/**
* Read the metrics and image files
*/
if( typename OptionType::Pointer option = this->m_Parser->GetOption( "image-metric" ) )
{
std::cout << " values " << option->GetNumberOfFunctions() << std::endl;
for( unsigned int i = 0; i < option->GetNumberOfFunctions(); i++ )
{
SimilarityMetricPointer similarityMetric = SimilarityMetricType::New();
RealType similarityMetricScalarWeight = 1.0;
similarityMetric->SetWeightScalar( similarityMetricScalarWeight );
unsigned int parameterCount = 0;
typename ReaderType::Pointer fixedImageFileReader = ReaderType::New();
fixedImageFileReader->SetFileName( option->GetFunction( i )->GetParameter( parameterCount ) );
fixedImageFileReader->Update();
ImagePointer fixedImage = this->PreprocessImage(
fixedImageFileReader->GetOutput() );
similarityMetric->SetFixedImage( fixedImage );
parameterCount++;
std::cout << " Fixed image file: "
<< fixedImageFileReader->GetFileName() << std::endl;
typename ReaderType::Pointer movingImageFileReader = ReaderType::New();
movingImageFileReader->SetFileName( option->GetFunction( i )->GetParameter( parameterCount ) );
movingImageFileReader->Update();
ImagePointer movingImage = this->PreprocessImage(
movingImageFileReader->GetOutput() );
similarityMetric->SetMovingImage( movingImage );
typename SimilarityMetricType::RadiusType radius;
radius.Fill( 0 );
parameterCount++;
std::cout << " Moving image file: "
<< movingImageFileReader->GetFileName() << std::endl;
/**
* Check if similarity metric is image based or point-set based.
* Image metrics:
* > mean-squares/MeanSquares/MSQ
* > mutual-information/MutualInformation/MI
* > cross-correlation/CrossCorrelation/CC
* > probabilistic/Probabilistic/PR
*
* Point-set metrics:
* > point-set-expectation/PointSetExpectation/PSE
* > jensen-tsallis-bspline/JensenTsallisBSpline/JTB
*
*/
bool isMetricPointSetBased = false;
std::string whichMetric = option->GetFunction( i )->GetName();
if( whichMetric == "point-set-expectation" ||
whichMetric == "PointSetExpectation" ||
whichMetric == "PSE"
// whichMetric == "jensen-tsallis-bspline" ||
// whichMetric == "JensenTsallisBSpline" ||
// whichMetric == "JTB"
)
{
isMetricPointSetBased = true;
}
if( isMetricPointSetBased )
{
std::cout << "Metric " << i << ": " << " Point-set " << whichMetric << " n-params "
<< option->GetFunction( i )->GetNumberOfParameters() << std::endl;
/**
* Read in the point-set metric parameters
*/
typedef LabeledPointSetFileReader<PointSetType> PointSetReaderType;
typename PointSetReaderType::Pointer fixedPointSetReader
= PointSetReaderType::New();
fixedPointSetReader->SetFileName(
option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
typename PointSetReaderType::Pointer movingPointSetReader
= PointSetReaderType::New();
movingPointSetReader->SetFileName(
option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
similarityMetricScalarWeight = this->m_Parser->template
Convert<RealType>( option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
}
similarityMetric->SetWeightScalar( similarityMetricScalarWeight );
std::cout << " similarity metric weight: "
<< similarityMetricScalarWeight << std::endl;
TReal pointSetPercent = 0.5;
TReal pointSetSigma = 5.0;
bool extractBoundaryPointsOnly = false;
unsigned int kNeighborhood = 50;
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
pointSetPercent = this->m_Parser->template
Convert<TReal>( option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
}
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
pointSetSigma = this->m_Parser->template
Convert<TReal>( option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
}
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
extractBoundaryPointsOnly = this->m_Parser->template
Convert<bool>( option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
}
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
kNeighborhood = this->m_Parser->template
Convert<unsigned int>( option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
}
std::cout << " point-set sigma = " << pointSetSigma << std::endl;
std::cout << " percentage of points = " << pointSetPercent << std::endl;
std::cout << " k-neighborhood = " << kNeighborhood << std::endl;
if( extractBoundaryPointsOnly )
{
std::cout << " use only boundary points. " << pointSetPercent << std::endl;
}
fixedPointSetReader->SetRandomPercentage( pointSetPercent );
fixedPointSetReader->SetExtractBoundaryPoints( extractBoundaryPointsOnly );
fixedPointSetReader->Update();
std::cout << " Fixed point-set file: "
<< fixedPointSetReader->GetFileName() << std::endl;
std::cout << " Number of fixed labels: "
<< fixedPointSetReader->GetLabelSet()->size() << std::endl;
std::cout << " Distinct fixed labels: ";
for( unsigned int n = 0;
n < fixedPointSetReader->GetLabelSet()->size(); n++ )
{
std::cout << fixedPointSetReader->GetLabelSet()->operator[]( n ) << " ";
}
std::cout << std::endl;
movingPointSetReader->SetRandomPercentage( pointSetPercent );
movingPointSetReader->SetExtractBoundaryPoints( extractBoundaryPointsOnly );
movingPointSetReader->Update();
movingPointSetReader->SetRandomPercentage( pointSetPercent );
movingPointSetReader->SetExtractBoundaryPoints( extractBoundaryPointsOnly );
movingPointSetReader->Update();
std::cout << " Moving point-set file: "
<< movingPointSetReader->GetFileName() << std::endl;
std::cout << " Number of moving labels: "
<< movingPointSetReader->GetLabelSet()->size() << std::endl;
std::cout << " Distinct moving labels: ";
for( unsigned int n = 0;
n < movingPointSetReader->GetLabelSet()->size(); n++ )
{
std::cout << movingPointSetReader->GetLabelSet()->operator[]( n ) << " ";
}
std::cout << std::endl;
if( whichMetric == "point-set-expectation" ||
whichMetric == "PointSetExpectation" ||
whichMetric == "PSE" )
{
typedef itk::ExpectationBasedPointSetRegistrationFunction
<ImageType, ImageType, DisplacementFieldType, PointSetType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
metric->SetRadius( radius );
metric->SetFixedPointSet( fixedPointSetReader->GetOutput() );
metric->SetMovingPointSet( movingPointSetReader->GetOutput() );
metric->SetFixedPointSetSigma( pointSetSigma );
metric->SetMovingPointSetSigma( pointSetSigma );
metric->SetKNeighborhood( kNeighborhood );
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
unsigned int pm =
this->m_Parser->template Convert<unsigned int>( option->GetFunction( i )->GetParameter( parameterCount ) );
metric->SetUseSymmetricMatching( pm );
std::cout << " Symmetric match iterations -- going Asymmeric for the rest " << pm << std::endl;
parameterCount++;
}
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
similarityMetric->SetFixedPointSet( fixedPointSetReader->GetOutput() );
similarityMetric->SetMovingPointSet(movingPointSetReader->GetOutput() );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
// else if ( whichMetric == "jensen-tsallis-bspline" ||
// whichMetric == "JensenTsallisBSpline" ||
// whichMetric == "JTB" )
// {
// typedef itk::JensenTsallisBSplineRegistrationFunction
// <ImageType, PointSetType, ImageType, PointSetType, DisplacementFieldType> MetricType;
// typename MetricType::Pointer metric = MetricType::New();
// metric->SetRadius( radius );
// metric->SetFixedPointSet( fixedPointSetReader->GetOutput() );
// metric->SetMovingPointSet( movingPointSetReader->GetOutput() );
// metric->SetFixedPointSetSigma( pointSetSigma );
// metric->SetMovingPointSetSigma( pointSetSigma );
// metric->SetFixedEvaluationKNeighborhood( kNeighborhood );
// metric->SetMovingEvaluationKNeighborhood( kNeighborhood );
//
// if ( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
// {
// metric->SetAlpha( this->m_Parser->template
// Convert<TReal>( option->GetFunction( i )->GetParameter( parameterCount ) ) );
// parameterCount++;
// }
// if ( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
// {
// typename RegistrationOptimizerType::ArrayType meshResolution;
// std::vector<TReal> resolution = this->m_Parser->template
// ConvertVector<TReal>( option->GetFunction( i )->GetParameter( parameterCount ) );
// if ( resolution.size() != TDimension )
// {
// itkExceptionMacro( "Mesh resolution does not match image dimension." );
// }
// for ( unsigned int d = 0; d < TDimension; d++ )
// {
// meshResolution[d] = resolution[d];
// }
// metric->SetMeshResolution( meshResolution );
// parameterCount++;
// }
// if ( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
// {
// metric->SetSplineOrder( this->m_Parser->template
// Convert<unsigned int>( option->GetFunction( i )->GetParameter( parameterCount ) ) );
// parameterCount++;
// }
// if ( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
// {
// metric->SetNumberOfLevels( this->m_Parser->template
// Convert<unsigned int>( option->GetFunction( i )->GetParameter( parameterCount ) ) );
// parameterCount++;
// }
// if ( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
// {
// metric->SetUseAnisotropicCovariances(
// this->m_Parser->template Convert<bool>(
// option->GetFunction( i )->GetParameter( parameterCount ) ) );
// parameterCount++;
// }
//
//
// std::cout << " B-spline parameters " << std::endl;
// std::cout << " mesh resolution: " << metric->GetMeshResolution() << std::endl;
// std::cout << " spline order: " << metric->GetSplineOrder() << std::endl;
// std::cout << " number of levels: " << metric->GetNumberOfLevels() << std::endl;
// std::cout << " Alpha: " << metric->GetAlpha() << std::endl;
// if ( metric->GetUseAnisotropicCovariances() )
// {
// std::cout << " using anisotropic covariances." << std::endl;
// }
//
// similarityMetric->SetMetric( metric );
// similarityMetric->SetMaximizeMetric( true );
// similarityMetric->SetFixedPointSet( fixedPointSetReader->GetOutput() );
// similarityMetric->SetMovingPointSet( movingPointSetReader->GetOutput() );
// this->m_SimilarityMetrics.push_back( similarityMetric );
// }
}
else // similarity metric is image-based
{
std::cout << "Metric " << i << ": " << " Not a Point-set" << std::endl;
std::cout << " Fixed image file: "
<< fixedImageFileReader->GetFileName() << std::endl;
std::cout << " Moving image file: "
<< movingImageFileReader->GetFileName() << std::endl;
similarityMetric->SetFixedPointSet( ITK_NULLPTR);
similarityMetric->SetMovingPointSet( ITK_NULLPTR );
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
similarityMetricScalarWeight = this->m_Parser->template
Convert<RealType>( option->GetFunction( i )->GetParameter( parameterCount ) );
parameterCount++;
}
similarityMetric->SetWeightScalar( similarityMetricScalarWeight );
std::cout << " similarity metric weight: "
<< similarityMetricScalarWeight << std::endl;
radius.Fill( 0 );
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
std::vector<unsigned int> rad = this->m_Parser->template
ConvertVector<unsigned int>( option->GetFunction( i )->GetParameter(
parameterCount ) );
if( rad.size() == 1 )
{
radius.Fill( rad[0] );
}
else if( rad.size() == TDimension )
{
for( unsigned int n = 0; n < TDimension; n++ )
{
radius[n] = rad[n];
}
}
else
{
std::cout << "Badly formed radius specification" << std::endl;
std::exception();
}
parameterCount++;
}
std::cout << " Radius: " << radius << std::endl;
TReal extraparam = -1.e12;
if( option->GetFunction( i )->GetNumberOfParameters() > parameterCount )
{
extraparam = this->m_Parser->template Convert<TReal>( option->GetFunction( i )->GetParameter(
parameterCount ) );
std::cout << " Setting Extra Param to : " << extraparam
<< " often used as a robustness parameter for longitudinal studies " << std::endl;
parameterCount++;
}
std::cout << " radius: " << radius << std::endl;
unsigned int numberOfHistogramBins = 64;
if( Dimension == 2 )
{
numberOfHistogramBins = 32;
}
if( whichMetric == "mean-squares" ||
whichMetric == "MeanSquares" ||
whichMetric == "MSQ" )
{
typedef itk::HistogramMatchingImageFilter<ImageType, ImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetSourceImage(movingImage );
filter->SetReferenceImage(fixedImage);
filter->SetNumberOfHistogramLevels( 256 );
filter->SetNumberOfMatchPoints( 12 );
filter->ThresholdAtMeanIntensityOn();
// filter->ThresholdAtMeanIntensityOff();
if( useHistMatch )
{
filter->Update(); std::cout << " use Histogram Matching " << std::endl;
movingImage = filter->GetOutput();
movingImage = this->PreprocessImage(movingImage);
similarityMetric->SetMovingImage( movingImage );
}
typedef SyNDemonsRegistrationFunction
<ImageType, ImageType, DisplacementFieldType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
if( radius[0] > 0 )
{
metric->SetUseMovingImageGradient( true );
}
metric->SetIntensityDifferenceThreshold( extraparam );
metric->SetRobustnessParameter( extraparam );
// metric->SetRobust( true );
// metric->SetSymmetric( false );
// / metric->SetNormalizeGradient( false );
metric->SetRadius( radius );
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
else if( whichMetric == "SSD" )
{
if( useHistMatch )
{
movingImage = this->PreprocessImage(movingImage);
typedef itk::HistogramMatchingImageFilter<ImageType, ImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetSourceImage(movingImage );
filter->SetReferenceImage(fixedImage);
filter->SetNumberOfHistogramLevels( 256 );
filter->SetNumberOfMatchPoints( 12 );
filter->ThresholdAtMeanIntensityOn();
filter->Update(); std::cout << " use Histogram Matching " << std::endl;
movingImage = filter->GetOutput();
}
similarityMetric->SetMovingImage( movingImage );
typedef SyNDemonsRegistrationFunction
<ImageType, ImageType, DisplacementFieldType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
if( radius[0] > 0 )
{
metric->SetUseMovingImageGradient( true );
}
metric->SetIntensityDifferenceThreshold( extraparam );
metric->SetRobustnessParameter( extraparam );
metric->SetUseSSD(true);
metric->SetRadius( radius );
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
else if( whichMetric == "mutual-information" ||
whichMetric == "MutualInformation" ||
whichMetric == "MI" )
{
typedef itk::AvantsMutualInformationRegistrationFunction
<ImageType, ImageType, DisplacementFieldType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
metric->SetNumberOfHistogramBins( numberOfHistogramBins );
metric->SetNormalizeGradient( false );
metric->SetRobustnessParameter( extraparam );
unsigned int histbins = radius[0];
if( histbins < 8 )
{
histbins = 8;
}
metric->SetNumberOfHistogramBins(histbins);
radius.Fill(0);
metric->SetRadius( radius );
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
else if( whichMetric == "spatial-mutual-information" ||
whichMetric == "SpatialMutualInformation" ||
whichMetric == "SMI" )
{
typedef itk::SpatialMutualInformationRegistrationFunction
<ImageType, ImageType, DisplacementFieldType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
metric->SetNumberOfHistogramBins( numberOfHistogramBins );
metric->SetNormalizeGradient( false );
metric->SetRobustnessParameter( extraparam );
unsigned int histbins = radius[0];
if( histbins < 8 )
{
histbins = 8;
}
metric->SetNumberOfHistogramBins(histbins);
radius.Fill(0);
metric->SetRadius( radius );
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
else if( whichMetric == "cross-correlation" ||
whichMetric == "CrossCorrelation" ||
whichMetric == "CC" )
{
typedef itk::HistogramMatchingImageFilter<ImageType, ImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetSourceImage(movingImage );
filter->SetReferenceImage(fixedImage);
filter->SetNumberOfHistogramLevels( 256 );
filter->SetNumberOfMatchPoints( 12 );
filter->ThresholdAtMeanIntensityOn();
// filter->ThresholdAtMeanIntensityOff();
if( useHistMatch )
{
std::cout << " use Histogram Matching " << std::endl;
filter->Update();
movingImage = filter->GetOutput();
std::cout << " prepro " << std::endl;
movingImage = this->PreprocessImage(movingImage);
std::cout << " set " << std::endl;
similarityMetric->SetMovingImage( movingImage );
}
typedef itk::CrossCorrelationRegistrationFunction
<ImageType, ImageType, DisplacementFieldType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
metric->SetNormalizeGradient( false );
metric->SetRadius( radius );
metric->SetRobustnessParameter( extraparam );
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
else if( whichMetric == "probabilistic" ||
whichMetric == "Probabilistic" ||
whichMetric == "PR" )
{
typedef itk::HistogramMatchingImageFilter<ImageType, ImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetSourceImage(movingImage );
filter->SetReferenceImage(fixedImage);
filter->SetNumberOfHistogramLevels( 256 );
filter->SetNumberOfMatchPoints( 12 );
filter->ThresholdAtMeanIntensityOn();
// filter->ThresholdAtMeanIntensityOff();
if( useHistMatch )
{
filter->Update(); std::cout << " use Histogram Matching " << std::endl;
movingImage = filter->GetOutput();
movingImage = this->PreprocessImage(movingImage);
similarityMetric->SetMovingImage( movingImage );
}
typedef itk::ProbabilisticRegistrationFunction
<ImageType, ImageType, DisplacementFieldType> MetricType;
typename MetricType::Pointer metric = MetricType::New();
metric->SetNormalizeGradient( false );
metric->SetRadius( radius );
metric->SetRobustnessParameter( extraparam );
similarityMetric->SetMetric( metric );
similarityMetric->SetMaximizeMetric( true );
this->m_SimilarityMetrics.push_back( similarityMetric );
}
else
{
itkWarningMacro( "Could not decipher image metric choice: " << whichMetric );
}
}
}
}
else
{
itkExceptionMacro( "No image metric specified on the command line." );
}
}
template <unsigned int TDimension, class TReal>
void
PICSLAdvancedNormalizationToolKit<TDimension, TReal>
::InitializeCommandLineOptions()
{
this->m_Parser = ParserType::New();
// this->m_Parser->SetCommandDescription( " PICSL Advanced Image Normalization Toolkit" );
if( false )
{
OptionType::Pointer option = OptionType::New();
option->SetLongName( "weight-image" );
option->SetShortName( 'w' );
option->SetDescription( "blah-blah" );
this->m_Parser->AddOption( option );
}
if( true )
{
std::string description =
std::string( "this mask -- defined in the 'fixed' image space defines " )
+ std::string( "the region of interest over which the registration is " )
+ std::string( "computed ==> above 0.1 means inside mask ==> continuous " )
+ std::string( "values in range [0.1,1.0] effect optimization like a " )
+ std::string( "probability. ==> values > 1 are treated as = 1.0 " );
OptionType::Pointer option = OptionType::New();
option->SetLongName( "mask-image" );
option->SetShortName( 'x' );
// option->SetDescription( "this mask -- defined in the 'fixed' image space defines the region of interest over
// which the registration is computed ==> above 0.1 means inside mask \n\t\t==> continuous values in range [0.1 , 1.0]
// effect optimization like a probability. \n\t\t==> values > 1 are treated as = 1.0 " );
option->SetDescription( description );
option->SetUsageOption( 0, "maskFileName" );
this->m_Parser->AddOption( option );
}
if( false )
{
OptionType::Pointer option = OptionType::New();
option->SetLongName( "mask-threshold" );
option->SetShortName( 'M' );
option->SetDescription( "blah-blah" );
this->m_Parser->AddOption( option );
}
if( true )
{
OptionType::Pointer option = OptionType::New();
option->SetLongName( "image-metric" );
option->SetShortName( 'm' );
std::string newLineTabs( "\n\t\t" );
std::string intensityBasedDescription( "Intensity-Based Metrics: " );
std::string weightDescription(
"The metric weights are relative to the weights on the N other metrics passed to ANTS --- N is unlimited. So, the weight, w_i on the i^{th} metric will be w_i=w_i/ ( sum_i w_i ).");
std::string intensityBasedOptions( "[fixedImage,movingImage,weight,radius/OrForMI-#histogramBins]" );
std::string ccDescription( "CC/cross-correlation/CrossCorrelation" );
std::string miDescription( "MI/mutual-information/MutualInformation" );
std::string smiDescription( "SMI/spatial-mutual-information/SpatialMutualInformation" );
std::string prDescription( "PR/probabilistic/Probabilistic" );
std::string msqDescription(
"MSQ/mean-squares/MeanSquares -- demons-like, radius > 0 uses moving image gradient in metric deriv." );
std::string ssdDescription( "SSD --- standard intensity difference." );
intensityBasedDescription += (
newLineTabs + ccDescription + intensityBasedOptions
+ newLineTabs + miDescription + intensityBasedOptions
+ newLineTabs + smiDescription + intensityBasedOptions
+ newLineTabs + prDescription + intensityBasedOptions
+ newLineTabs + ssdDescription + intensityBasedOptions
+ newLineTabs + msqDescription + intensityBasedOptions );
std::string pointBasedDescription( "\n\t Point-Set-Based Metrics: " );
std::string pointBasedOptions
= std::string( "[fixedImage,movingImage,fixedPoints,movingPoints" )