itkIO.h
Go to the documentation of this file.00001 #ifndef __itkIO_h
00002 #define __itkIO_h
00003
00004 #include "itkImageFileReader.h"
00005 #include "itkImageFileWriter.h"
00006 #include "itkImage.h"
00007 #include "itkCastImageFilter.h"
00008 #include "itkRescaleIntensityImageFilter.h"
00009 #include "itkOrientImageFilter.h"
00010 #include "itkSpatialOrientation.h"
00011 #include "itkSpatialOrientationAdapter.h"
00012 #include "itkAnalyzeImageIO.h"
00013 #include "itkMetaDataObject.h"
00014 #include "itkImageRegionIterator.h"
00015 #include "itkThinPlateR2LogRSplineKernelTransform.h"
00016 #include "itkResampleImageFilter.h"
00017 #include "itkImageDuplicator.h"
00018 #include "Imgmath.h"
00019 #include "itkGDCMSeriesFileNames.h"
00020 #include "itkImageSeriesReader.h"
00021 #include "itkGDCMImageIO.h"
00022
00023 namespace itkUtil
00024 {
00025 typedef itk::SpatialOrientationAdapter SOAdapterType;
00026 typedef SOAdapterType::DirectionType DirectionType;
00027
00034 template< typename TImage >
00035 typename TImage::Pointer ReadImage(const std::string fileName)
00036 {
00037 typename TImage::Pointer image;
00038 std::string extension = itksys::SystemTools::GetFilenameLastExtension(fileName);
00039 itk::GDCMImageIO::Pointer dicomIO = itk::GDCMImageIO::New();
00040 if ( dicomIO->CanReadFile( fileName.c_str() ) || ( itksys::SystemTools::LowerCase(extension) == ".dcm" ) )
00041 {
00042 std::string dicomDir = itksys::SystemTools::GetParentDirectory( fileName.c_str() );
00043
00044 itk::GDCMSeriesFileNames::Pointer FileNameGenerator = itk::GDCMSeriesFileNames::New();
00045 FileNameGenerator->SetUseSeriesDetails(true);
00046 FileNameGenerator->SetDirectory(dicomDir);
00047 typedef const std::vector< std::string > ContainerType;
00048 const ContainerType & seriesUIDs = FileNameGenerator->GetSeriesUIDs();
00049
00050 typedef typename itk::ImageSeriesReader< TImage > ReaderType;
00051 typename ReaderType::Pointer reader = ReaderType::New();
00052 reader->SetFileNames( FileNameGenerator->GetFileNames(seriesUIDs[0]) );
00053 reader->SetImageIO(dicomIO);
00054 try
00055 {
00056 reader->Update();
00057 }
00058 catch ( itk::ExceptionObject & err )
00059 {
00060 std::cout << "Caught an exception: " << std::endl;
00061 std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
00062 throw err;
00063 }
00064 catch ( ... )
00065 {
00066 std::cout << "Error while reading in image for patient " << fileName << std::endl;
00067 throw;
00068 }
00069 image = reader->GetOutput();
00070 image->DisconnectPipeline();
00071 reader->ReleaseDataFlagOn();
00072 }
00073 else
00074 {
00075 typedef itk::ImageFileReader< TImage > ReaderType;
00076 typename ReaderType::Pointer reader = ReaderType::New();
00077 reader->SetFileName( fileName.c_str() );
00078 try
00079 {
00080 reader->Update();
00081 }
00082 catch ( itk::ExceptionObject & err )
00083 {
00084 std::cout << "Caught an exception: " << std::endl;
00085 std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
00086 throw err;
00087 }
00088 catch ( ... )
00089 {
00090 std::cout << "Error while reading in image" << fileName << std::endl;
00091 throw;
00092 }
00093 image = reader->GetOutput();
00094 image->DisconnectPipeline();
00095 reader->ReleaseDataFlagOn();
00096 }
00097 return image;
00098 }
00099
00106 template< class ImageType1, class ImageType2 >
00107 bool
00108 ImagePhysicalDimensionsAreIdentical(typename ImageType1::Pointer & inputImage1,
00109 typename ImageType2::Pointer & inputImage2)
00110 {
00111 bool same = true;
00112
00113 same &= ( inputImage1->GetDirection() == inputImage2->GetDirection() );
00114 same &= ( inputImage1->GetSpacing() == inputImage2->GetSpacing() );
00115 same &= ( inputImage1->GetOrigin() == inputImage2->GetOrigin() );
00116 return same;
00117 }
00118
00119 template< class ImageType >
00120 typename ImageType::Pointer
00121 OrientImage(typename ImageType::ConstPointer & inputImage,
00122 itk::SpatialOrientation::ValidCoordinateOrientationFlags orient)
00123 {
00124 typename itk::OrientImageFilter< ImageType, ImageType >::Pointer orienter =
00125 itk::OrientImageFilter< ImageType, ImageType >::New();
00126
00127 orienter->SetDesiredCoordinateOrientation(orient);
00128 orienter->UseImageDirectionOn();
00129 orienter->SetInput(inputImage);
00130 orienter->Update();
00131 typename ImageType::Pointer returnval =
00132 orienter->GetOutput();
00133 returnval->DisconnectPipeline();
00134 orienter->ReleaseDataFlagOn();
00135 return returnval;
00136 }
00137
00138 template< class ImageType >
00139 typename ImageType::Pointer
00140 OrientImage(typename ImageType::ConstPointer & inputImage,
00141 const typename ImageType::DirectionType & dirCosines)
00142 {
00143 return OrientImage< ImageType >
00144 ( inputImage,
00145 SOAdapterType().FromDirectionCosines(
00146 dirCosines) );
00147 }
00148
00149 template< class ImageType >
00150 typename ImageType::Pointer
00151 OrientImage(typename ImageType::Pointer & inputImage,
00152 const typename ImageType::DirectionType & dirCosines)
00153 {
00154 typename ImageType::ConstPointer constImg(inputImage);
00155 return OrientImage< ImageType >
00156 ( constImg,
00157 SOAdapterType().FromDirectionCosines(
00158 dirCosines) );
00159 }
00160
00161 template< class ImageType >
00162 typename ImageType::Pointer
00163 OrientImage(typename ImageType::Pointer & inputImage,
00164 itk::SpatialOrientation::ValidCoordinateOrientationFlags orient)
00165 {
00166 typename ImageType::ConstPointer constImg(inputImage);
00167 return OrientImage< ImageType >(constImg, orient);
00168 }
00169
00170 template< class ImageType >
00171 typename ImageType::Pointer
00172 ReadImageAndOrient(const std::string & filename,
00173 itk::SpatialOrientation::ValidCoordinateOrientationFlags orient)
00174 {
00175 typename ImageType::Pointer img =
00176 ReadImage< ImageType >(filename);
00177 typename ImageType::ConstPointer constImg(img);
00178 typename ImageType::Pointer image = itkUtil::OrientImage< ImageType >(constImg,
00179 orient);
00180 return image;
00181 }
00182
00183 template< class ImageType >
00184 typename ImageType::Pointer
00185 ReadImageAndOrient(const std::string & filename,
00186 const DirectionType & dir)
00187 {
00188 return ReadImageAndOrient< ImageType >
00189 ( filename,
00190 SOAdapterType().FromDirectionCosines(dir) );
00191 }
00192
00193 template< typename TReadImageType >
00194 typename TReadImageType::Pointer ReadImageCoronal(const std::string & fileName)
00195 {
00196 DirectionType CORdir = SOAdapterType().ToDirectionCosines
00197 (itk::SpatialOrientation::ITK_COORDINATE_ORIENTATION_RIP);
00198
00199 return ReadImageAndOrient< TReadImageType >(fileName, CORdir);
00200 }
00201
00202 template< class ImageType >
00203 void
00204 WriteImage(typename ImageType::Pointer & image,
00205 const std::string & filename)
00206 {
00207 typedef itk::ImageFileWriter< ImageType > WriterType;
00208 typename WriterType::Pointer writer = WriterType::New();
00209 writer->UseCompressionOn();
00210 writer->SetFileName( filename.c_str() );
00211 writer->SetInput(image);
00212 try
00213 {
00214 writer->Update();
00215 }
00216 catch ( itk::ExceptionObject & err )
00217 {
00218 std::cout << "Exception Object caught: " << std::endl;
00219 std::cout << err << std::endl;
00220 throw;
00221 }
00222 }
00223
00235 template< class InputImageType, class OutputImageType >
00236 typename OutputImageType::Pointer
00237 TypeCast(const typename InputImageType::Pointer & input)
00238 {
00239 typedef itk::CastImageFilter< InputImageType,
00240 OutputImageType > CastToRealFilterType;
00241 typename CastToRealFilterType::Pointer toReal = CastToRealFilterType::New();
00242 toReal->SetInput(input);
00243 toReal->Update();
00244 return toReal->GetOutput();
00245 }
00246
00260 template< class InputImageType, class OutputImageType >
00261 typename OutputImageType::Pointer
00262 ScaleAndCast(const typename InputImageType::Pointer & image,
00263 const typename OutputImageType::PixelType OutputMin,
00264 const typename OutputImageType::PixelType OutputMax)
00265 {
00266 typedef itk::RescaleIntensityImageFilter< InputImageType,
00267 OutputImageType > R2CRescaleFilterType;
00268 typename R2CRescaleFilterType::Pointer RealToProbMapCast =
00269 R2CRescaleFilterType::New();
00270 RealToProbMapCast->SetOutputMinimum(OutputMin);
00271 RealToProbMapCast->SetOutputMaximum(OutputMax);
00272 RealToProbMapCast->SetInput(image);
00273 try
00274 {
00275 RealToProbMapCast->Update();
00276 }
00277 catch ( itk::ExceptionObject & e )
00278 {
00279 std::cerr << "Exception in Image cast." << std::endl;
00280 std::cerr << e.GetDescription() << std::endl;
00281 std::cerr << e.GetLocation() << std::endl;
00282 exit(-1);
00283 }
00284 typename OutputImageType::Pointer returnScaled = RealToProbMapCast->GetOutput();
00285 return returnScaled;
00286 }
00287
00302 template< class InputImageType, class OutputImageType >
00303 typename OutputImageType::Pointer
00304 PreserveCast(const typename InputImageType::Pointer image)
00305 {
00306 const typename InputImageType::PixelType inputmin =
00307 itk::NumericTraits< typename InputImageType::PixelType >::min();
00308 const typename InputImageType::PixelType inputmax =
00309 itk::NumericTraits< typename InputImageType::PixelType >::max();
00310 const typename OutputImageType::PixelType outputmin =
00311 itk::NumericTraits< typename OutputImageType::PixelType >::min();
00312 const typename OutputImageType::PixelType outputmax =
00313 itk::NumericTraits< typename OutputImageType::PixelType >::max();
00314 if ( ( inputmin >= outputmin ) && ( inputmax <= outputmax ) )
00315 {
00316 return TypeCast< InputImageType, OutputImageType >(image);
00317 }
00318 else
00319 {
00320 return ScaleAndCast< InputImageType, OutputImageType >(image,
00321 outputmin,
00322 outputmax);
00323 }
00324 }
00325
00326 template< class ImageType >
00327 typename ImageType::Pointer
00328 CopyImage(const typename ImageType::Pointer & input)
00329 {
00330 typedef itk::ImageDuplicator< ImageType > ImageDupeType;
00331 typename ImageDupeType::Pointer MyDuplicator = ImageDupeType::New();
00332 MyDuplicator->SetInputImage(input);
00333 MyDuplicator->Update();
00334 return MyDuplicator->GetOutput();
00335 }
00336 }
00337
00338 #endif