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
|
#include "antsUtilities.h"
#include "antsAllocImage.h"
#include "itkantsRegistrationHelper.h"
#include "ReadWriteData.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkResampleImageFilter.h"
#include "itkVectorIndexSelectionCastImageFilter.h"
#include "itkAffineTransform.h"
#include "itkCompositeTransform.h"
#include "itkDisplacementFieldTransform.h"
#include "itkIdentityTransform.h"
#include "itkMatrixOffsetTransformBase.h"
#include "itkTransformFactory.h"
#include "itkTransformFileWriter.h"
#include "itkTransformToDisplacementFieldFilter.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkGaussianInterpolateImageFunction.h"
#include "itkInterpolateImageFunction.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkWindowedSincInterpolateImageFunction.h"
#include "itkLabelImageGaussianInterpolateImageFunction.h"
namespace ants
{
template <unsigned int Dimension>
int
antsAlignOriginImplementation(itk::ants::CommandLineParser::Pointer & parser, unsigned int inputImageType)
{
if (inputImageType != 0)
{
std::cerr << "inputImageType is not used, therefore only mode 0 is supported at the momemnt." << std::endl;
return EXIT_FAILURE;
}
using PixelType = double;
using ImageType = itk::Image<PixelType, Dimension>;
typename ImageType::Pointer inputImage;
typename ImageType::Pointer outputImage;
/**
* Input object option - for now, we're limiting this to images.
*/
typename itk::ants::CommandLineParser::OptionType::Pointer inputOption = parser->GetOption("input");
typename itk::ants::CommandLineParser::OptionType::Pointer outputOption = parser->GetOption("output");
if (inputOption && inputOption->GetNumberOfFunctions() > 0)
{
if (inputOption->GetFunction()->GetNumberOfParameters() > 1 &&
parser->Convert<unsigned int>(outputOption->GetFunction(0)->GetParameter(1)) == 0)
{
std::cerr << "An input image is required." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Input image: " << inputOption->GetFunction()->GetName() << std::endl;
using ReaderType = itk::ImageFileReader<ImageType>;
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName((inputOption->GetFunction()->GetName()).c_str());
reader->Update();
inputImage = reader->GetOutput();
}
std::string outputTransform;
std::string outputWarpedImageName;
if (outputOption && outputOption->GetNumberOfFunctions() > 0)
{
outputTransform = outputOption->GetFunction(0)->GetName();
if (outputOption->GetFunction()->GetNumberOfParameters() > 0)
{
outputTransform = outputOption->GetFunction(0)->GetParameter(0);
}
if (outputOption->GetFunction(0)->GetNumberOfParameters() > 1)
{
outputWarpedImageName = outputOption->GetFunction(0)->GetParameter(1);
}
std::cout << "Output transform: " << outputTransform << std::endl;
std::cout << "Output image: " << outputWarpedImageName << std::endl;
}
/**
* Reference image option
*/
// read in the image as char since we only need the header information.
using ReferenceImageType = itk::Image<char, Dimension>;
typename ReferenceImageType::Pointer referenceImage;
typename itk::ants::CommandLineParser::OptionType::Pointer referenceOption = parser->GetOption("reference-image");
if (referenceOption && referenceOption->GetNumberOfFunctions() > 0)
{
std::cout << "Reference image: " << referenceOption->GetFunction()->GetName() << std::endl;
// read in the image as char since we only need the header information.
using ReferenceReaderType = itk::ImageFileReader<ReferenceImageType>;
typename ReferenceReaderType::Pointer referenceReader = ReferenceReaderType::New();
referenceReader->SetFileName((referenceOption->GetFunction()->GetName()).c_str());
referenceImage = referenceReader->GetOutput();
referenceImage->Update();
referenceImage->DisconnectPipeline();
}
else
{
std::cerr << "Error: No reference image specified." << std::endl;
return EXIT_FAILURE;
}
typename ImageType::PointType::VectorType translation = inputImage->GetOrigin() - referenceImage->GetOrigin();
translation = referenceImage->GetDirection() * inputImage->GetDirection() * translation;
std::cout << "offset = " << translation << std::endl;
using TransformType = itk::MatrixOffsetTransformBase<double, Dimension, Dimension>;
typename TransformType::Pointer transform = TransformType::New();
transform->SetIdentity();
transform->SetTranslation(translation);
typename itk::TransformFileWriter::Pointer transform_writer = itk::TransformFileWriter::New();
transform_writer->SetFileName(outputTransform);
transform_writer->SetInput(transform);
#if ITK_VERSION_MAJOR >= 5
transform_writer->SetUseCompression(true);
#endif
transform_writer->Update();
return EXIT_SUCCESS;
}
static void
InitializeCommandLineOptions(itk::ants::CommandLineParser * parser)
{
{
std::string description = std::string("This option forces the image to be treated as a specified-") +
std::string("dimensional image. If not specified, antsWarp tries to ") +
std::string("infer the dimensionality from the input image.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("dimensionality");
option->SetShortName('d');
option->SetUsageOption(0, "2/3");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("Currently, the only input objects supported are image ") +
std::string("objects. However, the current framework allows for ") +
std::string("warping of other objects such as meshes and point sets. ");
OptionType::Pointer option = OptionType::New();
option->SetLongName("input");
option->SetShortName('i');
option->SetUsageOption(0, "inputFileName");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("For warping input images, the reference image defines the ") +
std::string("spacing, origin, size, and direction of the output warped ") +
std::string("image. ");
OptionType::Pointer option = OptionType::New();
option->SetLongName("reference-image");
option->SetShortName('r');
option->SetUsageOption(0, "imageFileName");
option->SetDescription(description);
parser->AddOption(option);
}
{
std::string description = std::string("One can either output the warped image or, if the boolean ") +
std::string("is set, one can print out the displacement field based on the") +
std::string("composite transform and the reference image.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("output");
option->SetShortName('o');
option->SetUsageOption(0, "warpedOutputFileName");
option->SetUsageOption(1, "[transform,alignedImage]");
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);
option->AddFunction(std::string("0"));
parser->AddOption(option);
}
{
std::string description = std::string("Print the help menu.");
OptionType::Pointer option = OptionType::New();
option->SetLongName("help");
option->SetDescription(description);
option->AddFunction(std::string("0"));
parser->AddOption(option);
}
}
// entry point for the library; parameter 'args' is equivalent to 'argv' in (argc,argv) of commandline parameters to
// 'main()'
int
antsAlignOrigin(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(), "antsAlignOrigin");
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("antsAlignOrigin, applied to an input image, transforms it ") +
std::string("according to a reference image and a transform ") +
std::string("(or a set of transforms).");
parser->SetCommandDescription(commandDescription);
InitializeCommandLineOptions(parser);
if (parser->Parse(argc, argv) == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
if (argc < 2 ||
(parser->GetOption("help") && (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->GetOption('h') && (parser->Convert<bool>(parser->GetOption('h')->GetFunction()->GetName())))
{
parser->PrintMenu(std::cout, 5, true);
return EXIT_SUCCESS;
}
// Read in the first intensity image to get the image dimension.
std::string filename;
itk::ants::CommandLineParser::OptionType::Pointer inputOption = parser->GetOption("reference-image");
if (inputOption && inputOption->GetNumberOfFunctions() > 0)
{
if (inputOption->GetFunction(0)->GetNumberOfParameters() > 0)
{
filename = inputOption->GetFunction(0)->GetParameter(0);
}
else
{
filename = inputOption->GetFunction(0)->GetName();
}
}
else
{
std::cerr << "No reference image was specified." << std::endl;
return EXIT_FAILURE;
}
itk::ants::CommandLineParser::OptionType::Pointer inputImageTypeOption = parser->GetOption("input-image-type");
itk::ImageIOBase::Pointer imageIO =
itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::IOFileModeEnum::ReadMode);
unsigned int dimension = imageIO->GetNumberOfDimensions();
itk::ants::CommandLineParser::OptionType::Pointer dimOption = parser->GetOption("dimensionality");
if (dimOption && dimOption->GetNumberOfFunctions() > 0)
{
dimension = parser->Convert<unsigned int>(dimOption->GetFunction()->GetName());
}
switch (dimension)
{
case 2:
{
if (inputImageTypeOption)
{
std::string inputImageType = inputImageTypeOption->GetFunction()->GetName();
if (!std::strcmp(inputImageType.c_str(), "scalar") || !std::strcmp(inputImageType.c_str(), "0"))
{
return antsAlignOriginImplementation<2>(parser, 0);
}
else if (!std::strcmp(inputImageType.c_str(), "vector") || !std::strcmp(inputImageType.c_str(), "1"))
{
return antsAlignOriginImplementation<2>(parser, 1);
}
else if (!std::strcmp(inputImageType.c_str(), "tensor") || !std::strcmp(inputImageType.c_str(), "2"))
{
std::cerr << "antsApplyTransforms is not implemented for 2-D tensor images." << std::endl;
}
else
{
std::cerr << "Unrecognized input image type (cf --input-image-type option)." << std::endl;
return EXIT_FAILURE;
}
}
else
{
return antsAlignOriginImplementation<2>(parser, 0);
}
}
break;
case 3:
{
if (inputImageTypeOption)
{
std::string inputImageType = inputImageTypeOption->GetFunction()->GetName();
if (!std::strcmp(inputImageType.c_str(), "scalar") || !std::strcmp(inputImageType.c_str(), "0"))
{
return antsAlignOriginImplementation<3>(parser, 0);
}
else if (!std::strcmp(inputImageType.c_str(), "vector") || !std::strcmp(inputImageType.c_str(), "1"))
{
return antsAlignOriginImplementation<3>(parser, 1);
}
else if (!std::strcmp(inputImageType.c_str(), "tensor") || !std::strcmp(inputImageType.c_str(), "2"))
{
return antsAlignOriginImplementation<3>(parser, 2);
}
else
{
std::cerr << "Unrecognized input image type (cf --input-image-type option)." << std::endl;
return EXIT_FAILURE;
}
}
else
{
return antsAlignOriginImplementation<3>(parser, 0);
}
}
break;
case 4:
{
if (inputImageTypeOption)
{
std::string inputImageType = inputImageTypeOption->GetFunction()->GetName();
if (!std::strcmp(inputImageType.c_str(), "scalar") || !std::strcmp(inputImageType.c_str(), "0"))
{
return antsAlignOriginImplementation<4>(parser, 0);
}
else if (!std::strcmp(inputImageType.c_str(), "vector") || !std::strcmp(inputImageType.c_str(), "1"))
{
return antsAlignOriginImplementation<4>(parser, 1);
}
else if (!std::strcmp(inputImageType.c_str(), "tensor") || !std::strcmp(inputImageType.c_str(), "2"))
{
std::cerr << "antsApplyTransforms is not implemented for 4-D tensor images." << std::endl;
}
else
{
std::cerr << "Unrecognized input image type (cf --input-image-type option)." << std::endl;
return EXIT_FAILURE;
}
}
else
{
return antsAlignOriginImplementation<3>(parser, 0);
}
}
break;
default:
std::cerr << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace ants
|