27 #include <itkImageFileWriter.h>
29 #include <itksys/SystemTools.hxx>
30 #include <itkDirectory.h>
31 #include "itkLinearInterpolateImageFunction.h"
32 #include "itkWindowedSincInterpolateImageFunction.h"
33 #include "itkIdentityTransform.h"
34 #include "itkResampleImageFilter.h"
35 #include "itkNrrdImageIO.h"
44 if (fileType ==
".dwi")
47 selector->SetInput(image);
48 selector->SetTimeNr(0);
49 selector->UpdateLargestPossibleRegion();
55 static std::vector<std::string> &
split(
const std::string &s,
char delim, std::vector<std::string> &elems)
57 std::stringstream ss(s);
59 while (std::getline(ss, item, delim))
61 elems.push_back(item);
66 static std::vector<std::string>
split(
const std::string &s,
char delim)
68 std::vector < std::string > elems;
69 return split(s, delim, elems);
78 if( dir->Load(folder.c_str() ) )
80 int n = dir->GetNumberOfFiles();
83 std::string
filename = dir->GetFile( r );
84 if (filename ==
"." || filename ==
"..")
87 if (!itksys::SystemTools::FileExists( filename.c_str()))
89 if (filename.length() <= postfix.length() )
91 if (filename.substr(filename.length() -postfix.length() ) == postfix)
92 fileList.push_back(filename);
98 static std::string
GetSavePath(std::string outputFolder, std::string fileName)
100 std::string fileType = itksys::SystemTools::GetFilenameExtension(fileName);
101 std::string fileStem = itksys::SystemTools::GetFilenameWithoutExtension(fileName);
103 std::string savePathAndFileName = outputFolder +fileStem + fileType;
105 return savePathAndFileName;
123 typedef itk::IdentityTransform<double, 3> T_Transform;
126 typedef itk::ResampleImageFilter<InputImageType, InputImageType> T_ResampleFilter;
131 _pTransform->SetIdentity();
135 _pResizeFilter->SetTransform(_pTransform);
138 _pResizeFilter->SetOutputOrigin(itkImage->GetOrigin());
143 InputImageType::SpacingType inputSpacing = itkImage->GetSpacing();
144 InputImageType::SpacingType outputSpacing;
145 const InputImageType::RegionType& inputSize = itkImage->GetLargestPossibleRegion();
147 InputImageType::SizeType outputSize;
148 typedef InputImageType::SizeType::SizeValueType SizeValueType;
151 outputSpacing[0] = spacing[0];
152 outputSpacing[1] = spacing[1];
153 outputSpacing[2] = spacing[2];
155 outputSize[0] =
static_cast<SizeValueType
>(inputSize.GetSize()[0] * inputSpacing[0] / outputSpacing[0] + .5);
156 outputSize[1] =
static_cast<SizeValueType
>(inputSize.GetSize()[1] * inputSpacing[1] / outputSpacing[1] + .5);
157 outputSize[2] =
static_cast<SizeValueType
>(inputSize.GetSize()[2] * inputSpacing[2] / outputSpacing[2] + .5);
159 _pResizeFilter->SetOutputSpacing(outputSpacing);
160 _pResizeFilter->SetSize(outputSize);
163 typedef itk::LinearInterpolateImageFunction< InputImageType > LinearInterpolatorType;
166 typedef itk::Function::WelchWindowFunction<4> WelchWindowFunction;
167 typedef itk::WindowedSincInterpolateImageFunction< InputImageType, 4,WelchWindowFunction> WindowedSincInterpolatorType;
171 _pResizeFilter->SetInterpolator(lin_interpolator);
173 _pResizeFilter->SetInterpolator(sinc_interpolator);
176 _pResizeFilter->SetInput(itkImage);
177 _pResizeFilter->Update();
180 image->InitializeByItk(_pResizeFilter->GetOutput());
192 for (
unsigned int i=0; i < derivedPatterns.size(); i++)
194 std::string derResourceSuffix = derivedPatterns.at(i);
195 std::string derivedResourceFilename = baseFN.substr(0,baseFN.length() -baseSuffix.length()) + derResourceSuffix;
196 MITK_INFO <<
" Looking for file: " << derivedResourceFilename;
198 if (!itksys::SystemTools::FileExists(derivedResourceFilename.c_str()))
200 MITK_INFO <<
"CreateDerivedFileList: File does not exit. Skipping entry.";
203 files.push_back(derivedResourceFilename);
219 for (
unsigned int j=0; j < fileList.size(); j++)
221 std::string derivedResourceFilename = fileList.at(j);
222 std::string fileType = itksys::SystemTools::GetFilenameExtension(derivedResourceFilename);
223 std::string fileStem = itksys::SystemTools::GetFilenameWithoutExtension(derivedResourceFilename);
224 std::string savePathAndFileName = outputPath +fileStem +
"." + fileType;
225 MITK_INFO <<
"Copy resource " << savePathAndFileName;
231 int main(
int argc,
char* argv[] )
236 parser.
setTitle(
"Folder Registration");
238 parser.
setDescription(
"For detail description see https://docs.mitk.org/nightly/DiffusionMiniApps.html");
250 parser.
addArgument(
"resample",
"r",
mitkCommandLineParser::String,
"Resample (x,y,z)mm:",
"Resample provide x,y,z spacing in mm (e.g. -r 1,1,3), is not applied to tensor data",
us::Any());
256 map<string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
260 bool isBinary =
false;
261 bool alignOrigin =
false;
262 bool useLinearInterpol =
true;
264 if (parsedArgs.size() == 0)
269 if (parsedArgs.count(
"sinc-int"))
270 useLinearInterpol =
false;
272 if (parsedArgs.count(
"silent"))
275 if (parsedArgs.count(
"binary"))
278 if (parsedArgs.count(
"correct-origin"))
282 if ( parsedArgs.count(
"help") || parsedArgs.count(
"h"))
288 std::string refPattern =
"";
289 bool useFirstMoving =
false;
290 std::string movingImgPattern =
us::any_cast<
string>(parsedArgs[
"moving"]);
292 if (parsedArgs.count(
"fixed"))
298 useFirstMoving =
true;
299 refPattern = movingImgPattern;
302 std::string outputPath =
us::any_cast<
string>(parsedArgs[
"output"]);
304 std::string inputPath =
us::any_cast<
string>(parsedArgs[
"input"]);
309 std::vector<std::string> derPatterns;
311 if (parsedArgs.count(
"derived") || parsedArgs.count(
"d") )
313 std::string arg =
us::any_cast<
string>(parsedArgs[
"derived"]);
314 derPatterns =
split(arg ,
',');
318 std::vector<std::string> spacings;
320 bool doResampling =
false;
321 if (parsedArgs.count(
"resample") || parsedArgs.count(
"d") )
323 std::string arg =
us::any_cast<
string>(parsedArgs[
"resample"]);
324 spacings =
split(arg ,
',');
325 spacing[0] = atoi(spacings.at(0).c_str());
326 spacing[1] = atoi(spacings.at(1).c_str());
327 spacing[2] = atoi(spacings.at(2).c_str());
331 MITK_INFO <<
"Input Folder : " << inputPath;
332 MITK_INFO <<
"Looking for reference image ...";
335 if ((!useFirstMoving && referenceFileList.size() != 1) || (useFirstMoving && referenceFileList.size() == 0))
337 MITK_ERROR <<
"None or more than one possible reference images (" << refPattern <<
") found. Exiting." << referenceFileList.size();
338 MITK_INFO <<
"Choose a fixed arguement that is unique in the given folder!";
342 std::string referenceFileName = referenceFileList.at(0);
344 MITK_INFO <<
"Loading Reference (fixed) image: " << referenceFileName;
345 std::string fileType = itksys::SystemTools::GetFilenameExtension(referenceFileName);
351 resampleReference = refImage;
354 if (refImage.IsNull())
358 std::string savePathAndFileName =
GetSavePath(outputPath, referenceFileName);
366 std::string derivedResourceFilename;
372 std::cout <<
"<filter-start>";
373 std::cout <<
"<filter-name>Batched Registration</filter-name>";
374 std::cout <<
"<filter-comment>Starting registration ... </filter-comment>";
375 std::cout <<
"</filter-start>";
393 for (
unsigned int i =0; i < movingImagesList.size(); i++)
395 std::string fileMorphName = movingImagesList.at(i);
396 if (fileMorphName == referenceFileName)
402 MITK_INFO <<
"Processing image " << fileMorphName;
406 if (!itksys::SystemTools::FileExists(fileMorphName.c_str()))
408 MITK_WARN <<
"File does not exit. Skipping entry.";
416 std::string fileType = itksys::SystemTools::GetFilenameExtension(fileMorphName);
419 if (movingImage.IsNull())
423 MITK_INFO <<
"----------Registering moving image to reference----------";
428 savePathAndFileName =
GetSavePath(outputPath, fileMorphName);
429 if (fileType ==
".dwi")
432 if (movingImage->GetData() ==
nullptr)
443 std::cout <<
"<filter-progress-text progress=\"" <<
444 (float)i / (
float)movingImagesList.size()
445 <<
"\" >.</filter-progress-text>";
452 if (fList.size() > 0)
453 MITK_INFO <<
"----------DERIVED RESOURCES ---------";
454 for (
unsigned int j=0; j < fList.size(); j++)
456 derivedResourceFilename = fList.at(j);
457 MITK_INFO <<
"----Processing derived resorce " << derivedResourceFilename <<
" ...";
458 std::string fileType = itksys::SystemTools::GetFilenameExtension(derivedResourceFilename);
463 savePathAndFileName =
GetSavePath(outputPath, derivedResourceFilename);
469 std::cout <<
"<filter-end/>";
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
static void CopyResources(FileListType fileList, std::string outputPath)
Copy derived resources from first time step. Append _reg tag, but leave data untouched.
itk::SmartPointer< Self > Pointer
void setContributor(std::string contributor)
ValueType * any_cast(Any *operand)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
static void GetTransformation(Image::Pointer fixedImage, Image::Pointer movingImage, RidgidTransformType transformation, double *offset, bool useSameOrigin=true, mitk::Image *mask=NULL)
GetTransformation Registeres the moving to the fixed image and returns the according transformation...
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.
static FileListType CreateFileList(std::string folder, std::string postfix)
Create list of all files in provided folder ending with same postfix.
static bool SaveImage(mitk::Image::Pointer image, const std::string &path)
SaveImage Convenience method to save an arbitrary mitkImage.
static void ApplyTransformationToImage(mitk::Image::Pointer img, const RidgidTransformType &transformation, double *offset, mitk::Image *resampleReference=NULL, bool binary=false)
ApplyTransformationToImage Applies transformation from GetTransformation to provided image...
std::vector< std::string > FileListType
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)
static const std::string filename
Image class for storing images.
itk::Image< double, 3 > InputImageType
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static FileListType CreateDerivedFileList(std::string baseFN, std::string baseSuffix, std::vector< std::string > derivedPatterns)
Build a derived file name from moving images e.g. xxx_T2.nrrd becomes xxx_GTV.nrrd.
static void SaveImage(std::string fileName, mitk::Image *image, std::string fileType)
Save images according to file type.
static mitk::Image::Pointer ResampleBySpacing(mitk::Image *input, float *spacing, bool useLinInt=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 std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
static std::string GetSavePath(std::string outputFolder, std::string fileName)
static mitk::Image::Pointer ExtractFirstTS(mitk::Image *image, std::string fileType)
void setTitle(std::string title)
void setDescription(std::string description)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
int main(int argc, char *argv[])
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.