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
|
#include "antsUtilities.h"
#include "antsAllocImage.h"
#include "antsCommandLineParser.h"
#include "ReadWriteData.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkBSplineControlPointImageFilter.h"
#include "itkConstantPadImageFilter.h"
#include "itkExpImageFilter.h"
#include "itkExtractImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkLabelStatisticsImageFilter.h"
#include "itkN3MRIBiasFieldCorrectionImageFilter.h"
#include "itkN3BiasFieldCorrectionImageFilter.h"
#include "itkOtsuThresholdImageFilter.h"
#include "itkTimeProbe.h"
#include "itkShrinkImageFilter.h"
#include <string>
#include <algorithm>
#include <vector>
#include "ANTsVersion.h"
namespace ants
{
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
{
const auto * filter = dynamic_cast<const TFilter *>(object);
if (typeid(event) != typeid(itk::IterationEvent))
{
return;
}
std::cout << "Iteration " << filter->GetElapsedIterations() << " (of " << filter->GetMaximumNumberOfIterations()
<< "). ";
std::cout << " Current convergence value = " << filter->GetCurrentConvergenceMeasurement()
<< " (threshold = " << filter->GetConvergenceThreshold() << ")" << std::endl;
}
};
template <unsigned int ImageDimension>
int
N3BiasFieldCorrection(int argc, char * argv[])
{
using RealType = float;
using ImageType = itk::Image<RealType, ImageDimension>;
using MaskImageType = itk::Image<unsigned char, ImageDimension>;
typename ImageType::Pointer image;
ReadImage<ImageType>(image, argv[2]);
using ShrinkerType = itk::ShrinkImageFilter<ImageType, ImageType>;
typename ShrinkerType::Pointer shrinker = ShrinkerType::New();
shrinker->SetInput(image);
shrinker->SetShrinkFactors(1);
typename MaskImageType::Pointer maskImage = nullptr;
if (argc > 5)
{
ReadImage<MaskImageType>(maskImage, argv[5]);
}
if (!maskImage)
{
using ThresholderType = itk::OtsuThresholdImageFilter<ImageType, MaskImageType>;
typename ThresholderType::Pointer otsu = ThresholderType::New();
otsu->SetInput(image);
otsu->SetNumberOfHistogramBins(200);
otsu->SetInsideValue(0);
otsu->SetOutsideValue(1);
otsu->Update();
maskImage = otsu->GetOutput();
}
using MaskShrinkerType = itk::ShrinkImageFilter<MaskImageType, MaskImageType>;
typename MaskShrinkerType::Pointer maskshrinker = MaskShrinkerType::New();
maskshrinker->SetInput(maskImage);
maskshrinker->SetShrinkFactors(1);
// 4 is [shrinkFactor]
// 5 is [maskImage]
if (argc > 4)
{
shrinker->SetShrinkFactors(std::stoi(argv[4]));
maskshrinker->SetShrinkFactors(std::stoi(argv[4]));
}
shrinker->Update();
maskshrinker->Update();
using CorrecterType = itk::N3MRIBiasFieldCorrectionImageFilter<ImageType, MaskImageType, ImageType>;
typename CorrecterType::Pointer correcter = CorrecterType::New();
correcter->SetInput(shrinker->GetOutput());
correcter->SetMaskImage(maskshrinker->GetOutput());
// 6 is [numberOfIterations]
if (argc > 6)
{
correcter->SetMaximumNumberOfIterations(std::stoi(argv[6]));
}
// 7 is [numberOfFittingLevels]
if (argc > 7)
{
correcter->SetNumberOfFittingLevels(std::stoi(argv[7]));
}
// 8 is [outputBiasField]
// 9 is [verbose]
bool verbose = false;
if (argc > 9)
{
verbose = static_cast<bool>(std::stoi(argv[9]));
}
using CommandType = CommandIterationUpdate<CorrecterType>;
typename CommandType::Pointer observer = CommandType::New();
if (verbose)
{
correcter->AddObserver(itk::IterationEvent(), observer);
}
try
{
correcter->Update();
}
catch (...)
{
std::cout << "Exception caught." << std::endl;
return EXIT_FAILURE;
}
// correcter->Print( std::cout, 3 );
/**
* Reconstruct the bias field at full image resolution. Divide
* the original input image by the bias field to get the final
* corrected image.
*/
using BSplinerType = itk::BSplineControlPointImageFilter<typename CorrecterType::BiasFieldControlPointLatticeType,
typename CorrecterType::ScalarImageType>;
typename BSplinerType::Pointer bspliner = BSplinerType::New();
bspliner->SetInput(correcter->GetLogBiasFieldControlPointLattice());
bspliner->SetSplineOrder(correcter->GetSplineOrder());
bspliner->SetSize(image->GetLargestPossibleRegion().GetSize());
bspliner->SetOrigin(image->GetOrigin());
bspliner->SetDirection(image->GetDirection());
bspliner->SetSpacing(image->GetSpacing());
bspliner->Update();
typename ImageType::Pointer logField = AllocImage<ImageType>(bspliner->GetOutput());
itk::ImageRegionIterator<typename CorrecterType::ScalarImageType> ItB(
bspliner->GetOutput(), bspliner->GetOutput()->GetLargestPossibleRegion());
itk::ImageRegionIterator<ImageType> ItF(logField, logField->GetLargestPossibleRegion());
for (ItB.GoToBegin(), ItF.GoToBegin(); !ItB.IsAtEnd(); ++ItB, ++ItF)
{
ItF.Set(ItB.Get()[0]);
}
using ExpFilterType = itk::ExpImageFilter<ImageType, ImageType>;
typename ExpFilterType::Pointer expFilter = ExpFilterType::New();
expFilter->SetInput(logField);
expFilter->Update();
using DividerType = itk::DivideImageFilter<ImageType, ImageType, ImageType>;
typename DividerType::Pointer divider = DividerType::New();
divider->SetInput1(image);
divider->SetInput2(expFilter->GetOutput());
divider->Update();
using WriterType = itk::ImageFileWriter<ImageType>;
typename WriterType::Pointer writer = WriterType::New();
if (argc < 4)
{
std::cout << "missing divider image filename" << std::endl;
throw;
}
ANTs::WriteImage<ImageType>(divider->GetOutput(), argv[3]);
if (argc > 8)
{
ANTs::WriteImage<ImageType>(expFilter->GetOutput(), argv[8]);
}
return EXIT_SUCCESS;
}
template <unsigned int ImageDimension>
int
N3(itk::ants::CommandLineParser * parser)
{
using RealType = float;
using ImageType = itk::Image<RealType, ImageDimension>;
typename ImageType::Pointer inputImage = nullptr;
using MaskImageType = itk::Image<RealType, ImageDimension>;
typename MaskImageType::Pointer maskImage = nullptr;
bool verbose = false;
typename itk::ants::CommandLineParser::OptionType::Pointer verboseOption = parser->GetOption("verbose");
if (verboseOption && verboseOption->GetNumberOfFunctions())
{
verbose = parser->Convert<bool>(verboseOption->GetFunction(0)->GetName());
}
if (verbose)
{
std::cout << std::endl << "Running N3 for " << ImageDimension << "-dimensional images." << std::endl << std::endl;
}
using CorrecterType = itk::N3BiasFieldCorrectionImageFilter<ImageType, MaskImageType, ImageType>;
typename CorrecterType::Pointer correcter = CorrecterType::New();
typename itk::ants::CommandLineParser::OptionType::Pointer inputImageOption = parser->GetOption("input-image");
if (inputImageOption && inputImageOption->GetNumberOfFunctions())
{
std::string inputFile = inputImageOption->GetFunction(0)->GetName();
ReadImage<ImageType>(inputImage, inputFile.c_str());
}
else
{
if (verbose)
{
std::cerr << "Input image not specified." << std::endl;
}
return EXIT_FAILURE;
}
/**
* handle the mask image
*/
bool isMaskImageSpecified = false;
typename itk::ants::CommandLineParser::OptionType::Pointer maskImageOption = parser->GetOption("mask-image");
if (maskImageOption && maskImageOption->GetNumberOfFunctions())
{
std::string inputMaskFile = maskImageOption->GetFunction(0)->GetName();
ReadImage<MaskImageType>(maskImage, inputMaskFile.c_str());
isMaskImageSpecified = true;
}
if (maskImage.IsNull())
{
if (verbose)
{
std::cout << "Mask not read. Using the entire image as the mask." << std::endl << std::endl;
}
maskImage = MaskImageType::New();
maskImage->CopyInformation(inputImage);
maskImage->SetRegions(inputImage->GetRequestedRegion());
maskImage->Allocate();
maskImage->FillBuffer(itk::NumericTraits<typename MaskImageType::PixelType>::OneValue());
}
/**
* check for negative values in the masked region
*/
using ThresholderType = itk::BinaryThresholdImageFilter<MaskImageType, MaskImageType>;
typename ThresholderType::Pointer thresholder = ThresholderType::New();
thresholder->SetInsideValue(itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue());
thresholder->SetOutsideValue(itk::NumericTraits<typename MaskImageType::PixelType>::OneValue());
thresholder->SetLowerThreshold(itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue());
thresholder->SetUpperThreshold(itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue());
thresholder->SetInput(maskImage);
using StatsType = itk::LabelStatisticsImageFilter<ImageType, MaskImageType>;
typename StatsType::Pointer statsOriginal = StatsType::New();
statsOriginal->SetInput(inputImage);
statsOriginal->SetLabelInput(thresholder->GetOutput());
statsOriginal->UseHistogramsOff();
statsOriginal->Update();
using StatsLabelType = typename StatsType::LabelPixelType;
StatsLabelType maskLabel = itk::NumericTraits<StatsLabelType>::OneValue();
RealType minOriginal = statsOriginal->GetMinimum(maskLabel);
RealType maxOriginal = statsOriginal->GetMaximum(maskLabel);
if (verbose)
{
std::cout << "Original intensity range: [" << minOriginal << ", " << maxOriginal << "]" << std::endl;
}
if (minOriginal <= 0)
{
if (verbose)
{
std::cout << std::endl;
std::cout << "***********************************************************" << std::endl;
std::cout << "Warning: Your input image contains nonpositive values" << std::endl;
std::cout << "which could cause failure or problematic results. A" << std::endl;
std::cout << "possible workaround would be to:" << std::endl;
std::cout << " 1. rescale your image to positive values e.g., [10,100]." << std::endl;
std::cout << " 2. run N3 on your rescaled image." << std::endl;
std::cout << " 3. (optional) rescale the N3 output to the original" << std::endl;
std::cout << " intensity range." << std::endl;
std::cout << "***********************************************************" << std::endl;
std::cout << std::endl;
}
}
/**
* handle the weight image
*/
typename ImageType::Pointer weightImage = nullptr;
typename itk::ants::CommandLineParser::OptionType::Pointer weightImageOption = parser->GetOption("weight-image");
if (weightImageOption && weightImageOption->GetNumberOfFunctions())
{
std::string inputFile = weightImageOption->GetFunction(0)->GetName();
ReadImage<ImageType>(weightImage, inputFile.c_str());
}
/**
* convergence options
*/
typename itk::ants::CommandLineParser::OptionType::Pointer convergenceOption = parser->GetOption("convergence");
if (convergenceOption && convergenceOption->GetNumberOfFunctions())
{
if (convergenceOption->GetFunction(0)->GetNumberOfParameters() > 0)
{
unsigned int maximumNumberOfIterations =
parser->Convert<unsigned int>(convergenceOption->GetFunction(0)->GetParameter(0));
correcter->SetMaximumNumberOfIterations(maximumNumberOfIterations);
correcter->SetConvergenceThreshold(0.0);
}
if (convergenceOption->GetFunction(0)->GetNumberOfParameters() > 1)
{
correcter->SetConvergenceThreshold(parser->Convert<float>(convergenceOption->GetFunction(0)->GetParameter(1)));
}
}
else // set default values
{
correcter->SetMaximumNumberOfIterations(50);
correcter->SetConvergenceThreshold(0.0);
}
/**
* B-spline options -- we place this here to take care of the case where
* the user wants to specify things in terms of the spline distance.
*/
typename ImageType::IndexType inputImageIndex = inputImage->GetLargestPossibleRegion().GetIndex();
typename ImageType::SizeType inputImageSize = inputImage->GetLargestPossibleRegion().GetSize();
typename ImageType::PointType newOrigin = inputImage->GetOrigin();
typename itk::ants::CommandLineParser::OptionType::Pointer bsplineOption = parser->GetOption("bspline-fitting");
if (bsplineOption && bsplineOption->GetNumberOfFunctions())
{
if (bsplineOption->GetFunction(0)->GetNumberOfParameters() > 2)
{
correcter->SetSplineOrder(parser->Convert<unsigned int>(bsplineOption->GetFunction(0)->GetParameter(2)));
}
if (bsplineOption->GetFunction(0)->GetNumberOfParameters() > 1)
{
correcter->SetNumberOfFittingLevels(
parser->Convert<unsigned int>(bsplineOption->GetFunction(0)->GetParameter(1)));
}
if (bsplineOption->GetFunction(0)->GetNumberOfParameters() > 0)
{
std::vector<float> array = parser->ConvertVector<float>(bsplineOption->GetFunction(0)->GetParameter(0));
typename CorrecterType::ArrayType numberOfControlPoints;
if (array.size() == 1)
{
// the user wants to specify things in terms of spline distance.
// 1. need to pad the images to get as close to possible to the
// requested domain size.
float splineDistance = array[0];
typename ImageType::SizeType originalImageSize = inputImage->GetLargestPossibleRegion().GetSize();
itk::Size<ImageDimension> lowerBound;
itk::Size<ImageDimension> upperBound;
for (unsigned int d = 0; d < ImageDimension; d++)
{
float domain = static_cast<float>(originalImageSize[d] - 1) * static_cast<float>(inputImage->GetSpacing()[d]);
auto numberOfSpans = static_cast<unsigned int>(std::ceil(domain / splineDistance));
auto extraPadding = static_cast<unsigned long>((numberOfSpans * splineDistance - domain) /
static_cast<float>(inputImage->GetSpacing()[d]) +
static_cast<float>(0.5));
lowerBound[d] = static_cast<unsigned long>(0.5 * extraPadding);
upperBound[d] = extraPadding - lowerBound[d];
newOrigin[d] -= (static_cast<double>(lowerBound[d]) * inputImage->GetSpacing()[d]);
numberOfControlPoints[d] = numberOfSpans + correcter->GetSplineOrder();
}
using PadderType = itk::ConstantPadImageFilter<ImageType, ImageType>;
typename PadderType::Pointer padder = PadderType::New();
padder->SetInput(inputImage);
padder->SetPadLowerBound(lowerBound);
padder->SetPadUpperBound(upperBound);
padder->SetConstant(itk::NumericTraits<typename ImageType::PixelType>::ZeroValue());
padder->Update();
inputImage = padder->GetOutput();
inputImage->DisconnectPipeline();
using MaskPadderType = itk::ConstantPadImageFilter<MaskImageType, MaskImageType>;
typename MaskPadderType::Pointer maskPadder = MaskPadderType::New();
maskPadder->SetInput(maskImage);
maskPadder->SetPadLowerBound(lowerBound);
maskPadder->SetPadUpperBound(upperBound);
maskPadder->SetConstant(0);
maskPadder->Update();
maskImage = maskPadder->GetOutput();
maskImage->DisconnectPipeline();
if (weightImage)
{
typename PadderType::Pointer weightPadder = PadderType::New();
weightPadder->SetInput(weightImage);
weightPadder->SetPadLowerBound(lowerBound);
weightPadder->SetPadUpperBound(upperBound);
weightPadder->SetConstant(0);
weightPadder->Update();
weightImage = weightPadder->GetOutput();
weightImage->DisconnectPipeline();
}
if (verbose)
{
std::cout << "Specified spline distance: " << splineDistance << "mm" << std::endl;
std::cout << " original image size: " << originalImageSize << std::endl;
std::cout << " padded image size: " << inputImage->GetLargestPossibleRegion().GetSize() << std::endl;
std::cout << " number of control points: " << numberOfControlPoints << std::endl;
std::cout << std::endl;
}
}
else if (array.size() == ImageDimension)
{
for (unsigned int d = 0; d < ImageDimension; d++)
{
numberOfControlPoints[d] = static_cast<unsigned int>(array[d]) + correcter->GetSplineOrder();
}
}
else
{
if (verbose)
{
std::cerr << "Incorrect mesh resolution" << std::endl;
}
return EXIT_FAILURE;
}
correcter->SetNumberOfControlPoints(numberOfControlPoints);
}
}
using ShrinkerType = itk::ShrinkImageFilter<ImageType, ImageType>;
typename ShrinkerType::Pointer shrinker = ShrinkerType::New();
shrinker->SetInput(inputImage);
shrinker->SetShrinkFactors(1);
using MaskShrinkerType = itk::ShrinkImageFilter<MaskImageType, MaskImageType>;
typename MaskShrinkerType::Pointer maskshrinker = MaskShrinkerType::New();
maskshrinker->SetInput(maskImage);
maskshrinker->SetShrinkFactors(1);
typename itk::ants::CommandLineParser::OptionType::Pointer shrinkFactorOption = parser->GetOption("shrink-factor");
int shrinkFactor = 4;
if (shrinkFactorOption && shrinkFactorOption->GetNumberOfFunctions())
{
shrinkFactor = parser->Convert<int>(shrinkFactorOption->GetFunction(0)->GetName());
}
shrinker->SetShrinkFactors(shrinkFactor);
maskshrinker->SetShrinkFactors(shrinkFactor);
if (ImageDimension == 4)
{
shrinker->SetShrinkFactor(3, 1);
maskshrinker->SetShrinkFactor(3, 1);
}
shrinker->Update();
maskshrinker->Update();
itk::TimeProbe timer;
timer.Start();
correcter->SetInput(shrinker->GetOutput());
correcter->SetMaskImage(maskshrinker->GetOutput());
using WeightShrinkerType = itk::ShrinkImageFilter<ImageType, ImageType>;
typename WeightShrinkerType::Pointer weightshrinker = WeightShrinkerType::New();
if (weightImage)
{
weightshrinker->SetInput(weightImage);
weightshrinker->SetShrinkFactors(shrinker->GetShrinkFactors());
weightshrinker->Update();
correcter->SetConfidenceImage(weightshrinker->GetOutput());
}
if (verbose)
{
using CommandType = CommandIterationUpdate<CorrecterType>;
typename CommandType::Pointer observer = CommandType::New();
correcter->AddObserver(itk::IterationEvent(), observer);
}
/**
* histogram sharpening options
*/
typename itk::ants::CommandLineParser::OptionType::Pointer histOption = parser->GetOption("histogram-sharpening");
if (histOption && histOption->GetNumberOfFunctions())
{
if (histOption->GetFunction(0)->GetNumberOfParameters() > 0)
{
correcter->SetBiasFieldFullWidthAtHalfMaximum(
parser->Convert<float>(histOption->GetFunction(0)->GetParameter(0)));
}
if (histOption->GetFunction(0)->GetNumberOfParameters() > 1)
{
correcter->SetWienerFilterNoise(parser->Convert<float>(histOption->GetFunction(0)->GetParameter(1)));
}
if (histOption->GetFunction(0)->GetNumberOfParameters() > 2)
{
correcter->SetNumberOfHistogramBins(parser->Convert<unsigned int>(histOption->GetFunction(0)->GetParameter(2)));
}
}
try
{
// correcter->DebugOn();
correcter->Update();
}
catch (const itk::ExceptionObject & e)
{
if (verbose)
{
std::cerr << "Exception caught: " << e << std::endl;
}
return EXIT_FAILURE;
}
if (verbose)
{
correcter->Print(std::cout, 3);
}
timer.Stop();
if (verbose)
{
std::cout << "Elapsed time: " << timer.GetMean() << std::endl;
}
/**
* output
*/
typename itk::ants::CommandLineParser::OptionType::Pointer outputOption = parser->GetOption("output");
if (outputOption && outputOption->GetNumberOfFunctions())
{
/**
* Reconstruct the bias field at full image resolution. Divide
* the original input image by the bias field to get the final
* corrected image.
*/
using BSplinerType = itk::BSplineControlPointImageFilter<typename CorrecterType::BiasFieldControlPointLatticeType,
typename CorrecterType::ScalarImageType>;
typename BSplinerType::Pointer bspliner = BSplinerType::New();
bspliner->SetInput(correcter->GetLogBiasFieldControlPointLattice());
bspliner->SetSplineOrder(correcter->GetSplineOrder());
bspliner->SetSize(inputImage->GetLargestPossibleRegion().GetSize());
bspliner->SetOrigin(newOrigin);
bspliner->SetDirection(inputImage->GetDirection());
bspliner->SetSpacing(inputImage->GetSpacing());
bspliner->Update();
typename ImageType::Pointer logField = AllocImage<ImageType>(inputImage);
itk::ImageRegionIterator<typename CorrecterType::ScalarImageType> ItB(
bspliner->GetOutput(), bspliner->GetOutput()->GetLargestPossibleRegion());
itk::ImageRegionIterator<ImageType> ItF(logField, logField->GetLargestPossibleRegion());
for (ItB.GoToBegin(), ItF.GoToBegin(); !ItB.IsAtEnd(); ++ItB, ++ItF)
{
ItF.Set(ItB.Get()[0]);
}
using ExpFilterType = itk::ExpImageFilter<ImageType, ImageType>;
typename ExpFilterType::Pointer expFilter = ExpFilterType::New();
expFilter->SetInput(logField);
expFilter->Update();
using DividerType = itk::DivideImageFilter<ImageType, ImageType, ImageType>;
typename DividerType::Pointer divider = DividerType::New();
divider->SetInput1(inputImage);
divider->SetInput2(expFilter->GetOutput());
typename ImageType::Pointer dividedImage = divider->GetOutput();
dividedImage->Update();
dividedImage->DisconnectPipeline();
if (maskImageOption && maskImageOption->GetNumberOfFunctions() > 0)
{
itk::ImageRegionIteratorWithIndex<ImageType> ItD(dividedImage, dividedImage->GetLargestPossibleRegion());
itk::ImageRegionIterator<ImageType> ItI(inputImage, inputImage->GetLargestPossibleRegion());
for (ItD.GoToBegin(), ItI.GoToBegin(); !ItD.IsAtEnd(); ++ItD, ++ItI)
{
if (itk::Math::FloatAlmostEqual(maskImage->GetPixel(ItD.GetIndex()),
itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue()))
{
ItD.Set(ItI.Get());
}
}
}
bool doRescale = true;
typename itk::ants::CommandLineParser::OptionType::Pointer rescaleOption = parser->GetOption("rescale-intensities");
if (!isMaskImageSpecified || (rescaleOption && rescaleOption->GetNumberOfFunctions() &&
!parser->Convert<bool>(rescaleOption->GetFunction()->GetName())))
{
doRescale = false;
}
if (doRescale)
{
typename ThresholderType::Pointer thresholder2 = ThresholderType::New();
thresholder2->SetInsideValue(itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue());
thresholder2->SetOutsideValue(itk::NumericTraits<typename MaskImageType::PixelType>::OneValue());
thresholder2->SetLowerThreshold(itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue());
thresholder2->SetUpperThreshold(itk::NumericTraits<typename MaskImageType::PixelType>::ZeroValue());
thresholder2->SetInput(maskImage);
typename StatsType::Pointer statsBiasCorrected = StatsType::New();
statsBiasCorrected->SetInput(dividedImage);
statsBiasCorrected->SetLabelInput(thresholder2->GetOutput());
statsBiasCorrected->UseHistogramsOff();
statsBiasCorrected->Update();
RealType minBiasCorrected = statsBiasCorrected->GetMinimum(maskLabel);
RealType maxBiasCorrected = statsBiasCorrected->GetMaximum(maskLabel);
RealType slope = (maxOriginal - minOriginal) / (maxBiasCorrected - minBiasCorrected);
itk::ImageRegionIteratorWithIndex<ImageType> ItD(dividedImage, dividedImage->GetLargestPossibleRegion());
for (ItD.GoToBegin(); !ItD.IsAtEnd(); ++ItD)
{
if (itk::Math::FloatAlmostEqual(maskImage->GetPixel(ItD.GetIndex()), static_cast<RealType>(maskLabel)))
{
RealType originalIntensity = ItD.Get();
RealType rescaledIntensity = maxOriginal - slope * (maxBiasCorrected - originalIntensity);
ItD.Set(rescaledIntensity);
}
}
}
typename ImageType::RegionType inputRegion;
inputRegion.SetIndex(inputImageIndex);
inputRegion.SetSize(inputImageSize);
using CropperType = itk::ExtractImageFilter<ImageType, ImageType>;
typename CropperType::Pointer cropper = CropperType::New();
cropper->SetInput(dividedImage);
cropper->SetExtractionRegion(inputRegion);
cropper->SetDirectionCollapseToSubmatrix();
cropper->Update();
typename CropperType::Pointer biasFieldCropper = CropperType::New();
biasFieldCropper->SetInput(expFilter->GetOutput());
biasFieldCropper->SetExtractionRegion(inputRegion);
biasFieldCropper->SetDirectionCollapseToSubmatrix();
biasFieldCropper->Update();
if (outputOption->GetFunction(0)->GetNumberOfParameters() == 0)
{
ANTs::WriteImage<ImageType>(cropper->GetOutput(), (outputOption->GetFunction(0)->GetName()).c_str());
}
else if (outputOption->GetFunction(0)->GetNumberOfParameters() > 0)
{
ANTs::WriteImage<ImageType>(cropper->GetOutput(), (outputOption->GetFunction(0)->GetParameter(0)).c_str());
if (outputOption->GetFunction(0)->GetNumberOfParameters() > 1)
{
ANTs::WriteImage<ImageType>(biasFieldCropper->GetOutput(),
(outputOption->GetFunction(0)->GetParameter(1)).c_str());
}
}
}
return EXIT_SUCCESS;
}
void
N3InitializeCommandLineOptions(itk::ants::CommandLineParser * parser)
{
using OptionType = itk::ants::CommandLineParser::OptionType;
{
std::string description = std::string("This option forces the image to be treated as a specified-") +
std::string("dimensional image. If not specified, N3 tries to ") +
std::string("infer the dimensionality from the input image.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("image-dimensionality");
option->SetShortName('d');
option->SetUsageOption(0, "2/3/4");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("A scalar image is expected as input for bias correction. ") +
std::string("Since N3 log transforms the intensities, negative values ") +
std::string("or values close to zero should be processed prior to ") +
std::string("correction.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("input-image");
option->SetShortName('i');
option->SetUsageOption(0, "inputImageFilename");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description =
std::string("If a mask image is specified, the final bias correction is ") +
std::string("only performed in the mask region. If a weight image is not ") +
std::string("specified, only intensity values inside the masked region are ") +
std::string("used during the execution of the algorithm. If a weight ") +
std::string("image is specified, only the non-zero weights are used in the ") +
std::string("execution of the algorithm although the mask region defines ") +
std::string("where bias correction is performed in the final output. ") +
std::string("Otherwise bias correction occurs over the entire image domain. ") +
std::string("See also the option description for the weight image. ") +
std::string("If a mask image is *not* specified then the entire image region ") +
std::string("will be used as the mask region. Note that this is different than ") +
std::string("the N3 implementation which uses the results of Otsu thresholding ") +
std::string("to define a mask. However, this leads to unknown anatomical regions being ") +
std::string("included and excluded during the bias correction.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("mask-image");
option->SetShortName('x');
option->SetUsageOption(0, "maskImageFilename");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("At each iteration, a new intensity mapping is calculated ") +
std::string("and applied but there is nothing which constrains the ") +
std::string("new intensity range to be within certain values. The ") +
std::string("result is that the range can \"drift\" from the original ") +
std::string("at each iteration. This option rescales to the [min,max] ") +
std::string("range of the original image intensities within the user-specified mask.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("rescale-intensities");
option->SetShortName('r');
option->SetUsageOption(0, "0/(1)");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("The weight image allows the user to perform a relative ") +
std::string("weighting of specific voxels during the B-spline fitting. ") +
std::string("For example, some studies have shown that N3 performed on ") +
std::string("white matter segmentations improves performance. If one ") +
std::string("has a spatial probability map of the white matter, one can ") +
std::string("use this map to weight the b-spline fitting towards those ") +
std::string("voxels which are more probabilistically classified as white ") +
std::string("matter. See also the option description for the mask image.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("weight-image");
option->SetUsageOption(0, "weightImageFilename");
option->SetShortName('w');
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Running N3 on large images can be time consuming. ") +
std::string("To lessen computation time, the input image can be resampled. ") +
std::string("The shrink factor, specified as a single integer, describes ") +
std::string("this resampling. Shrink factors <= 4 are commonly used.") +
std::string("Note that the shrink factor is only applied to the first two or ") +
std::string("three dimensions which we assume are spatial. ");
OptionType::Pointer option = OptionType::New();
option->SetLongName("shrink-factor");
option->SetShortName('s');
option->SetUsageOption(0, "1/2/3/(4)/...");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Convergence is determined by calculating the sigma of the coefficient of ") +
std::string("variation between subsequent iterations. When this value ") +
std::string("is less than the specified threshold ") +
std::string("from the previous iteration or the maximum number of ") +
std::string("iterations is exceeded the program terminates.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("convergence");
option->SetShortName('c');
option->SetUsageOption(0, "[<numberOfIterations=50>,<convergenceThreshold=0.0>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description =
std::string("These options describe the b-spline fitting parameters. ") + std::string("The b-spline mesh is ") +
std::string("specified either as the number of elements in each dimension, ") +
std::string("e.g. 2x2x3 for 3-D images, or it can be specified as a ") +
std::string("single scalar parameter which describes the isotropic sizing ") +
std::string("of the mesh elements. The latter option is typically preferred. ") +
std::string("The default setting ") +
std::string("is to employ a single mesh element over the entire domain, i.e., ") + std::string("-b [1x1x1,4,3].");
OptionType::Pointer option = OptionType::New();
option->SetLongName("bspline-fitting");
option->SetShortName('b');
option->SetUsageOption(0, "[splineDistance,<numberOfFittingLevels=4>,<splineOrder=3>]");
option->SetUsageOption(1, "[meshResolution,<numberOfFittingLevels=4>,<splineOrder=3>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("These options describe the histogram sharpening parameters, ") +
std::string("i.e. the deconvolution step parameters described in the ") +
std::string("original N3 algorithm. The default values have been shown ") +
std::string("to work fairly well.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("histogram-sharpening");
option->SetShortName('t');
option->SetUsageOption(0, "[<FWHM=0.15>,<wienerNoise=0.01>,<numberOfHistogramBins=200>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("The output consists of the bias corrected version of the ") +
std::string("input image. Optionally, one can also output the estimated ") +
std::string("bias field.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("output");
option->SetShortName('o');
option->SetUsageOption(0, "correctedImage");
option->SetUsageOption(1, "[correctedImage,<biasField>]");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Get Version Information.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("version");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Verbose output.");
OptionType::Pointer option = OptionType::New();
option->SetShortName('v');
option->SetLongName("verbose");
option->SetUsageOption(0, "(0)/1");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Print the help menu (short version).");
OptionType::Pointer option = OptionType::New();
option->SetShortName('h');
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Print the help menu.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("help");
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
N3BiasFieldCorrection(std::vector<std::string> args, std::ostream * /*out_stream = nullptr */)
{
if (args.size() >= 1 && std::string(args[0]) != std::string("2") && std::string(args[0]) != std::string("3") &&
std::string(args[0]) != std::string("4"))
{
// 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(), "N3BiasFieldCorrection");
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("This N3 is a variant of the popular N3 (nonparameteric nonuniform ") +
std::string("normalization) retrospective bias correction algorithm. Based ") +
std::string("on the assumption that the corruption of the low frequency bias ") +
std::string("field can be modeled as a convolution of the intensity histogram ") +
std::string("by a Gaussian, the basic algorithmic protocol is to iterate ") +
std::string("between deconvolving the intensity histogram by a Gaussian, ") +
std::string("remapping the intensities, and then spatially smoothing this ") +
std::string("result by a B-spline modeling of the bias field itself. ");
parser->SetCommandDescription(commandDescription);
N3InitializeCommandLineOptions(parser);
if (parser->Parse(argc, argv) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
if (argc == 1)
{
parser->PrintMenu(std::cerr, 5, false);
return EXIT_FAILURE;
}
else if (parser->GetOption("help")->GetFunction() &&
parser->Convert<bool>(parser->GetOption("help")->GetFunction()->GetName()))
{
parser->PrintMenu(std::cout, 5, false);
return EXIT_SUCCESS;
}
else if (parser->GetOption('h')->GetFunction() &&
parser->Convert<bool>(parser->GetOption('h')->GetFunction()->GetName()))
{
parser->PrintMenu(std::cout, 5, true);
return EXIT_SUCCESS;
}
// Show automatic version
itk::ants::CommandLineParser::OptionType::Pointer versionOption = parser->GetOption("version");
if (versionOption && versionOption->GetNumberOfFunctions())
{
std::string versionFunction = versionOption->GetFunction(0)->GetName();
ConvertToLowerCase(versionFunction);
if (versionFunction.compare("1") == 0 || versionFunction.compare("true") == 0)
{
// Print Version Information
std::cout << ANTs::Version::ExtendedVersionString() << std::endl;
return EXIT_SUCCESS;
}
}
// Get dimensionality
unsigned int dimension = 3;
itk::ants::CommandLineParser::OptionType::Pointer dimOption = parser->GetOption("image-dimensionality");
if (dimOption && dimOption->GetNumberOfFunctions())
{
dimension = parser->Convert<unsigned int>(dimOption->GetFunction(0)->GetName());
}
else
{
// Read in the first intensity image to get the image dimension.
std::string filename;
itk::ants::CommandLineParser::OptionType::Pointer imageOption = parser->GetOption("input-image");
if (imageOption && imageOption->GetNumberOfFunctions() > 0)
{
if (imageOption->GetFunction(0)->GetNumberOfParameters() > 0)
{
filename = imageOption->GetFunction(0)->GetParameter(0);
}
else
{
filename = imageOption->GetFunction(0)->GetName();
}
}
else
{
std::cerr << "No input images were specified. Specify an input image"
<< " with the -i option" << std::endl;
return EXIT_FAILURE;
}
itk::ImageIOBase::Pointer imageIO =
itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::IOFileModeEnum::ReadMode);
dimension = imageIO->GetNumberOfDimensions();
}
int returnValue = EXIT_FAILURE;
switch (dimension)
{
case 2:
{
returnValue = N3<2>(parser);
}
break;
case 3:
{
returnValue = N3<3>(parser);
}
break;
case 4:
{
returnValue = N3<4>(parser);
}
break;
default:
std::cout << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return returnValue;
}
else
{
// 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(), "N3BiasFieldCorrection");
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 );
if (argc < 4)
{
std::cout << "Usage: " << argv[0] << " imageDimension inputImage "
<< "outputImage [shrinkFactor] [maskImage] [numberOfIterations] "
<< "[numberOfFittingLevels] [outputBiasField] [verbose]" << std::endl;
if (argc >= 2 && (std::string(argv[1]) == std::string("--help") || std::string(argv[1]) == std::string("-h")))
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
switch (std::stoi(argv[1]))
{
case 2:
{
return N3BiasFieldCorrection<2>(argc, argv);
}
break;
case 3:
{
return N3BiasFieldCorrection<3>(argc, argv);
}
break;
case 4:
{
return N3BiasFieldCorrection<4>(argc, argv);
}
break;
default:
std::cout << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
}
} // namespace ants
|