30 #include <itksys/SystemTools.hxx>
31 #include <itkDirectory.h>
32 #include "itkLinearInterpolateImageFunction.h"
33 #include "itkWindowedSincInterpolateImageFunction.h"
34 #include "itkNearestNeighborInterpolateImageFunction.h"
35 #include "itkIdentityTransform.h"
36 #include "itkResampleImageFilter.h"
50 typedef itk::IdentityTransform<double, 3> T_Transform;
52 _pTransform->SetIdentity();
54 typedef itk::WindowedSincInterpolateImageFunction< InputImageType, 3> WindowedSincInterpolatorType;
57 typedef itk::LinearInterpolateImageFunction< InputImageType> LinearInterpolateImageFunctionType;
60 typedef itk::NearestNeighborInterpolateImageFunction< BinaryImageType> NearestNeighborInterpolateImageFunctionType;
70 typedef itk::ResampleImageFilter<InputImageType, InputImageType> ResampleFilterType;
74 resampler->SetInput(itkMoving);
75 resampler->SetReferenceImage( itkReference );
76 resampler->UseReferenceImageOn();
77 resampler->SetTransform(_pTransform);
79 resampler->SetInterpolator(sinc_interpolator);
81 resampler->SetInterpolator(lin_interpolator);
87 result->InitializeByItk(resampler->GetOutput());
99 typedef itk::ResampleImageFilter<BinaryImageType, BinaryImageType> ResampleFilterType;
103 resampler->SetInput(itkMoving);
104 resampler->SetReferenceImage( itkReference );
105 resampler->UseReferenceImageOn();
106 resampler->SetTransform(_pTransform);
107 resampler->SetInterpolator(nn_interpolator);
112 result->InitializeByItk(resampler->GetOutput());
119 static std::vector<std::string> &
split(
const std::string &s,
char delim, std::vector<std::string> &elems)
121 std::stringstream ss(s);
123 while (std::getline(ss, item, delim))
125 elems.push_back(item);
130 static std::vector<std::string>
split(
const std::string &s,
char delim)
132 std::vector < std::string > elems;
133 return split(s, delim, elems);
153 typedef itk::IdentityTransform<double, 3> T_Transform;
156 typedef itk::ResampleImageFilter<InputImageType, InputImageType> T_ResampleFilter;
161 _pTransform->SetIdentity();
167 _pResizeFilter->SetInput(itkImage);
169 _pResizeFilter->SetTransform(_pTransform);
172 _pResizeFilter->SetOutputOrigin(itkImage->GetOrigin());
177 InputImageType::SpacingType inputSpacing = itkImage->GetSpacing();
178 InputImageType::SpacingType outputSpacing;
179 const InputImageType::RegionType& inputSize = itkImage->GetLargestPossibleRegion();
181 InputImageType::SizeType outputSize;
182 typedef InputImageType::SizeType::SizeValueType SizeValueType;
185 outputSpacing[0] = spacing[0];
186 outputSpacing[1] = spacing[1];
187 outputSpacing[2] = spacing[2];
189 outputSize[0] =
static_cast<SizeValueType
>(inputSize.GetSize()[0] * inputSpacing[0] / outputSpacing[0] + .5);
190 outputSize[1] =
static_cast<SizeValueType
>(inputSize.GetSize()[1] * inputSpacing[1] / outputSpacing[1] + .5);
191 outputSize[2] =
static_cast<SizeValueType
>(inputSize.GetSize()[2] * inputSpacing[2] / outputSpacing[2] + .5);
193 _pResizeFilter->SetOutputSpacing(outputSpacing);
194 _pResizeFilter->SetSize(outputSize);
196 typedef itk::LinearInterpolateImageFunction< InputImageType > LinearInterpolatorType;
200 typedef itk::WindowedSincInterpolateImageFunction< InputImageType, 4> WindowedSincInterpolatorType;
204 _pResizeFilter->SetInterpolator(lin_interpolator);
206 _pResizeFilter->SetInterpolator(sinc_interpolator);
208 _pResizeFilter->Update();
211 image->InitializeByItk(_pResizeFilter->GetOutput());
228 typedef itk::IdentityTransform<double, 3> T_Transform;
231 typedef itk::ResampleImageFilter<BinaryImageType, BinaryImageType> T_ResampleFilter;
236 _pTransform->SetIdentity();
242 _pResizeFilter->SetInput(itkImage);
244 _pResizeFilter->SetTransform(_pTransform);
247 _pResizeFilter->SetOutputOrigin(itkImage->GetOrigin());
252 BinaryImageType::SpacingType inputSpacing = itkImage->GetSpacing();
253 BinaryImageType::SpacingType outputSpacing;
254 const BinaryImageType::RegionType& inputSize = itkImage->GetLargestPossibleRegion();
256 BinaryImageType::SizeType outputSize;
257 typedef BinaryImageType::SizeType::SizeValueType SizeValueType;
260 outputSpacing[0] = spacing[0];
261 outputSpacing[1] = spacing[1];
262 outputSpacing[2] = spacing[2];
264 outputSize[0] =
static_cast<SizeValueType
>(inputSize.GetSize()[0] * inputSpacing[0] / outputSpacing[0] + .5);
265 outputSize[1] =
static_cast<SizeValueType
>(inputSize.GetSize()[1] * inputSpacing[1] / outputSpacing[1] + .5);
266 outputSize[2] =
static_cast<SizeValueType
>(inputSize.GetSize()[2] * inputSpacing[2] / outputSpacing[2] + .5);
268 _pResizeFilter->SetOutputSpacing(outputSpacing);
269 _pResizeFilter->SetSize(outputSize);
271 typedef itk::NearestNeighborInterpolateImageFunction< BinaryImageType> NearestNeighborInterpolateImageType;
273 _pResizeFilter->SetInterpolator(nn_interpolator);
275 _pResizeFilter->Update();
278 image->InitializeByItk(_pResizeFilter->GetOutput());
287 std::cout <<
"----Save to " << fileName;
295 itk::Vector<double, 3> spacingVector;
296 spacingVector[0] = spacing[0];
297 spacingVector[1] = spacing[1];
298 spacingVector[2] = spacing[2];
306 resampler->SetInput( itkVectorImagePointer );
307 resampler->SetInterpolation(ResampleFilterType::Interpolate_Linear);
308 resampler->SetNewSpacing(spacingVector);
320 int main(
int argc,
char* argv[] )
328 parser.
setDescription(
"Resample an image to eigther a specific spacing or to a reference image.");
334 parser.
addArgument(
"spacing",
"s",
mitkCommandLineParser::String,
"Spacing:",
"Resample provide x,y,z spacing in mm (e.g. -r 1,1,3), is not applied to tensor data",
us::Any());
340 map<string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
343 bool useSpacing =
false;
344 bool useLinearInterpol =
true;
347 if (parsedArgs.size() == 0)
352 if (parsedArgs.count(
"sinc-int"))
353 useLinearInterpol =
false;
355 if (parsedArgs.count(
"nearest-neigh"))
359 if ( parsedArgs.count(
"help") || parsedArgs.count(
"h"))
366 std::string outputFile =
us::any_cast<
string>(parsedArgs[
"output"]);
367 std::string inputFile =
us::any_cast<
string>(parsedArgs[
"input"]);
369 std::vector<std::string> spacings;
371 if (parsedArgs.count(
"spacing"))
374 std::string arg =
us::any_cast<
string>(parsedArgs[
"spacing"]);
377 spacings =
split(arg ,
',');
378 spacing[0] = atoi(spacings.at(0).c_str());
379 spacing[1] = atoi(spacings.at(1).c_str());
380 spacing[2] = atoi(spacings.at(2).c_str());
385 std::string refImageFile =
"";
386 if (parsedArgs.count(
"reference"))
388 refImageFile =
us::any_cast<
string>(parsedArgs[
"reference"]);
391 if (refImageFile ==
"" && useSpacing ==
false)
393 MITK_ERROR <<
"No information how to resample is supplied. Use eigther --spacing or --reference !";
411 MITK_WARN <<
"Not supported yet, to resample a DWI please set a new spacing.";
bool IsDiffusionWeightedImage() const
mitk::Image::Pointer ResampleDWIbySpacing(mitk::Image::Pointer input, float *spacing, bool useLinInt=true)
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
static const std::string REFERENCEBVALUEPROPERTYNAME
int main(int argc, char *argv[])
itk::SmartPointer< Self > Pointer
void setContributor(std::string contributor)
Helper class for mitk::Images containing diffusion weighted data.
ValueType * any_cast(Any *operand)
static mitk::Image::Pointer TransformToReference(mitk::Image *reference, mitk::Image *moving, bool sincInterpol=false, bool nn=false)
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.
void InitializeImage()
Make certain the owned image is up to date with all necessary properties.
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
itk::Image< double, 3 > InputImageType
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)
Image class for storing images.
itk::Image< unsigned char, 3 > BinaryImageType
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
Resample DWI channel by channel.
float GetReferenceBValue() const
static mitk::Image::Pointer ResampleBySpacing(mitk::Image *input, float *spacing, bool useLinInt=true, bool useNN=false)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
std::string helpText() const
static void SaveImage(std::string fileName, mitk::Image *image, std::string fileType)
Save images according to file type.
GradientDirectionsContainerType::Pointer GetGradientContainer() const
static const std::string GRADIENTCONTAINERPROPERTYNAME
void setTitle(std::string title)
void setDescription(std::string description)
static std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.