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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "antsUtilities.h"
#include "antsAllocImage.h"
#include "ReadWriteData.h"
#include "antsCommandLineParser.h"
#include "itkCSVNumericObjectFileWriter.h"
#include "itkImageRegistrationMethodv4.h"
#include "itkSyNImageRegistrationMethod.h"
#include "itkDisplacementFieldTransform.h"
#include "itkANTSNeighborhoodCorrelationImageToImageMetricv4.h"
#include "itkMeanSquaresImageToImageMetricv4.h"
#include "itkCorrelationImageToImageMetricv4.h"
#include "itkImageToImageMetricv4.h"
#include "itkMattesMutualInformationImageToImageMetricv4.h"
#include "itkImageToHistogramFilter.h"
#include "itkHistogramMatchingImageFilter.h"
#include "itkIntensityWindowingImageFilter.h"
#include "itkTransformToDisplacementFieldFilter.h"
#include "itkAffineTransform.h"
#include "itkBSplineTransform.h"
#include "itkBSplineSmoothingOnUpdateDisplacementFieldTransform.h"
#include "itkCompositeTransform.h"
#include "itkGaussianSmoothingOnUpdateDisplacementFieldTransform.h"
#include "itkIdentityTransform.h"
#include "itkEuler2DTransform.h"
#include "itkEuler3DTransform.h"
#include "itkSimilarity2DTransform.h"
#include "itkTransform.h"
#include "itkExtractImageFilter.h"
#include "itkBSplineTransformParametersAdaptor.h"
#include "itkBSplineSmoothingOnUpdateDisplacementFieldTransformParametersAdaptor.h"
#include "itkGaussianSmoothingOnUpdateDisplacementFieldTransformParametersAdaptor.h"
#include "itkTimeVaryingVelocityFieldTransformParametersAdaptor.h"
#include "itkGradientDescentOptimizerv4.h"
#include "itkConjugateGradientLineSearchOptimizerv4.h"
#include "itkQuasiNewtonOptimizerv4.h"
#include "itkHistogramMatchingImageFilter.h"
#include "itkMinimumMaximumImageCalculator.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkMacro.h"
#include "itkRegistrationParameterScalesFromPhysicalShift.h"
#include "itkResampleImageFilter.h"
#include "itkShrinkImageFilter.h"
#include "itkTimeProbe.h"
#include "itkTransformFileReader.h"
#include "itkTransformFileWriter.h"
#include "itkSimilarity2DTransform.h"
#include "itkSimilarity3DTransform.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkGaussianInterpolateImageFunction.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkWindowedSincInterpolateImageFunction.h"
#include "itkLabelImageGaussianInterpolateImageFunction.h"
#include "itkLabelImageGenericInterpolateImageFunction.h"
#include <sstream>
namespace ants
{
/** \class antsRegistrationCommandIterationUpdate
* \brief change parameters between iterations of registration
*/
template <typename TFilter>
class antsSliceRegularizedRegistrationCommandIterationUpdate : public itk::Command
{
public:
using Self = antsSliceRegularizedRegistrationCommandIterationUpdate<TFilter>;
using Superclass = itk::Command;
using Pointer = itk::SmartPointer<Self>;
itkNewMacro(Self);
protected:
antsSliceRegularizedRegistrationCommandIterationUpdate() { this->m_LogStream = &std::cout; }
public:
void
Execute(itk::Object * caller, const itk::EventObject & event) override
{
Execute((const itk::Object *)caller, event);
}
void
Execute(const itk::Object * object, const itk::EventObject & event) override
{
auto * filter = const_cast<TFilter *>(dynamic_cast<const TFilter *>(object));
unsigned int currentLevel = 0;
if (typeid(event) == typeid(itk::IterationEvent))
{
currentLevel = filter->GetCurrentLevel() + 1;
}
if (currentLevel < this->m_NumberOfIterations.size())
{
typename TFilter::ShrinkFactorsPerDimensionContainerType shrinkFactors =
filter->GetShrinkFactorsPerDimension(currentLevel);
typename TFilter::SmoothingSigmasArrayType smoothingSigmas = filter->GetSmoothingSigmasPerLevel();
typename TFilter::TransformParametersAdaptorsContainerType adaptors =
filter->GetTransformParametersAdaptorsPerLevel();
this->Logger() << " Current level = " << currentLevel << std::endl;
this->Logger() << " number of iterations = " << this->m_NumberOfIterations[currentLevel] << std::endl;
this->Logger() << " shrink factors = " << shrinkFactors << std::endl;
this->Logger() << " smoothing sigmas = " << smoothingSigmas[currentLevel] << std::endl;
this->Logger() << " required fixed parameters = " << adaptors[currentLevel]->GetRequiredFixedParameters()
<< std::endl;
using GradientDescentOptimizerType = itk::ConjugateGradientLineSearchOptimizerv4;
auto * optimizer = reinterpret_cast<GradientDescentOptimizerType *>(filter->GetModifiableOptimizer());
optimizer->SetNumberOfIterations(this->m_NumberOfIterations[currentLevel]);
optimizer->SetMinimumConvergenceValue(1.e-7);
optimizer->SetConvergenceWindowSize(10);
optimizer->SetLowerLimit(0);
optimizer->SetUpperLimit(2);
optimizer->SetEpsilon(0.1);
}
}
void
SetNumberOfIterations(const std::vector<unsigned int> & iterations)
{
this->m_NumberOfIterations = iterations;
}
void
SetLogStream(std::ostream & logStream)
{
this->m_LogStream = &logStream;
}
private:
std::ostream &
Logger() const
{
return *m_LogStream;
}
std::vector<unsigned int> m_NumberOfIterations;
std::ostream * m_LogStream;
};
template <typename T>
inline std::string
ants_slice_regularized_to_string(const T & t)
{
std::stringstream ss;
ss << t;
return ss.str();
}
template <typename ImageType>
typename ImageType::Pointer
sliceRegularizedPreprocessImage(ImageType * inputImage,
typename ImageType::PixelType lowerScaleFunction,
typename ImageType::PixelType upperScaleFunction,
float winsorizeLowerQuantile,
float winsorizeUpperQuantile,
ImageType * histogramMatchSourceImage = nullptr)
{
using HistogramFilterType = itk::Statistics::ImageToHistogramFilter<ImageType>;
using InputBooleanObjectType = typename HistogramFilterType::InputBooleanObjectType;
using HistogramSizeType = typename HistogramFilterType::HistogramSizeType;
HistogramSizeType histogramSize(1);
histogramSize[0] = 256;
typename InputBooleanObjectType::Pointer autoMinMaxInputObject = InputBooleanObjectType::New();
autoMinMaxInputObject->Set(true);
typename HistogramFilterType::Pointer histogramFilter = HistogramFilterType::New();
histogramFilter->SetInput(inputImage);
histogramFilter->SetAutoMinimumMaximumInput(autoMinMaxInputObject);
histogramFilter->SetHistogramSize(histogramSize);
histogramFilter->SetMarginalScale(10.0);
histogramFilter->Update();
float lowerFunction = histogramFilter->GetOutput()->Quantile(0, winsorizeLowerQuantile);
float upperFunction = histogramFilter->GetOutput()->Quantile(0, winsorizeUpperQuantile);
using IntensityWindowingImageFilterType = itk::IntensityWindowingImageFilter<ImageType, ImageType>;
typename IntensityWindowingImageFilterType::Pointer windowingFilter = IntensityWindowingImageFilterType::New();
windowingFilter->SetInput(inputImage);
windowingFilter->SetWindowMinimum(lowerFunction);
windowingFilter->SetWindowMaximum(upperFunction);
windowingFilter->SetOutputMinimum(lowerScaleFunction);
windowingFilter->SetOutputMaximum(upperScaleFunction);
windowingFilter->Update();
typename ImageType::Pointer outputImage = nullptr;
if (histogramMatchSourceImage)
{
using HistogramMatchingFilterType = itk::HistogramMatchingImageFilter<ImageType, ImageType>;
typename HistogramMatchingFilterType::Pointer matchingFilter = HistogramMatchingFilterType::New();
matchingFilter->SetSourceImage(windowingFilter->GetOutput());
matchingFilter->SetReferenceImage(histogramMatchSourceImage);
matchingFilter->SetNumberOfHistogramLevels(64);
matchingFilter->SetNumberOfMatchPoints(12);
matchingFilter->ThresholdAtMeanIntensityOn();
matchingFilter->Update();
outputImage = matchingFilter->GetOutput();
outputImage->Update();
outputImage->DisconnectPipeline();
using CalculatorType = itk::MinimumMaximumImageCalculator<ImageType>;
typename CalculatorType::Pointer calc = CalculatorType::New();
calc->SetImage(inputImage);
calc->ComputeMaximum();
calc->ComputeMinimum();
if (itk::Math::abs(calc->GetMaximum() - calc->GetMinimum()) < static_cast<float>(1.e-9))
{
std::cout << "Warning: bad time point - too little intensity variation" << calc->GetMinimum() << " "
<< calc->GetMaximum() << std::endl;
return nullptr;
}
}
else
{
outputImage = windowingFilter->GetOutput();
outputImage->Update();
outputImage->DisconnectPipeline();
}
return outputImage;
}
template <typename T>
struct ants_slice_regularized_index_cmp
{
ants_slice_regularized_index_cmp(const T _arr)
: arr(_arr)
{}
bool
operator()(const size_t a, const size_t b) const
{
return arr[a] < arr[b];
}
const T arr;
};
template <typename TFilter>
class CommandIterationUpdate final : public itk::Command
{
public:
using Self = CommandIterationUpdate<TFilter>;
using Superclass = itk::Command;
using Pointer = itk::SmartPointer<Self>;
itkNewMacro(Self);
protected:
CommandIterationUpdate() = default;
public:
void
Execute(itk::Object * caller, const itk::EventObject & event) override
{
Execute((const itk::Object *)caller, event);
}
void
Execute(const itk::Object * object, const itk::EventObject & event) override
{
auto * filter = const_cast<TFilter *>(dynamic_cast<const TFilter *>(object));
if (typeid(event) != typeid(itk::IterationEvent))
{
return;
}
unsigned int currentLevel = filter->GetCurrentLevel();
typename TFilter::TransformParametersAdaptorsContainerType adaptors =
filter->GetTransformParametersAdaptorsPerLevel();
using OptimizerType = itk::ConjugateGradientLineSearchOptimizerv4;
auto * optimizer = reinterpret_cast<OptimizerType *>(filter->GetModifiableOptimizer());
optimizer->SetNumberOfIterations(this->m_NumberOfIterations[currentLevel]);
optimizer->SetMinimumConvergenceValue(1.e-7);
optimizer->SetConvergenceWindowSize(10);
optimizer->SetLowerLimit(0);
optimizer->SetUpperLimit(2);
optimizer->SetEpsilon(0.1);
}
void
SetNumberOfIterations(std::vector<unsigned int> iterations)
{
this->m_NumberOfIterations = iterations;
}
private:
std::vector<unsigned int> m_NumberOfIterations;
};
void
ants_slice_poly_regularize(vnl_matrix<double> A,
unsigned int timedims,
vnl_vector<double> & solnx,
double & interceptx,
vnl_matrix<double> param_values,
unsigned int whichCol)
{
using RealType = double;
using vVector = vnl_vector<RealType>;
for (unsigned int z = 0; z < timedims; z++)
{
auto zz = static_cast<RealType>(z + 1);
A(z, 0) = zz;
for (unsigned int lcol = 1; lcol < A.cols(); lcol++)
{
A(z, lcol) = std::pow(zz, static_cast<RealType>(lcol + 1));
}
}
for (unsigned int lcol = 0; lcol < A.cols(); lcol++)
{
vVector acol = A.get_column(lcol);
RealType acolsd = (acol - acol.mean()).rms();
A.set_column(lcol, (acol - acol.mean()) / acolsd);
}
vnl_svd<double> svd(A);
vVector ob = param_values.get_column(whichCol);
vVector polyx = svd.solve(ob);
interceptx = param_values.get_column(whichCol).mean();
for (unsigned int Acol = 0; Acol < A.cols(); Acol++)
{
interceptx -= A.get_column(Acol).mean() * polyx(Acol);
}
solnx = A * polyx + interceptx;
}
template <unsigned int ImageDimension, typename TXType>
int
ants_slice_regularized_registration(itk::ants::CommandLineParser * parser)
{
// We infer the number of stages by the number of transformations
// specified by the user which should match the number of metrics.
unsigned numberOfStages = 0;
using PixelType = float;
using RealType = double;
using FixedIOImageType = itk::Image<PixelType, ImageDimension>;
using FixedImageType = itk::Image<PixelType, ImageDimension - 1>;
using MovingIOImageType = itk::Image<PixelType, ImageDimension>;
using MovingImageType = itk::Image<PixelType, ImageDimension - 1>;
using VectorIOType = itk::Vector<RealType, ImageDimension>;
using DisplacementIOFieldType = itk::Image<VectorIOType, ImageDimension>;
using VectorType = itk::Vector<RealType, ImageDimension - 1>;
using DisplacementFieldType = itk::Image<VectorType, ImageDimension - 1>;
using vMatrix = vnl_matrix<RealType>;
using vVector = vnl_vector<RealType>;
vMatrix param_values;
using OptionType = typename itk::ants::CommandLineParser::OptionType;
typename OptionType::Pointer transformOption = parser->GetOption("transform");
if (transformOption && transformOption->GetNumberOfFunctions())
{
numberOfStages = transformOption->GetNumberOfFunctions();
}
else
{
std::cerr << "No transformations are specified." << std::endl;
return EXIT_FAILURE;
}
if (numberOfStages != 1)
{
std::cerr << "Must specify one and only one stage." << std::endl;
return EXIT_FAILURE;
}
typename OptionType::Pointer metricOption = parser->GetOption("metric");
if (!metricOption || metricOption->GetNumberOfFunctions() != numberOfStages)
{
std::cerr << "The number of metrics specified does not match the number of stages." << std::endl;
return EXIT_FAILURE;
}
std::string whichInterpolator("linear");
typename itk::ants::CommandLineParser::OptionType::Pointer interpolationOption = parser->GetOption("interpolation");
if (interpolationOption && interpolationOption->GetNumberOfFunctions())
{
whichInterpolator = interpolationOption->GetFunction(0)->GetName();
ConvertToLowerCase(whichInterpolator);
}
using ImageType = MovingImageType;
typename ImageType::SpacingType cache_spacing_for_smoothing_sigmas(
itk::NumericTraits<typename ImageType::SpacingType::ValueType>::ZeroValue());
const unsigned int VImageDimension = ImageDimension - 1;
#include "make_interpolator_snip.tmpl"
typename OptionType::Pointer iterationsOption = parser->GetOption("iterations");
if (!iterationsOption || iterationsOption->GetNumberOfFunctions() != numberOfStages)
{
std::cerr << "The number of iteration sets specified does not match the number of stages." << std::endl;
return EXIT_FAILURE;
}
typename OptionType::Pointer shrinkFactorsOption = parser->GetOption("shrinkFactors");
if (!shrinkFactorsOption || shrinkFactorsOption->GetNumberOfFunctions() != numberOfStages)
{
std::cerr << "The number of shrinkFactor sets specified does not match the number of stages." << std::endl;
return EXIT_FAILURE;
}
typename OptionType::Pointer smoothingSigmasOption = parser->GetOption("smoothingSigmas");
if (!smoothingSigmasOption || smoothingSigmasOption->GetNumberOfFunctions() != numberOfStages)
{
std::cerr << "The number of smoothing sigma sets specified does not match the number of stages." << std::endl;
return EXIT_FAILURE;
}
typename OptionType::Pointer outputOption = parser->GetOption("output");
if (!outputOption)
{
std::cerr << "Output option not specified." << std::endl;
return EXIT_FAILURE;
}
std::string outputPrefix = outputOption->GetFunction(0)->GetParameter(0);
if (outputPrefix.length() < 3)
{
outputPrefix = outputOption->GetFunction(0)->GetName();
}
std::string maskfn = std::string("");
typename OptionType::Pointer maskOption = parser->GetOption("mask");
if (maskOption->GetNumberOfFunctions() > 0)
{
maskfn = maskOption->GetFunction(0)->GetName();
}
bool doEstimateLearningRateOnce(true);
itk::TimeProbe totalTimer;
totalTimer.Start();
using TranslationRegistrationType = itk::ImageRegistrationMethodv4<FixedImageType, MovingImageType, TXType>;
// We iterate backwards because the command line options are stored as a stack (first in last out)
using SingleTransformItemType = typename TXType::Pointer;
std::vector<SingleTransformItemType> transformList;
std::vector<SingleTransformItemType> transformUList;
std::vector<typename FixedImageType::Pointer> fixedSliceList;
std::vector<typename FixedImageType::Pointer> movingSliceList;
typename FixedIOImageType::Pointer maskImage;
using ImageMaskType = itk::Image<unsigned char, ImageDimension - 1>;
typename ImageMaskType::Pointer mask_time_slice = nullptr;
if (maskfn.length() > 3)
ReadImage<FixedIOImageType>(maskImage, maskfn.c_str());
for (int currentStage = numberOfStages - 1; currentStage >= 0; currentStage--)
{
std::stringstream currentStageString;
currentStageString << currentStage;
// Get the fixed and moving images
std::string fixedImageFileName = metricOption->GetFunction(currentStage)->GetParameter(0);
std::string movingImageFileName = metricOption->GetFunction(currentStage)->GetParameter(1);
typename FixedImageType::Pointer fixed_time_slice = nullptr;
typename FixedImageType::Pointer moving_time_slice = nullptr;
typename FixedIOImageType::Pointer fixedImage;
ReadImage<FixedIOImageType>(fixedImage, fixedImageFileName.c_str());
unsigned int timedims = fixedImage->GetLargestPossibleRegion().GetSize()[ImageDimension - 1];
unsigned int nparams = TXType::New()->GetNumberOfParameters();
param_values.set_size(timedims, nparams);
param_values.fill(0);
typename MovingIOImageType::Pointer movingImage;
ReadImage<MovingIOImageType>(movingImage, movingImageFileName.c_str());
unsigned int moving_timedims = movingImage->GetLargestPossibleRegion().GetSize()[ImageDimension - 1];
if (timedims != moving_timedims)
{
std::cerr << "We require that the n^th dimensions of the fixed and moving image are equal" << std::endl;
return EXIT_FAILURE;
}
typename FixedIOImageType::Pointer outputImage;
ReadImage<FixedIOImageType>(outputImage, fixedImageFileName.c_str());
outputImage->FillBuffer(0);
// Get the number of iterations and use that information to specify the number of levels
std::vector<unsigned int> iterations =
parser->ConvertVector<unsigned int>(iterationsOption->GetFunction(currentStage)->GetName());
unsigned int numberOfLevels = iterations.size();
// Get shrink factors
std::vector<unsigned int> factors =
parser->ConvertVector<unsigned int>(shrinkFactorsOption->GetFunction(currentStage)->GetName());
typename TranslationRegistrationType::ShrinkFactorsArrayType shrinkFactorsPerLevel;
shrinkFactorsPerLevel.SetSize(factors.size());
if (factors.size() != numberOfLevels)
{
std::cerr << "ERROR: The number of shrink factors does not match the number of levels." << std::endl;
return EXIT_FAILURE;
}
else
{
for (unsigned int n = 0; n < shrinkFactorsPerLevel.Size(); n++)
{
shrinkFactorsPerLevel[n] = factors[n];
}
}
// Get smoothing sigmas
std::vector<float> sigmas =
parser->ConvertVector<float>(smoothingSigmasOption->GetFunction(currentStage)->GetName());
typename TranslationRegistrationType::SmoothingSigmasArrayType smoothingSigmasPerLevel;
smoothingSigmasPerLevel.SetSize(sigmas.size());
if (sigmas.size() != numberOfLevels)
{
std::cerr << "ERROR: The number of smoothing sigmas does not match the number of levels." << std::endl;
return EXIT_FAILURE;
}
else
{
for (unsigned int n = 0; n < smoothingSigmasPerLevel.Size(); n++)
{
smoothingSigmasPerLevel[n] = sigmas[n];
}
}
bool verbose = false;
if (parser->Convert<bool>(parser->GetOption('v')->GetFunction()->GetName()))
{
verbose = true;
}
std::vector<unsigned int> polydegree;
itk::ants::CommandLineParser::OptionType::Pointer polyOption = parser->GetOption("polydegree");
if (polyOption && polyOption->GetNumberOfFunctions())
{
polydegree = parser->ConvertVector<unsigned int>(polyOption->GetFunction(0)->GetName());
}
if ((polydegree.size() != nparams))
{
polydegree.resize(nparams, polydegree[0]);
}
for (unsigned int & pind : polydegree)
{
if (pind > (timedims - 2))
pind = timedims - 2;
}
// the fixed image slice is a reference image in 2D while the moving is a 2D slice image
// loop over every time point and register image_i_moving to image_i_fixed
//
// Set up the image metric and scales estimator
for (unsigned int timedim = 0; timedim < timedims; timedim++)
{
using IdentityTransformType = itk::IdentityTransform<RealType, ImageDimension - 1>;
typename IdentityTransformType::Pointer identityTransform = IdentityTransformType::New();
using ExtractFilterType = itk::ExtractImageFilter<FixedIOImageType, FixedImageType>;
typename FixedIOImageType::RegionType extractRegion = movingImage->GetLargestPossibleRegion();
extractRegion.SetSize(ImageDimension - 1, 0);
extractRegion.SetIndex(ImageDimension - 1, timedim);
typename ExtractFilterType::Pointer extractFilterF = ExtractFilterType::New();
typename ExtractFilterType::Pointer extractFilterM = ExtractFilterType::New();
extractFilterF->SetInput(fixedImage);
extractFilterM->SetInput(movingImage);
extractFilterF->SetDirectionCollapseToSubmatrix();
extractFilterM->SetDirectionCollapseToSubmatrix();
bool toidentity = false;
if (toidentity)
extractFilterF->SetDirectionCollapseToIdentity();
if (toidentity)
extractFilterM->SetDirectionCollapseToIdentity();
extractFilterF->SetExtractionRegion(extractRegion);
extractFilterM->SetExtractionRegion(extractRegion);
extractFilterF->Update();
extractFilterM->Update();
fixed_time_slice = extractFilterF->GetOutput();
moving_time_slice = extractFilterM->GetOutput();
fixedSliceList.push_back(fixed_time_slice);
movingSliceList.push_back(moving_time_slice);
if (maskfn.length() > 3)
{
using ExtractFilterTypeX = itk::ExtractImageFilter<FixedIOImageType, ImageMaskType>;
typename ExtractFilterTypeX::Pointer extractFilterX = ExtractFilterTypeX::New();
extractFilterX->SetInput(maskImage);
extractFilterX->SetDirectionCollapseToSubmatrix();
if (toidentity)
extractFilterX->SetDirectionCollapseToIdentity();
extractFilterX->SetExtractionRegion(extractRegion);
extractFilterX->Update();
mask_time_slice = extractFilterX->GetOutput();
}
// set up initial transform parameters
typename TXType::Pointer translationTransform = TXType::New();
translationTransform->SetIdentity();
transformList.push_back(translationTransform);
// set up update transform parameters
typename TXType::Pointer translationTransformU = TXType::New();
translationTransformU->SetIdentity();
transformUList.push_back(translationTransformU);
}
// implement a gradient descent on the polynomial parameters by looping over registration results
using MetricType = itk::ImageToImageMetricv4<FixedImageType, FixedImageType>;
typename MetricType::Pointer metric;
unsigned int maxloop = 2;
for (unsigned int loop = 0; loop < maxloop; loop++)
{
RealType metricval = 0;
bool skipThisTimePoint = false;
for (unsigned int timedim = 0; timedim < timedims; timedim++)
{
typename FixedImageType::Pointer preprocessFixedImage =
sliceRegularizedPreprocessImage<FixedImageType>(fixedSliceList[timedim], 0, 1, 0.005, 0.995, nullptr);
typename FixedImageType::Pointer preprocessMovingImage = sliceRegularizedPreprocessImage<FixedImageType>(
movingSliceList[timedim], 0, 1, 0.005, 0.995, preprocessFixedImage);
if (preprocessFixedImage.IsNull() || preprocessMovingImage.IsNull())
{
preprocessFixedImage = fixedSliceList[timedim];
preprocessMovingImage = movingSliceList[timedim];
skipThisTimePoint = true;
}
std::string whichMetric = metricOption->GetFunction(currentStage)->GetName();
ConvertToLowerCase(whichMetric);
float samplingPercentage = 1.0;
if (metricOption->GetFunction(0)->GetNumberOfParameters() > 5)
{
samplingPercentage = parser->Convert<float>(metricOption->GetFunction(currentStage)->GetParameter(5));
}
std::string samplingStrategy = "";
if (metricOption->GetFunction(0)->GetNumberOfParameters() > 4)
{
samplingStrategy = metricOption->GetFunction(currentStage)->GetParameter(4);
}
ConvertToLowerCase(samplingStrategy);
itk::ImageRegistrationMethodv4Enums::MetricSamplingStrategy metricSamplingStrategy =
itk::ImageRegistrationMethodv4Enums::MetricSamplingStrategy::NONE;
if (std::strcmp(samplingStrategy.c_str(), "random") == 0)
{
metricSamplingStrategy = itk::ImageRegistrationMethodv4Enums::MetricSamplingStrategy::RANDOM;
}
if (std::strcmp(samplingStrategy.c_str(), "regular") == 0)
{
metricSamplingStrategy = itk::ImageRegistrationMethodv4Enums::MetricSamplingStrategy::REGULAR;
}
if (std::strcmp(whichMetric.c_str(), "cc") == 0)
{
auto radiusOption = parser->Convert<unsigned int>(metricOption->GetFunction(currentStage)->GetParameter(3));
using CorrelationMetricType =
itk::ANTSNeighborhoodCorrelationImageToImageMetricv4<FixedImageType, FixedImageType>;
typename CorrelationMetricType::Pointer correlationMetric = CorrelationMetricType::New();
typename CorrelationMetricType::RadiusType radius;
radius.Fill(radiusOption);
correlationMetric->SetRadius(radius);
correlationMetric->SetUseMovingImageGradientFilter(false);
correlationMetric->SetUseFixedImageGradientFilter(false);
metric = correlationMetric;
}
else if (std::strcmp(whichMetric.c_str(), "mi") == 0)
{
auto binOption = parser->Convert<unsigned int>(metricOption->GetFunction(currentStage)->GetParameter(3));
using MutualInformationMetricType =
itk::MattesMutualInformationImageToImageMetricv4<FixedImageType, FixedImageType>;
typename MutualInformationMetricType::Pointer mutualInformationMetric = MutualInformationMetricType::New();
// mutualInformationMetric = mutualInformationMetric;
mutualInformationMetric->SetNumberOfHistogramBins(binOption);
mutualInformationMetric->SetUseMovingImageGradientFilter(false);
mutualInformationMetric->SetUseFixedImageGradientFilter(false);
metric = mutualInformationMetric;
}
else if (std::strcmp(whichMetric.c_str(), "meansquares") == 0)
{
using MSQMetricType = itk::MeanSquaresImageToImageMetricv4<FixedImageType, FixedImageType>;
typename MSQMetricType::Pointer demonsMetric = MSQMetricType::New();
// demonsMetric = demonsMetric;
metric = demonsMetric;
}
else if (std::strcmp(whichMetric.c_str(), "gc") == 0)
{
using corrMetricType = itk::CorrelationImageToImageMetricv4<FixedImageType, FixedImageType>;
typename corrMetricType::Pointer corrMetric = corrMetricType::New();
metric = corrMetric;
}
else
{
std::cerr << "ERROR: Unrecognized image metric: " << whichMetric << std::endl;
return EXIT_FAILURE;
}
metric->SetVirtualDomainFromImage(fixedSliceList[timedim]);
if (maskfn.length() > 3)
{
using spMaskType = itk::ImageMaskSpatialObject<ImageDimension - 1>;
typename spMaskType::Pointer spatialObjectMask = spMaskType::New();
spatialObjectMask->SetImage(mask_time_slice);
metric->SetFixedImageMask(spatialObjectMask);
if ((verbose) && (loop == 0) && (timedim == 0))
std::cout << " setting mask " << maskfn << std::endl;
}
using ScalesEstimatorType = itk::RegistrationParameterScalesFromPhysicalShift<MetricType>;
typename ScalesEstimatorType::Pointer scalesEstimator = ScalesEstimatorType::New();
scalesEstimator->SetMetric(metric);
scalesEstimator->SetTransformForward(true);
auto learningRate = parser->Convert<float>(transformOption->GetFunction(currentStage)->GetParameter(0));
using OptimizerType = itk::ConjugateGradientLineSearchOptimizerv4;
typename OptimizerType::Pointer optimizer = OptimizerType::New();
optimizer->SetNumberOfIterations(iterations[0]);
optimizer->SetMinimumConvergenceValue(1.e-7);
optimizer->SetConvergenceWindowSize(8);
optimizer->SetLowerLimit(0);
optimizer->SetUpperLimit(2);
optimizer->SetEpsilon(0.1);
optimizer->SetScalesEstimator(scalesEstimator);
optimizer->SetMaximumStepSizeInPhysicalUnits(learningRate);
optimizer->SetDoEstimateLearningRateOnce(doEstimateLearningRateOnce);
optimizer->SetDoEstimateLearningRateAtEachIteration(!doEstimateLearningRateOnce);
// Set up the image registration methods along with the transforms
std::string whichTransform = transformOption->GetFunction(currentStage)->GetName();
ConvertToLowerCase(whichTransform);
typename TranslationRegistrationType::Pointer translationRegistration = TranslationRegistrationType::New();
if ((std::strcmp(whichTransform.c_str(), "translation") == 0) ||
(std::strcmp(whichTransform.c_str(), "rigid") == 0) ||
(std::strcmp(whichTransform.c_str(), "similarity") == 0))
{
transformList[timedim]->GetNumberOfParameters();
metric->SetFixedImage(preprocessFixedImage);
metric->SetVirtualDomainFromImage(preprocessFixedImage);
metric->SetMovingImage(preprocessMovingImage);
metric->SetMovingTransform(transformList[timedim]);
typename ScalesEstimatorType::ScalesType scales(transformList[timedim]->GetNumberOfParameters());
typename MetricType::ParametersType newparams(transformList[timedim]->GetParameters());
metric->SetParameters(newparams);
metric->Initialize();
scalesEstimator->SetMetric(metric);
scalesEstimator->EstimateScales(scales);
optimizer->SetScales(scales);
translationRegistration->SetFixedImage(preprocessFixedImage);
translationRegistration->SetMovingImage(preprocessMovingImage);
translationRegistration->SetNumberOfLevels(numberOfLevels);
translationRegistration->SetShrinkFactorsPerLevel(shrinkFactorsPerLevel);
translationRegistration->SetSmoothingSigmasPerLevel(smoothingSigmasPerLevel);
translationRegistration->SetMetricSamplingStrategy(metricSamplingStrategy);
translationRegistration->SetMetricSamplingPercentage(samplingPercentage);
translationRegistration->SetMetric(metric);
translationRegistration->SetOptimizer(optimizer);
using TranslationCommandType = CommandIterationUpdate<TranslationRegistrationType>;
typename TranslationCommandType::Pointer translationObserver = TranslationCommandType::New();
translationObserver->SetNumberOfIterations(iterations);
translationRegistration->AddObserver(itk::IterationEvent(), translationObserver);
if (!skipThisTimePoint)
try
{
translationRegistration->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "Exception caught: " << e << std::endl;
return EXIT_FAILURE;
}
}
else
{
std::cerr << "ERROR: Unrecognized transform option - " << whichTransform << std::endl;
return EXIT_FAILURE;
}
transformUList[timedim] = translationRegistration->GetModifiableTransform();
metricval += metric->GetValue();
}
for (unsigned int i = 0; i < transformList.size(); i++)
{
typename TXType::ParametersType pu = transformUList[i]->GetParameters();
for (unsigned int kk = 0; kk < pu.Size(); kk++)
param_values(i, kk) = pu[kk];
}
// project updated solution back to polynomial space
vVector solnx;
RealType interceptx = 0;
if (polydegree[0] > 0)
{
// set up polynomial system of equations
vMatrix A(timedims, polydegree[0], 0.0);
ants_slice_poly_regularize(A, timedims, solnx, interceptx, param_values, 0);
}
// now y-dimension
vVector solny;
RealType intercepty = 0;
if (polydegree[1] > 0)
{
vMatrix A(timedims, polydegree[1], 0.0);
ants_slice_poly_regularize(A, timedims, solny, intercepty, param_values, 1);
}
// now rotation
vVector solnr;
RealType interceptr = 0;
if (nparams >= 3)
if (polydegree[2] > 0)
{
vMatrix A(timedims, polydegree[2], 0.0);
ants_slice_poly_regularize(A, timedims, solnr, interceptr, param_values, 2);
}
// now scaling-dimension
vVector solns;
RealType intercepts = 0;
if (nparams >= 4)
if (polydegree[3] > 0)
{
vMatrix A(timedims, polydegree[3], 0.0);
ants_slice_poly_regularize(A, timedims, solns, intercepts, param_values, 3);
}
// now look at delta and do projection
if ((solnx.size() != transformList.size()) && (polydegree[0] > 0))
std::cerr << "solnx.size() != transformList.size()" << std::endl;
if ((solny.size() != transformList.size()) && (polydegree[1] > 0))
std::cerr << "solny.size() != transformList.size()" << std::endl;
RealType err = 0;
for (unsigned int i = 0; i < transformList.size(); i++)
{
/** FIXME - this should be vectorized: DRY */
typename TXType::ParametersType pOld = transformList[i]->GetParameters();
typename TXType::ParametersType p = transformUList[i]->GetParameters();
if (polydegree[0] > 0)
p[0] = solnx[i];
if (polydegree[1] > 0)
p[1] = solny[i];
param_values(i, 0) = p[0];
param_values(i, 1) = p[1];
if (nparams >= 3)
{
if (polydegree[2] > 0)
p[2] = solnr[i];
param_values(i, 2) = p[2];
}
if (nparams >= 4)
{
if (polydegree[3] > 0)
p[3] = solns[i];
param_values(i, 3) = p[3];
}
transformList[i]->SetParameters(p);
transformUList[i]->SetParameters(p);
err += (p - pOld).rms();
}
err = err / static_cast<double>(transformList.size());
if (verbose)
{
std::cout << "Loop" << loop << " polyerr: " << err << " image-metric " << metricval << std::endl;
}
// transformList = transformUList;
for (unsigned int i = 0; i < transformList.size(); i++)
{
typename TXType::ParametersType p = transformList[i]->GetParameters();
if (polydegree[0] == 0)
param_values(i, 0) = p[0];
if (polydegree[1] == 0)
param_values(i, 1) = p[1];
if (nparams >= 3)
if (polydegree[2] == 0)
param_values(i, 2) = p[2];
if (nparams >= 4)
if (polydegree[3] == 0)
param_values(i, 3) = p[3];
}
} // done with optimization, now move on to writing data ... closes for ( unsigned int loop = 0; loop < maxloop;
// loop++ )
// write polynomial predicted data
{
std::vector<std::string> ColumnHeaders;
std::string colname;
colname = std::string("Tx");
ColumnHeaders.push_back(colname);
colname = std::string("Ty");
ColumnHeaders.push_back(colname);
if (nparams >= 3)
{
colname = std::string("Tr");
ColumnHeaders.push_back(colname);
}
if (nparams >= 4)
{
colname = std::string("Ts");
ColumnHeaders.push_back(colname);
}
using WriterType = itk::CSVNumericObjectFileWriter<double, 1, 1>;
WriterType::Pointer writer = WriterType::New();
std::string fnmp;
fnmp = outputPrefix + std::string("TxTy_poly.csv");
writer->SetFileName(fnmp.c_str());
writer->SetColumnHeaders(ColumnHeaders);
writer->SetInput(¶m_values);
writer->Write();
}
/** Handle all output: images and displacement fields */
using IdentityIOTransformType = itk::IdentityTransform<RealType, ImageDimension>;
typename IdentityIOTransformType::Pointer identityIOTransform = IdentityIOTransformType::New();
using ConverterType = typename itk::TransformToDisplacementFieldFilter<DisplacementIOFieldType, RealType>;
typename ConverterType::Pointer idconverter = ConverterType::New();
idconverter->SetOutputOrigin(outputImage->GetOrigin());
idconverter->SetOutputStartIndex(outputImage->GetBufferedRegion().GetIndex());
idconverter->SetSize(outputImage->GetBufferedRegion().GetSize());
idconverter->SetOutputSpacing(outputImage->GetSpacing());
idconverter->SetOutputDirection(outputImage->GetDirection());
idconverter->SetTransform(identityIOTransform);
idconverter->Update();
typename DisplacementIOFieldType::Pointer displacementout = idconverter->GetOutput();
typename DisplacementIOFieldType::Pointer displacementinv = DisplacementIOFieldType::New();
displacementinv->CopyInformation(displacementout);
displacementinv->SetRegions(displacementout->GetRequestedRegion());
displacementinv->AllocateInitialized();
typename DisplacementIOFieldType::IndexType dind;
dind.Fill(0);
displacementinv->FillBuffer(displacementout->GetPixel(dind));
for (unsigned int timedim = 0; timedim < timedims; timedim++)
{
using _ConverterType = typename itk::TransformToDisplacementFieldFilter<DisplacementFieldType, RealType>;
typename _ConverterType::Pointer converter = _ConverterType::New();
converter->SetOutputOrigin(fixedSliceList[timedim]->GetOrigin());
converter->SetOutputStartIndex(fixedSliceList[timedim]->GetBufferedRegion().GetIndex());
converter->SetSize(fixedSliceList[timedim]->GetBufferedRegion().GetSize());
converter->SetOutputSpacing(fixedSliceList[timedim]->GetSpacing());
converter->SetOutputDirection(fixedSliceList[timedim]->GetDirection());
converter->SetTransform(transformList[timedim]);
converter->Update();
// resample the moving image and then put it in its place
interpolator->SetInputImage(movingSliceList[timedim]);
using ResampleFilterType = itk::ResampleImageFilter<FixedImageType, FixedImageType>;
typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
resampler->SetTransform(transformList[timedim]);
resampler->SetInterpolator(interpolator);
resampler->SetInput(movingSliceList[timedim]);
resampler->SetOutputParametersFromImage(fixedSliceList[timedim]);
resampler->SetDefaultPixelValue(0);
resampler->Update();
/** Here, we put the resampled 2D image into the 3D volume */
using Iterator = itk::ImageRegionIteratorWithIndex<FixedImageType>;
Iterator vfIter2(resampler->GetOutput(), resampler->GetOutput()->GetLargestPossibleRegion());
for (vfIter2.GoToBegin(); !vfIter2.IsAtEnd(); ++vfIter2)
{
typename FixedImageType::PixelType fval = vfIter2.Get();
VectorType vec = converter->GetOutput()->GetPixel(vfIter2.GetIndex());
VectorIOType vecout;
vecout.Fill(0);
typename MovingIOImageType::IndexType ind;
for (itk::SizeValueType xx = 0; xx < ImageDimension - 1; xx++)
{
ind[xx] = vfIter2.GetIndex()[xx];
vecout[xx] = vec[xx];
}
unsigned int tdim = timedim;
if (tdim > (timedims - 1))
{
tdim = timedims - 1;
}
ind[MovingIOImageType::ImageDimension - 1] = tdim;
outputImage->SetPixel(ind, fval);
displacementout->SetPixel(ind, vecout);
}
}
// now apply to the inverse map
unsigned int timedimX = 0;
for (timedimX = 0; timedimX < timedims; timedimX++)
{
using _ConverterType = typename itk::TransformToDisplacementFieldFilter<DisplacementFieldType, RealType>;
typename _ConverterType::Pointer converter = _ConverterType::New();
converter->SetOutputOrigin(movingSliceList[timedimX]->GetOrigin());
converter->SetOutputStartIndex(movingSliceList[timedimX]->GetBufferedRegion().GetIndex());
converter->SetSize(movingSliceList[timedimX]->GetBufferedRegion().GetSize());
converter->SetOutputSpacing(movingSliceList[timedimX]->GetSpacing());
converter->SetOutputDirection(movingSliceList[timedimX]->GetDirection());
typename TXType::Pointer invtx = TXType::New();
invtx->SetIdentity();
transformList[timedimX]->GetInverse(invtx);
converter->SetTransform(invtx);
converter->Update();
// resample the moving image and then put it in its place
using ResampleFilterType = itk::ResampleImageFilter<FixedImageType, FixedImageType>;
typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
resampler->SetTransform(invtx);
interpolator->SetInputImage(fixedSliceList[timedimX]);
resampler->SetInterpolator(interpolator);
resampler->SetInput(fixedSliceList[timedimX]);
resampler->SetOutputParametersFromImage(movingSliceList[timedimX]);
resampler->SetDefaultPixelValue(0);
resampler->Update();
/** Here, we put the resampled 2D image into the 3D volume */
using Iterator = itk::ImageRegionIteratorWithIndex<FixedImageType>;
Iterator vfIter2(resampler->GetOutput(), resampler->GetOutput()->GetLargestPossibleRegion());
for (vfIter2.GoToBegin(); !vfIter2.IsAtEnd(); ++vfIter2)
{
VectorType vec = converter->GetOutput()->GetPixel(vfIter2.GetIndex());
VectorIOType vecout;
vecout.Fill(0);
typename MovingIOImageType::IndexType ind;
for (itk::SizeValueType xx = 0; xx < ImageDimension - 1; xx++)
{
ind[xx] = vfIter2.GetIndex()[xx];
vecout[xx] = vec[xx];
}
unsigned int tdim = timedimX;
if (tdim > (timedims - 1))
{
tdim = timedims - 1;
}
ind[MovingIOImageType::ImageDimension - 1] = tdim;
displacementinv->SetPixel(ind, vecout);
}
}
if (outputOption && outputOption->GetFunction(0)->GetNumberOfParameters() > 1 && currentStage == 0)
{
std::string fileName = outputOption->GetFunction(0)->GetParameter(1);
if (outputPrefix.length() < 3)
{
outputPrefix = outputOption->GetFunction(0)->GetName();
}
ANTs::WriteImage<MovingIOImageType>(outputImage, fileName.c_str());
}
if (outputOption && outputOption->GetFunction(0)->GetNumberOfParameters() > 2 && outputImage && currentStage == 0)
{
std::string fileName = outputOption->GetFunction(0)->GetParameter(2);
}
{
std::string dispfn = outputPrefix + std::string("Warp.nii.gz");
using DisplacementFieldWriterType = itk::ImageFileWriter<DisplacementIOFieldType>;
typename DisplacementFieldWriterType::Pointer displacementFieldWriter = DisplacementFieldWriterType::New();
displacementFieldWriter->SetInput(displacementout);
displacementFieldWriter->SetFileName(dispfn.c_str());
displacementFieldWriter->Update();
}
{
std::string dispfn = outputPrefix + std::string("InverseWarp.nii.gz");
using DisplacementFieldWriterType = itk::ImageFileWriter<DisplacementIOFieldType>;
typename DisplacementFieldWriterType::Pointer displacementFieldWriter = DisplacementFieldWriterType::New();
displacementFieldWriter->SetInput(displacementinv);
displacementFieldWriter->SetFileName(dispfn.c_str());
displacementFieldWriter->Update();
}
}
totalTimer.Stop();
// std::cout << std::endl << "Total elapsed time: " << totalTimer.GetMean() << std::endl;
return EXIT_SUCCESS;
}
void
antsSliceRegularizedRegistrationInitializeCommandLineOptions(itk::ants::CommandLineParser * parser)
{
using OptionType = itk::ants::CommandLineParser::OptionType;
{
std::string description =
std::string("Four image metrics are available--- ") +
std::string("GC : global correlation, CC: ANTS neighborhood cross correlation, MI: Mutual information, and ") +
std::string("MeanSquares: mean-squares intensity difference. ") +
std::string("Note that the metricWeight is currently not used. ") +
std::string("Rather, it is a temporary place holder until multivariate metrics ") +
std::string("are available for a single stage.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("metric");
option->SetShortName('m');
option->SetUsageOption(0,
"CC[fixedImage,movingImage,metricWeight,radius,<samplingStrategy={Regular,Random,None}>,<"
"samplingPercentage=[0,1]>]");
option->SetUsageOption(1,
"MI[fixedImage,movingImage,metricWeight,numberOfBins,<samplingStrategy={Regular,Random,None}"
">,<samplingPercentage=[0,1]>]");
option->SetUsageOption(2,
"MeanSquares[fixedImage,movingImage,metricWeight,radius,<samplingStrategy={Regular,Random,"
"None}>,<samplingPercentage=[0,1]>]");
option->SetUsageOption(3,
"GC[fixedImage,movingImage,metricWeight,radius,<samplingStrategy={Regular,Random,None}>,<"
"samplingPercentage=[0,1]>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = "Fixed image mask to limit voxels considered by the metric.";
OptionType::Pointer option = OptionType::New();
option->SetLongName("mask");
option->SetShortName('x');
option->SetUsageOption(0, "mask-in-fixed-image-space.nii.gz");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Several interpolation options are available in ITK. ") +
std::string("These have all been made available.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("interpolation");
option->SetShortName('n');
option->SetUsageOption(0, "Linear");
option->SetUsageOption(1, "NearestNeighbor");
option->SetUsageOption(2, "MultiLabel[<sigma=imageSpacing>,<alpha=4.0>]");
option->SetUsageOption(3, "Gaussian[<sigma=imageSpacing>,<alpha=1.0>]");
option->SetUsageOption(4, "BSpline[<order=3>]");
option->SetUsageOption(5, "CosineWindowedSinc");
option->SetUsageOption(6, "WelchWindowedSinc");
option->SetUsageOption(7, "HammingWindowedSinc");
option->SetUsageOption(8, "LanczosWindowedSinc");
option->SetUsageOption(9, "GenericLabel[<interpolator=Linear>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description =
std::string("Several transform options are available. The gradientStep or") +
std::string("learningRate characterizes the gradient descent optimization and is scaled appropriately ") +
std::string("for each transform using the shift scales estimator. Subsequent parameters are ") +
std::string("transform-specific and can be determined from the usage. ");
OptionType::Pointer option = OptionType::New();
option->SetLongName("transform");
option->SetShortName('t');
option->SetUsageOption(0, "Translation[gradientStep]");
option->SetUsageOption(1, "Rigid[gradientStep]");
option->SetUsageOption(2, "Similarity[gradientStep]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Specify the number of iterations at each level.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("iterations");
option->SetShortName('i');
option->SetUsageOption(0, "MxNx0...");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Specify the amount of smoothing at each level.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("smoothingSigmas");
option->SetShortName('s');
option->SetUsageOption(0, "MxNx0...");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description =
std::string("Specify the shrink factor for the virtual domain (typically the fixed image) at each level.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("shrinkFactors");
option->SetShortName('f');
option->SetUsageOption(0, "MxNx0...");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description =
std::string("Specify the output transform prefix (output format is .nii.gz ).") +
std::string("Optionally, one can choose to warp the moving image to the fixed space and, if the ") +
std::string("inverse transform exists, one can also output the warped fixed image.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("output");
option->SetShortName('o');
option->SetUsageOption(0, "[outputTransformPrefix,<outputWarpedImage>,<outputAverageImage>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Print the help menu (short version).");
OptionType::Pointer option = OptionType::New();
option->SetLongName("help");
option->SetShortName('h');
option->SetDescription(description);
option->AddFunction(std::string("0"));
parser->AddOption(option);
}
{
std::string description = std::string("verbose option");
OptionType::Pointer option = OptionType::New();
option->SetLongName("verbose");
option->SetShortName('v');
option->SetDescription(description);
option->AddFunction(std::string("0"));
parser->AddOption(option);
}
{
std::string description =
std::string("degree of polynomial - up to zDimension-2. ") +
std::string("Controls the polynomial degree. 0 means no regularization. This may be a vector ") +
std::string("denoted by 2x2x1 for a 3-parameter transform ( e.g. rigid ). This would ") +
std::string("regularize the translation by 2nd degree polynomial and the rotation by a ") +
std::string("linear function.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("polydegree");
option->SetShortName('p');
option->SetDescription(description);
parser->AddOption(option);
}
}
// entry point for the library; parameter 'args' is equivalent to 'argv' in (argc,argv) of commandline parameters to
// 'main()'
int
antsSliceRegularizedRegistration(std::vector<std::string> args, std::ostream * /*out_stream = nullptr */)
{
// put the arguments coming in as 'args' into standard (argc,argv) format;
// 'args' doesn't have the command name as first, argument, so add it manually;
// 'args' may have adjacent arguments concatenated into one argument,
// which the parser should handle
args.insert(args.begin(), "antsSliceRegularizedRegistration");
int argc = args.size();
char ** argv = new char *[args.size() + 1];
for (unsigned int i = 0; i < args.size(); ++i)
{
// allocate space for the string plus a null character
argv[i] = new char[args[i].length() + 1];
std::strncpy(argv[i], args[i].c_str(), args[i].length());
// place the null character in the end
argv[i][args[i].length()] = '\0';
}
argv[argc] = nullptr;
// class to automatically cleanup argv upon destruction
class Cleanup_argv
{
public:
Cleanup_argv(char ** argv_, int argc_plus_one_)
: argv(argv_)
, argc_plus_one(argc_plus_one_)
{}
~Cleanup_argv()
{
for (unsigned int i = 0; i < argc_plus_one; ++i)
{
delete[] argv[i];
}
delete[] argv;
}
private:
char ** argv;
unsigned int argc_plus_one;
};
Cleanup_argv cleanup_argv(argv, argc + 1);
// antscout->set_stream( out_stream );
itk::ants::CommandLineParser::Pointer parser = itk::ants::CommandLineParser::New();
parser->SetCommand(argv[0]);
std::string commandDescription =
std::string("antsSliceRegularizedRegistration ") +
std::string("This program is a user-level application for slice-by-slice translation registration. ") +
std::string(
"Results are regularized in z using polynomial regression. The program is targeted at spinal cord MRI. ") +
std::string("Only one stage is supported where a stage consists of a transform; an image metric; ") +
std::string("and iterations, shrink factors, and smoothing sigmas for each level. ") +
std::string("Specialized for 3D data: fixed image is 3D, moving image is 3D. ") +
std::string("Registration is performed slice-by-slice then regularized in z. ") +
std::string(" The parameter -p controls the polynomial degree. -p 0 means no regularization.") +
std::string("Implemented by B. Avants and conceived by Julien Cohen-Adad.\n") + std::string("Outputs: \n\n") +
std::string(" OutputPrefixTxTy_poly.csv: polynomial fit to Tx & Ty \n") +
std::string(" OutputPrefix.nii.gz: transformed image \n") + std::string("Example call: \n\n") +
std::string(" antsSliceRegularizedRegistration -p 4 --output [OutputPrefix,OutputPrefix.nii.gz] ") +
std::string("--transform Translation[0.1] --metric MI[ fixed.nii.gz, moving.nii.gz , 1 , 16 , Regular , 0.2 ] ") +
std::string("--iterations 20 --shrinkFactors 1 --smoothingSigmas 0 \n\n");
parser->SetCommandDescription(commandDescription);
antsSliceRegularizedRegistrationInitializeCommandLineOptions(parser);
if (parser->Parse(argc, argv) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
if (argc < 2 || parser->Convert<bool>(parser->GetOption("help")->GetFunction()->GetName()))
{
parser->PrintMenu(std::cout, 5, false);
if (argc < 2)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
else if (parser->Convert<bool>(parser->GetOption('h')->GetFunction()->GetName()))
{
parser->PrintMenu(std::cout, 5, true);
return EXIT_SUCCESS;
}
// Get dimensionality
unsigned int dimension = 3;
using RealType = double;
using TranslationTransformType = itk::TranslationTransform<RealType, 2>;
using EulerTransformType = itk::Euler2DTransform<RealType>;
using SimilarityTransformType = itk::Similarity2DTransform<RealType>;
itk::ants::CommandLineParser::OptionType::Pointer transformOption = parser->GetOption("transform");
if (!(transformOption && transformOption->GetNumberOfFunctions()))
{
std::cerr << "No transformations are specified." << std::endl;
return EXIT_FAILURE;
}
std::string whichTransform = transformOption->GetFunction(0)->GetName();
ConvertToLowerCase(whichTransform);
if (std::strcmp(whichTransform.c_str(), "translation") == 0)
{
switch (dimension)
{
case 3:
{
return ants_slice_regularized_registration<3, TranslationTransformType>(parser);
}
break;
default:
std::cerr << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
else if (std::strcmp(whichTransform.c_str(), "rigid") == 0)
{
switch (dimension)
{
case 3:
{
return ants_slice_regularized_registration<3, EulerTransformType>(parser);
}
break;
default:
std::cerr << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
else if (std::strcmp(whichTransform.c_str(), "similarity") == 0)
{
switch (dimension)
{
case 3:
{
return ants_slice_regularized_registration<3, SimilarityTransformType>(parser);
}
break;
default:
std::cerr << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}
} // namespace ants
|