1 #ifndef CLITKVFMERGE_CXX
2 #define CLITKVFMERGE_CXX
5 * @file clitkVFMerge.cxx
6 * @author Jef Vandemeulebroucke <jefvdmb@gmail.com>
7 * @date June 15 10:14:53 2007
9 * @brief Read in one VF (ex mhd, vf) and write to another. Transforming from mm to voxels needed for the vf format is implemented in clitkVfImageIO.cxx .
15 #include "clitkVFMerge_ggo.h"
17 #include "clitkImageCommon.h"
20 //#include "itkReadTransform.h"
21 #include "itkImageFileWriter.h"
23 #include "itkImageFileReader.h"
24 //#include "itkRawImageIO.h"
27 int main( int argc, char *argv[] )
31 GGO(clitkVFMerge, args_info);
34 const unsigned int SpaceDimension = 3;
35 const unsigned int ModelDimension = 4;
36 typedef itk::Vector< float, SpaceDimension > Displacement;
37 typedef itk::Image< Displacement, SpaceDimension > DeformationFieldType;
38 typedef itk::Image< Displacement, ModelDimension > DynamicDeformationFieldType;
39 typedef itk::ImageFileReader< DeformationFieldType > DeformationFieldReaderType;
40 typedef itk::ImageFileWriter< DynamicDeformationFieldType > DynamicDeformationFieldWriterType;
44 DynamicDeformationFieldType::Pointer dynamicDeformationField=DynamicDeformationFieldType::New();
47 //declare their iterators
48 typedef itk::ImageRegionIterator< DynamicDeformationFieldType> DynamicDeformationFieldIteratorType;
49 DynamicDeformationFieldIteratorType *dynamicIteratorPointer= new DynamicDeformationFieldIteratorType;
51 for (unsigned int i=0 ; i< args_info.inputs_num ; i ++)
53 //read in the deformation field i
54 DeformationFieldReaderType::Pointer deformationFieldReader = DeformationFieldReaderType::New();
55 deformationFieldReader->SetFileName( args_info.inputs[i]);
56 if (args_info.verbose_flag) std::cout<<"Reading VF number "<< i+1 << std::endl;
57 deformationFieldReader->Update();
58 DeformationFieldType::Pointer currentDeformationField = deformationFieldReader->GetOutput();
60 //create an iterator for the current deformation field
61 typedef itk::ImageRegionIterator<DeformationFieldType> FieldIteratorType;
62 FieldIteratorType fieldIterator(currentDeformationField, currentDeformationField->GetLargestPossibleRegion());
64 //Allocate memory for the dynamic components
67 DynamicDeformationFieldType::RegionType dynamicDeformationFieldRegion;
68 DynamicDeformationFieldType::RegionType::SizeType dynamicDeformationFieldSize;
69 DeformationFieldType::RegionType::SizeType deformationFieldSize;
70 deformationFieldSize= currentDeformationField->GetLargestPossibleRegion().GetSize();
71 dynamicDeformationFieldSize[0]=deformationFieldSize[0];
72 dynamicDeformationFieldSize[1]=deformationFieldSize[1];
73 dynamicDeformationFieldSize[2]=deformationFieldSize[2];
74 dynamicDeformationFieldSize[3]=args_info.inputs_num;
75 dynamicDeformationFieldRegion.SetSize(dynamicDeformationFieldSize);
76 DynamicDeformationFieldType::IndexType start;
78 dynamicDeformationFieldRegion.SetIndex(start);
79 dynamicDeformationField->SetRegions(dynamicDeformationFieldRegion);
80 dynamicDeformationField->Allocate();
84 DeformationFieldType::SpacingType spacing= currentDeformationField->GetSpacing();
85 DynamicDeformationFieldType::SpacingType dynamicSpacing;
86 dynamicSpacing[0]=spacing[0];
87 dynamicSpacing[1]=spacing[1];
88 dynamicSpacing[2]=spacing[2];
89 dynamicSpacing[3]=args_info.spacing_arg; //JV par exemple
90 dynamicDeformationField->SetSpacing(dynamicSpacing);
91 DynamicDeformationFieldType::PointType origin;
92 origin[0]=args_info.xorigin_arg;
93 origin[1]=args_info.yorigin_arg;
94 origin[2]=args_info.zorigin_arg;
95 origin[3]=0; //temporal origin is always 0
96 dynamicDeformationField->SetOrigin(origin);
98 // Creating iterators for the currentDeformationField and the DynamicDeformationField
99 DynamicDeformationFieldIteratorType *dynamicIterator= new DynamicDeformationFieldIteratorType(dynamicDeformationField, dynamicDeformationField->GetLargestPossibleRegion());
100 dynamicIteratorPointer=dynamicIterator;
101 dynamicIteratorPointer->GoToBegin();
103 if (args_info.verbose_flag) std::cout<<"Merging VF number "<< i+1 << std::endl;
104 //Copy the current component of the input into dynamicDeformationFieldComponent
105 fieldIterator.GoToBegin();
106 while(!fieldIterator.IsAtEnd())
108 dynamicIteratorPointer->Set(fieldIterator.Get());
110 ++(*dynamicIteratorPointer);
115 //Write the vector field
116 DynamicDeformationFieldWriterType::Pointer writer = DynamicDeformationFieldWriterType::New();
117 writer->SetInput( dynamicDeformationField );
118 writer->SetFileName( args_info.output_arg );
119 if (args_info.verbose_flag) std::cout<<"Writing the dynamic VF"<< std::endl;
126 catch( itk::ExceptionObject & excp ) {
127 std::cerr << "Problem writing the output file" << std::endl;
128 std::cerr << excp << std::endl;