3 #include <itkImageFileReader.h> 4 #include <itkImageFileWriter.h> 5 #include <itkExtractImageFilter.h> 10 template <
class ImageType>
11 typename itk::SmartPointer<ImageType>
14 typedef itk::ImageFileReader<ImageType> ReaderType;
15 typename ReaderType::Pointer reader = ReaderType::New();
16 reader->SetFileName(filename);
18 typename itk::SmartPointer<ImageType> img;
22 img = reader->GetOutput();
26 template <
class OutputImageType>
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);
40 template <
class InputImageType,
class OutputImageType>
41 std::vector < itk::SmartPointer <OutputImageType> >
44 unsigned int highDimImage = InputImageType::ImageDimension;
45 unsigned int lowerDimImage = OutputImageType::ImageDimension;
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);
50 unsigned int ndim = inputImage->GetLargestPossibleRegion().GetSize()[lowerDimImage];
52 typename InputImageType::RegionType largeRegion = inputImage->GetLargestPossibleRegion();
53 typename InputImageType::RegionType smallRegion = largeRegion;
54 typedef itk::ExtractImageFilter <InputImageType, OutputImageType> ExtractFilterType;
56 std::vector < itk::SmartPointer <OutputImageType> > outputData;
57 smallRegion.SetSize(lowerDimImage,0);
58 for (
unsigned int i = 0;i < ndim;++i)
60 smallRegion.SetIndex(lowerDimImage,i + largeRegion.GetIndex(lowerDimImage));
62 typename ExtractFilterType::Pointer extractor = ExtractFilterType::New();
63 extractor->SetInput(inputImage);
64 extractor->SetExtractionRegion(smallRegion);
65 extractor->SetDirectionCollapseToGuess();
69 outputData.push_back(extractor->GetOutput());
70 outputData[i]->DisconnectPipeline();
77 template <
class InputImageType,
class ImageFilterType>
80 ImageFilterType *filter)
82 typedef itk::Image <typename InputImageType::PixelType, InputImageType::ImageDimension + 1> HigherDimImageType;
83 typedef itk::ImageFileReader <HigherDimImageType> HigherDimImageReaderType;
84 typedef itk::ImageFileReader < InputImageType > ImageReaderType;
86 unsigned int nbPats = 0;
88 itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(fileName.c_str(), itk::ImageIOFactory::ReadMode);
92 std::ifstream fileIn(fileName.c_str());
93 if (!fileIn.is_open())
95 std::string errStr =
"Unable to read file: ";
98 throw itk::ExceptionObject(__FILE__, __LINE__,errStr,ITK_LOCATION);
101 typename ImageReaderType::Pointer imageReader;
103 while (!fileIn.eof())
106 fileIn.getline(tmpStr,2048);
108 if (strcmp(tmpStr,
"") == 0)
111 std::cout <<
"Loading image " << nbPats <<
" " << tmpStr <<
"..." << std::endl;
112 imageReader = ImageReaderType::New();
113 imageReader->SetFileName(tmpStr);
114 imageReader->Update();
116 filter->SetInput(nbPats,imageReader->GetOutput());
126 imageIO->SetFileName(fileName);
127 imageIO->ReadImageInformation();
130 unsigned int ndim = imageIO->GetNumberOfDimensions();
132 if (ndim == InputImageType::ImageDimension)
134 filter->SetInput(anima::readImage <InputImageType> (fileName));
138 if (ndim != InputImageType::ImageDimension + 1)
140 std::string errStr =
"Unable to read file: ";
143 throw itk::ExceptionObject(__FILE__, __LINE__,errStr,ITK_LOCATION);
146 typename HigherDimImageReaderType::Pointer imageReader = HigherDimImageReaderType::New();
147 imageReader->SetImageIO(imageIO);
148 imageReader->SetFileName(fileName);
149 imageReader->Update();
151 std::vector <typename InputImageType::Pointer> inputData;
152 inputData = anima::getImagesFromHigherDimensionImage<HigherDimImageType,InputImageType>(imageReader->GetOutput());
154 for (
unsigned int i = 0;i < inputData.size();++i)
155 filter->SetInput(i,inputData[i]);
157 nbPats = inputData.size();
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...