1 #include <tclap/CmdLine.h> 4 #include <itkCommand.h> 5 #include <itkExtractImageFilter.h> 17 template <
class InputImageType,
unsigned int InputDim,
class OutputImageType >
21 unsigned int input = 0;
23 typename OutputImageType::Pointer baseImg;
26 std::cout <<
"No base given, direction of the created one will be guessed....\n" << std::endl;
28 typename InputImageType::Pointer fimage = anima::readImage<InputImageType>(args.
inputs[
input]);
31 typename OutputImageType::RegionType finalRegion;
32 typename OutputImageType::SizeType finalSize;
33 typename OutputImageType::IndexType finalIndex;
34 typename OutputImageType::PointType finalOrigin;
35 typename OutputImageType::SpacingType finalSpacing;
36 typename OutputImageType::DirectionType finalDirection;
38 for (
unsigned int d = 0; d < InputDim; ++d)
41 finalIndex[d] = fimage->GetLargestPossibleRegion().GetIndex()[d];
42 finalSize[d] = fimage->GetLargestPossibleRegion().GetSize()[d];
43 finalOrigin[d] = fimage->GetOrigin()[d];
44 finalSpacing[d] = fimage->GetSpacing()[d];
45 for(
unsigned int i = 0; i < InputDim; ++i)
46 finalDirection[d][i] = fimage->GetDirection()[d][i];
48 finalIndex[InputDim] = 0;
49 finalSize[InputDim] = 1;
50 finalOrigin[InputDim] = args.
origin;
51 finalSpacing[InputDim] = args.
spacing;
53 for(
unsigned int i = 0; i < InputDim; ++i)
55 finalDirection[InputDim][i] = 0;
56 finalDirection[i][InputDim] = 0;
58 finalDirection[InputDim][InputDim] = 1;
60 finalRegion.SetIndex(finalIndex);
61 finalRegion.SetSize(finalSize);
63 baseImg = OutputImageType::New();
64 baseImg->Initialize();
65 baseImg->SetRegions(finalRegion);
66 baseImg->SetOrigin(finalOrigin);
67 baseImg->SetSpacing(finalSpacing);
68 baseImg->SetDirection(finalDirection);
69 baseImg->SetNumberOfComponentsPerPixel(args.
nbComponents);
72 typedef itk::ImageRegionIterator <OutputImageType> FillIteratorType;
73 FillIteratorType fillItr(baseImg, baseImg->GetLargestPossibleRegion());
74 typedef itk::ImageRegionConstIterator<InputImageType> SourceIteratorType;
75 SourceIteratorType srcItr(fimage, fimage->GetLargestPossibleRegion());
76 while(!srcItr.IsAtEnd())
78 fillItr.Set(srcItr.Get());
82 std::cout <<
"input " << input <<
" / " << args.
inputs.size() <<
" concatenated..." << std::endl;
85 baseImg = anima::readImage<OutputImageType>(args.
base);
88 typename OutputImageType::Pointer outputImg;
89 typename OutputImageType::RegionType finalRegion;
90 typename OutputImageType::SizeType finalSize;
91 typename OutputImageType::IndexType finalIndex;
92 typename OutputImageType::PointType finalOrigin;
93 typename OutputImageType::SpacingType finalSpacing;
94 typename OutputImageType::DirectionType finalDirection;
96 for (
unsigned int d = 0; d < InputDim; ++d)
98 finalIndex[d] = baseImg->GetLargestPossibleRegion().GetIndex()[d];
99 finalSize[d] = baseImg->GetLargestPossibleRegion().GetSize()[d];
100 finalOrigin[d] = baseImg->GetOrigin()[d];
101 finalSpacing[d] = baseImg->GetSpacing()[d];
103 for(
unsigned int i = 0;i < InputDim;++i)
104 finalDirection[d][i] = baseImg->GetDirection()[d][i];
107 for(
unsigned int i = 0;i < InputDim + 1;++i)
109 finalDirection[InputDim][i] = baseImg->GetDirection()[InputDim][i];
110 finalDirection[i][InputDim] = baseImg->GetDirection()[i][InputDim];
113 finalIndex[InputDim] = baseImg->GetLargestPossibleRegion().GetIndex()[InputDim];
114 finalSize[InputDim] = baseImg->GetLargestPossibleRegion().GetSize()[InputDim] + args.
inputs.size() -
input;
115 finalOrigin[InputDim] = baseImg->GetOrigin()[InputDim];
116 finalSpacing[InputDim] = baseImg->GetSpacing()[InputDim];
118 finalRegion.SetIndex(finalIndex);
119 finalRegion.SetSize(finalSize);
121 outputImg = OutputImageType::New();
122 outputImg->Initialize();
123 outputImg->SetRegions(finalRegion);
124 outputImg->SetOrigin(finalOrigin);
125 outputImg->SetSpacing(finalSpacing);
126 outputImg->SetDirection(finalDirection);
127 outputImg->SetNumberOfComponentsPerPixel(args.
nbComponents);
128 outputImg->Allocate();
131 typedef itk::ImageRegionIterator <OutputImageType> FillIteratorType;
132 FillIteratorType fillItr(outputImg, outputImg->GetLargestPossibleRegion());
133 typedef itk::ImageRegionConstIterator<OutputImageType> BaseIteratorType;
134 BaseIteratorType baseItr(baseImg, baseImg->GetLargestPossibleRegion());
135 while(!baseItr.IsAtEnd())
137 fillItr.Set(baseItr.Get());
138 ++fillItr; ++baseItr;
142 for(
unsigned int i = input; i < args.
inputs.size();++i)
144 typename InputImageType::Pointer inputImg = anima::readImage<InputImageType>(args.
inputs[i]);
146 typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
147 InputIteratorType inputItr(inputImg, inputImg->GetLargestPossibleRegion());
148 while(!inputItr.IsAtEnd())
150 fillItr.Set(inputItr.Get());
151 ++fillItr; ++inputItr;
154 std::cout <<
"input " << i+1 <<
" / " << args.
inputs.size() <<
" concatenated..." << std::endl;
158 anima::writeImage<OutputImageType>(args.
output, outputImg);
161 template <
class ComponentType,
unsigned int InputDim>
169 typedef itk::Image<ComponentType, InputDim> InputImageType;
170 typedef itk::Image<ComponentType, InputDim + 1> OutputImageType;
171 concatenate <InputImageType, InputDim, OutputImageType >(args);
175 typedef itk::VectorImage<ComponentType, InputDim> InputVectorImageType;
176 typedef itk::VectorImage<ComponentType, InputDim + 1> OutputVectorImageType;
177 concatenate < InputVectorImageType, InputDim, OutputVectorImageType >(args);
180 itk::ExceptionObject excp(__FILE__, __LINE__,
"Number of component not supported.", ITK_LOCATION);
185 template <
class ComponentType >
190 unsigned int nbDim = imageIO->GetNumberOfDimensions();
195 retrieveNbComponent<ComponentType, 2>(args,
imageIO);
198 retrieveNbComponent<ComponentType, 3>(args,
imageIO);
201 itk::ExceptionObject excp(__FILE__, __LINE__,
"Number of dimension not supported.", ITK_LOCATION);
209 switch (imageIO->GetComponentType())
211 case itk::ImageIOBase::UCHAR:
212 case itk::ImageIOBase::USHORT:
213 case itk::ImageIOBase::UINT:
214 case itk::ImageIOBase::ULONG:
215 retrieveNbDimensions<unsigned int>(args,
imageIO);
217 case itk::ImageIOBase::CHAR:
218 case itk::ImageIOBase::SHORT:
219 case itk::ImageIOBase::INT:
220 case itk::ImageIOBase::LONG:
221 retrieveNbDimensions<int>(args,
imageIO);
223 case itk::ImageIOBase::FLOAT:
224 retrieveNbDimensions<double>(args,
imageIO);
226 case itk::ImageIOBase::DOUBLE:
227 retrieveNbDimensions<double>(args,
imageIO);
230 itk::ExceptionObject excp(__FILE__, __LINE__,
"Component type not supported.", ITK_LOCATION);
236 int main(
int ac,
const char** av)
239 TCLAP::CmdLine cmd(
"Concatenate args serie of image between them.\nYou can give several -i input to concatenate.\n\ 240 A -b base image is facultative, if it is given, all the inputs will be add at the end of the last dim of this base.\n\ 241 if args base is given the geometry of the base is used otherwise it use the geometry of the first given input.\n\ 242 with the origin -O(def = 0) and spacing -s(def = 1) passed as arguments.\ 243 It's your responsability to give args set of well formed input.\n\n\ 244 Example: the arguments -b 4x4x4x1 and -i 4x4x4 -i 4x4x4 will result on an outpu 4x4x4x3.\n\ 245 INRIA / IRISA - VisAGeS/Empenn Team",
248 TCLAP::ValueArg<std::string> baseArg(
"b",
250 "Inputs will be concatenate with this base image",
256 TCLAP::MultiArg<std::string> inputArg(
"i",
258 "Input images to concatenate",
263 TCLAP::ValueArg<std::string> outputArg(
"o",
270 TCLAP::ValueArg<double> originArg(
"O",
272 "Origin of the last dimension of the concatenated image. ignore if args base has been given",
277 TCLAP::ValueArg<double> spacingArg(
"s",
279 "Spacing of the last dimension of the concatenated image. ignore if args base has been given",
289 catch (TCLAP::ArgException& e)
291 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
296 args.
inputs = inputArg.getValue();
299 itk::ImageIOBase::Pointer
imageIO = itk::ImageIOFactory::CreateImageIO(args.
inputs[0].c_str(),
300 itk::ImageIOFactory::ReadMode);
304 std::ifstream fileIn(args.
inputs[0].c_str());
305 std::vector <std::string> listInputNames;
307 if (!fileIn.is_open())
309 std::cerr <<
"Unable to read file: " << args.
inputs[0] << std::endl;
313 while (!fileIn.eof())
316 fileIn.getline(tmpStr,2048);
318 if (strcmp(tmpStr,
"") == 0)
321 listInputNames.push_back(tmpStr);
326 if (listInputNames.size() > 0)
327 imageIO = itk::ImageIOFactory::CreateImageIO(listInputNames[0].c_str(),itk::ImageIOFactory::ReadMode);
331 std::cerr <<
"Itk could not find suitable IO factory for the input" << std::endl;
335 args.
inputs = listInputNames;
339 imageIO->SetFileName(args.
inputs[0]);
340 imageIO->ReadImageInformation();
342 std::cout<<
"\npreparing filter...\n";
344 args.
output = outputArg.getValue();
345 args.
base = baseArg.getValue();
346 args.
origin = originArg.getValue();
347 args.
spacing = spacingArg.getValue();
353 catch ( itk::ExceptionObject & err )
355 std::cerr <<
"Itk cannot concatenate, be sure to use valid arguments..." << std::endl;
356 std::cerr << err << std::endl;
void concatenate(const arguments &args)
std::vector< std::string > inputs
void retrieveNbDimensions(arguments &args, itk::ImageIOBase::Pointer imageIO)
void retrieveNbComponent(arguments &args, itk::ImageIOBase::Pointer imageIO)
itk::ImageIOBase::Pointer imageIO
unsigned int nbComponents
int main(int ac, const char **av)
void retrieveComponentType(arguments &args, itk::ImageIOBase::Pointer imageIO)