Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
CLResampleImageToReference.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 #ifndef mitkCLResampleImageToReference_cpp
13 #define mitkCLResampleImageToReference_cpp
14 
15 #include "mitkCommandLineParser.h"
16 #include <mitkImageAccessByItk.h>
17 #include <mitkIOUtil.h>
18 #include <mitkImage.h>
19 #include <mitkImageCast.h>
20 #include <mitkITKImageImport.h>
21 #include <mitkImageTimeSelector.h>
22 
23 // ITK
24 #include "itkLinearInterpolateImageFunction.h"
25 #include "itkWindowedSincInterpolateImageFunction.h"
26 #include "itkNearestNeighborInterpolateImageFunction.h"
27 #include "itkIdentityTransform.h"
28 #include "itkResampleImageFilter.h"
29 
30 
31 template<typename TPixel, unsigned int VImageDimension>
32 void
33 ResampleImageToReferenceFunction(itk::Image<TPixel, VImageDimension>* itkReference, mitk::Image::Pointer moving, std::string ergPath)
34 {
35  typedef itk::Image<TPixel, VImageDimension> InputImageType;
36 
37  // Identify Transform
38  typedef itk::IdentityTransform<double, VImageDimension> T_Transform;
39  typename T_Transform::Pointer _pTransform = T_Transform::New();
40  _pTransform->SetIdentity();
41 
42  typedef itk::WindowedSincInterpolateImageFunction< InputImageType, VImageDimension> WindowedSincInterpolatorType;
43  typename WindowedSincInterpolatorType::Pointer sinc_interpolator = WindowedSincInterpolatorType::New();
44 
45  typedef itk::LinearInterpolateImageFunction< InputImageType> LinearInterpolateImageFunctionType;
46  typename LinearInterpolateImageFunctionType::Pointer lin_interpolator = LinearInterpolateImageFunctionType::New();
47 
48  typedef itk::NearestNeighborInterpolateImageFunction< InputImageType> NearestNeighborInterpolateImageFunctionType;
49  typename NearestNeighborInterpolateImageFunctionType::Pointer nn_interpolator = NearestNeighborInterpolateImageFunctionType::New();
50 
51  typename InputImageType::Pointer itkMoving = InputImageType::New();
52  mitk::CastToItkImage(moving,itkMoving);
53  typedef itk::ResampleImageFilter<InputImageType, InputImageType> ResampleFilterType;
54 
55  typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
56  resampler->SetInput(itkMoving);
57  resampler->SetReferenceImage( itkReference );
58  resampler->UseReferenceImageOn();
59  resampler->SetTransform(_pTransform);
60  //if ( sincInterpol)
61  // resampler->SetInterpolator(sinc_interpolator);
62  //else
63  resampler->SetInterpolator(lin_interpolator);
64 
65  resampler->Update();
66 
67  // Convert back to mitk
69  result->InitializeByItk(resampler->GetOutput());
70  GrabItkImageMemory(resampler->GetOutput(), result);
71  MITK_INFO << "writing result to: " << ergPath;
72  mitk::IOUtil::Save(result, ergPath);
73  //return result;
74 }
75 
76 int main(int argc, char* argv[])
77 {
78  mitkCommandLineParser parser;
79  parser.setArgumentPrefix("--", "-");
80  // required params
81  parser.addArgument("fix", "f", mitkCommandLineParser::Image, "Input Image", "Path to the input VTK polydata", us::Any(), false, false, false, mitkCommandLineParser::Input);
82  parser.addArgument("moving", "m", mitkCommandLineParser::File, "Output text file", "Target file. The output statistic is appended to this file.", us::Any(), false, false, false, mitkCommandLineParser::Output);
83  parser.addArgument("output", "o", mitkCommandLineParser::File, "Extension", "File extension. Default is .nii.gz", us::Any(), false, false, false, mitkCommandLineParser::Output);
84 
85  // Miniapp Infos
86  parser.setCategory("Classification Tools");
87  parser.setTitle("Resample Image To Reference");
88  parser.setDescription("Resamples an image (moving) to an given image (fix) without additional registration.");
89  parser.setContributor("German Cancer Research Center (DKFZ)");
90 
91  std::map<std::string, us::Any> parsedArgs = parser.parseArguments(argc, argv);
92 
93  if (parsedArgs.size() == 0)
94  {
95  return EXIT_FAILURE;
96  }
97  if (parsedArgs.count("help") || parsedArgs.count("h"))
98  {
99  return EXIT_SUCCESS;
100  }
101 
102  mitk::Image::Pointer fix = mitk::IOUtil::Load<mitk::Image>(parsedArgs["fix"].ToString());
103  mitk::Image::Pointer moving = mitk::IOUtil::Load<mitk::Image>(parsedArgs["moving"].ToString());
105 
106  AccessByItk_2(fix, ResampleImageToReferenceFunction, moving, parsedArgs["output"].ToString());
107 
108 }
109 
110 
111 
112 #endif
#define MITK_INFO
Definition: mitkLogMacros.h:18
void setContributor(std::string contributor)
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
Image::Pointer GrabItkImageMemory(itk::SmartPointer< ItkOutputImageType > &itkimage, mitk::Image *mitkImage=nullptr, const BaseGeometry *geometry=nullptr, bool update=true)
Grabs the memory of an itk::Image (with a specific type) and puts it into an mitk::Image.The memory is managed by the mitk::Image after calling this function. The itk::Image remains valid until the mitk::Image decides to free the memory.
int main(int argc, char *argv[])
Definition: usAny.h:163
void setCategory(std::string category)
static Pointer New()
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
void ResampleImageToReferenceFunction(itk::Image< TPixel, VImageDimension > *itkReference, mitk::Image::Pointer moving, std::string ergPath)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
Definition: mitkIOUtil.cpp:774
#define AccessByItk_2(mitkImage, itkImageTypeFunction, arg1, arg2)
void setTitle(std::string title)
void setDescription(std::string description)