ANIMA  4.0
animaReadWriteFunctions.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <itkImageFileReader.h>
4 #include <itkImageFileWriter.h>
5 #include <itkExtractImageFilter.h>
6 
7 namespace anima
8 {
9 
10 template <class ImageType>
11 typename itk::SmartPointer<ImageType>
12 readImage(std::string filename)
13 {
14  typedef itk::ImageFileReader<ImageType> ReaderType;
15  typename ReaderType::Pointer reader = ReaderType::New();
16  reader->SetFileName(filename);
17 
18  typename itk::SmartPointer<ImageType> img;
19 
20  reader->Update();
21 
22  img = reader->GetOutput();
23  return img;
24 }
25 
26 template <class OutputImageType>
27 void
28 writeImage(std::string filename, OutputImageType* img)
29 {
30  typedef itk::ImageFileWriter<OutputImageType> WriterType;
31  typename WriterType::Pointer writer = WriterType::New();
32  writer->SetUseCompression(true);
33  writer->SetFileName(filename);
34  writer->SetInput(img);
35 
36  writer->Update();
37 }
38 
40 template <class InputImageType, class OutputImageType>
41 std::vector < itk::SmartPointer <OutputImageType> >
42 getImagesFromHigherDimensionImage(InputImageType *inputImage)
43 {
44  unsigned int highDimImage = InputImageType::ImageDimension;
45  unsigned int lowerDimImage = OutputImageType::ImageDimension;
46 
47  if (highDimImage != lowerDimImage + 1)
48  throw itk::ExceptionObject(__FILE__, __LINE__, "Trying to divide an image that doesn't have one more dimension",ITK_LOCATION);
49 
50  unsigned int ndim = inputImage->GetLargestPossibleRegion().GetSize()[lowerDimImage];
51 
52  typename InputImageType::RegionType largeRegion = inputImage->GetLargestPossibleRegion();
53  typename InputImageType::RegionType smallRegion = largeRegion;
54  typedef itk::ExtractImageFilter <InputImageType, OutputImageType> ExtractFilterType;
55 
56  std::vector < itk::SmartPointer <OutputImageType> > outputData;
57  smallRegion.SetSize(lowerDimImage,0);
58  for (unsigned int i = 0;i < ndim;++i)
59  {
60  smallRegion.SetIndex(lowerDimImage,i + largeRegion.GetIndex(lowerDimImage));
61 
62  typename ExtractFilterType::Pointer extractor = ExtractFilterType::New();
63  extractor->SetInput(inputImage);
64  extractor->SetExtractionRegion(smallRegion);
65  extractor->SetDirectionCollapseToGuess();
66 
67  extractor->Update();
68 
69  outputData.push_back(extractor->GetOutput());
70  outputData[i]->DisconnectPipeline();
71  }
72 
73  return outputData;
74 }
75 
77 template <class InputImageType, class ImageFilterType>
78 unsigned int
80  ImageFilterType *filter)
81 {
82  typedef itk::Image <typename InputImageType::PixelType, InputImageType::ImageDimension + 1> HigherDimImageType;
83  typedef itk::ImageFileReader <HigherDimImageType> HigherDimImageReaderType;
84  typedef itk::ImageFileReader < InputImageType > ImageReaderType;
85 
86  unsigned int nbPats = 0;
87 
88  itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(fileName.c_str(), itk::ImageIOFactory::ReadMode);
89 
90  if( !imageIO ) // file list
91  {
92  std::ifstream fileIn(fileName.c_str());
93  if (!fileIn.is_open())
94  {
95  std::string errStr = "Unable to read file: ";
96  errStr += fileName;
97 
98  throw itk::ExceptionObject(__FILE__, __LINE__,errStr,ITK_LOCATION);
99  }
100 
101  typename ImageReaderType::Pointer imageReader;
102 
103  while (!fileIn.eof())
104  {
105  char tmpStr[2048];
106  fileIn.getline(tmpStr,2048);
107 
108  if (strcmp(tmpStr,"") == 0)
109  continue;
110 
111  std::cout << "Loading image " << nbPats << " " << tmpStr << "..." << std::endl;
112  imageReader = ImageReaderType::New();
113  imageReader->SetFileName(tmpStr);
114  imageReader->Update();
115 
116  filter->SetInput(nbPats,imageReader->GetOutput());
117 
118  nbPats++;
119  }
120 
121  fileIn.close();
122  }
123  else // N+1D image tentative
124  {
125  // Now that we found the appropriate ImageIO class, ask it to read the meta data from the image file.
126  imageIO->SetFileName(fileName);
127  imageIO->ReadImageInformation();
128 
129  // Do we have a (N+1)D image ? If not, exiting, otherwise considering the last dimension as each input
130  unsigned int ndim = imageIO->GetNumberOfDimensions();
131 
132  if (ndim == InputImageType::ImageDimension)
133  {
134  filter->SetInput(anima::readImage <InputImageType> (fileName));
135  return 1;
136  }
137 
138  if (ndim != InputImageType::ImageDimension + 1)
139  {
140  std::string errStr = "Unable to read file: ";
141  errStr += fileName;
142 
143  throw itk::ExceptionObject(__FILE__, __LINE__,errStr,ITK_LOCATION);
144  }
145 
146  typename HigherDimImageReaderType::Pointer imageReader = HigherDimImageReaderType::New();
147  imageReader->SetImageIO(imageIO);
148  imageReader->SetFileName(fileName);
149  imageReader->Update();
150 
151  std::vector <typename InputImageType::Pointer> inputData;
152  inputData = anima::getImagesFromHigherDimensionImage<HigherDimImageType,InputImageType>(imageReader->GetOutput());
153 
154  for (unsigned int i = 0;i < inputData.size();++i)
155  filter->SetInput(i,inputData[i]);
156 
157  nbPats = inputData.size();
158  }
159 
160  return nbPats;
161 }
162 
163 }// end of namespace anima
void writeImage(std::string filename, OutputImageType *img)
std::vector< itk::SmartPointer< OutputImageType > > getImagesFromHigherDimensionImage(InputImageType *inputImage)
Get a vector of input images from a higher dimensional image.
itk::SmartPointer< ImageType > readImage(std::string filename)
unsigned int setMultipleImageFilterInputsFromFileName(std::string &fileName, ImageFilterType *filter)
Set inputs of an image to image filter from a file name containing either a list of files or a higher...