CBICA Toolkit  1.0.0
cbicaITKSafeImageIO.h File Reference

Defines safe input and output of itk::Images. More...

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"
#include "itkCastImageFilter.h"
#include "itkImageFileWriter.h"
#include "itkImageIOBase.h"
#include "itkImageIOFactory.h"
#include "itkNiftiImageIO.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkNumericSeriesFileNames.h"
#include "itkOrientImageFilter.h"
#include "itkChangeInformationImageFilter.h"
#include "gdcm/src/gdcmFile.h"
#include "gdcm/src/gdcmUtil.h"
#include "cbicaUtilities.h"
#include "cbicaITKImageInfo.h"
#include "cbicaITKUtilities.h"
#include "DicomIOManager.h"
Include dependency graph for cbicaITKSafeImageIO.h:

Go to the source code of this file.

Typedefs

using ImageTypeFloat3D = itk::Image< float, 3 >
 
using MaskType = itk::Image< unsigned int, 3 >
 
using TImageType = ImageTypeFloat3D
 

Functions

template<class TImageType = ImageTypeFloat3D>
itk::ImageSeriesReader< TImageType >::Pointer GetDicomImageReader (const std::string &dirName)
 Returns the unique series IDs in the specified directory. More...
 
template<class TImageType = ImageTypeFloat3D>
TImageType::Pointer GetImage (const std::string &fName, const std::string &supportedExtensions=".nii.gz,.nii", const std::string &delimitor=",")
 Get the itk::Image from input file name. More...
 
template<class TImageType = ImageTypeFloat3D>
itk::ImageFileReader< TImageType >::Pointer GetImageReader (const std::string &fName, const std::string &supportedExtensions=".nii.gz,.nii,.dcm", const std::string &delimitor=",")
 Get the itk::ImageFileReader from input file name. More...
 
template<class TImageType >
TImageType::Pointer GetImageWithOrientFix (const typename TImageType::Pointer inputImage)
 This is an inline function used to correct the orientation for correct visualization. More...
 
template<class TImageType = ImageTypeFloat3D>
TImageType::Pointer ReadImage (const std::string &fName, const std::string &supportedExtensions=".nii.gz,.nii,.dcm", const std::string &delimitor=",")
 Get the itk::Image from input file name. More...
 
template<class TImageType >
TImageType::Pointer ReadImageWithOrientFix (const std::string &fName, const std::string &supportedExtensions=".nii.gz,.nii", const std::string &delimitor=",")
 The reads the image according to the appropriate extension and outputs the result in ITK's RAI orientation for visualization. More...
 
template<typename ComputedImageType = ImageTypeFloat3D>
void WriteDicomImage (const typename ComputedImageType::Pointer imageToWrite, const std::string &dirName)
 
template<typename ComputedImageType = ImageTypeFloat3D>
void WriteDicomImage (const typename itk::ImageSeriesReader< ComputedImageType >::Pointer inputImageReader, const typename ComputedImageType::Pointer imageToWrite, const std::string &dirName)
 Write the itk::Image as a DICOM to the specified directory. More...
 
template<typename ComputedImageType = ImageTypeFloat3D, typename ExpectedImageType = ComputedImageType>
void WriteImage (typename ComputedImageType::Pointer imageToWrite, const std::string &fileName)
 Get the itk::Image from input dir name. More...
 

Detailed Description

Defines safe input and output of itk::Images.

Read and Write itk::Image data in a safe manner. Header-only

https://www.cbica.upenn.edu/sbia/software/
softw.nosp@m.are@.nosp@m.cbica.nosp@m..upe.nosp@m.nn.ed.nosp@m.u

Copyright (c) 2018 University of Pennsylvania. All rights reserved.
See COPYING file or https://www.cbica.upenn.edu/sbia/software/license.html

Function Documentation

◆ GetDicomImageReader()

itk::ImageSeriesReader< TImageType >::Pointer cbica::GetDicomImageReader ( const std::string &  dirName)

Returns the unique series IDs in the specified directory.

The check is only done on the DICOM tag provided, so if there are series with the same UID information (but are indeed different images), this function will not able to handle it.

Parameters
dirNameThe directory in question
tagToCheckThe tag on the basis of which the test is done; defaults to "0x0020|0x00E"
Returns
Vector of Series UIDs and fileName collection pairs, with each fileName collection corresponding to a UIDGet the Dicom image reader (not the image, the READER). This is useful for scenarios where reader meta information is needed for later writing step(s).

Usage:

typedef itk::Image< float, 3 > ExpectedImageType;
std::string inputDirName = parser.getParameterValue("inputDirName");
auto inputImageReader = GetDicomImageReader< ExpectedImageType >(inputDirName); // reads *all* DICOM images
auto inputImage = inputImageReader->GetOutput();
DoAwesomeStuffWithImage( inputImage );
Parameters
dirNameThis is the directory name of the DICOM image which needs to be loaded - if this is an image, the underlying path of the image is considered

References cbica::GetDicomImageReader(), cbica::getFilenamePath(), cbica::GetUniqueElements(), cbica::isDir(), and cbica::normPath().

Referenced by cbica::GetDicomImageReader().

◆ GetImage()

TImageType::Pointer cbica::GetImage ( const std::string &  fName,
const std::string &  supportedExtensions = ".nii.gz,.nii",
const std::string &  delimitor = "," 
)

Get the itk::Image from input file name.

Usage:

using ExpectedImageType = itk::Image< float, 3 >;
std::string inputFileName = parser.getParameterValue("inputImage");
auto inputImage_1 = cbica::ReadImage< ExpectedImageType >(inputFileName);
auto inputImage_2 = cbica::ReadImage< ExpectedImageType >(inputFileName, ".nii.gz,.img");
DoAwesomeStuffWithImage( inputImage );
Parameters
fNameFile name of the image
supportedExtensionsSupported extensions, defaults to ".nii.gz,.nii"
Returns
itk::ImageFileReader::Pointer templated over the same as requested by user

References cbica::GetImage().

Referenced by cbica::GetImage().

◆ GetImageReader()

itk::ImageFileReader< TImageType >::Pointer cbica::GetImageReader ( const std::string &  fName,
const std::string &  supportedExtensions = ".nii.gz,.nii,.dcm",
const std::string &  delimitor = "," 
)

Get the itk::ImageFileReader from input file name.

This is useful for scenarios where reader meta information is needed for later writing step(s).

Usage:

typedef itk::Image< float, 3 > ExpectedImageType;
std::string inputFileName = parser.getParameterValue("inputImage");
ExpectedImageType::Pointer inputImage_1 = GetImageReader< ExpectedImageType >(inputFileName)->GetOutput();
ExpectedImageType::Pointer inputImage_2 = GetImageReader< ExpectedImageType >(inputFileName, ".nii.gz,.img")->GetOutput();
DoAwesomeStuffWithImage( inputImage );
Parameters
fNamename of the image
supportedExtensionsSupported extensions, defaults to ".nii.gz,.nii"
Returns
itk::ImageFileReader::Pointer templated over the same as requested by user

References cbica::filesInDirectory(), cbica::getFilenameExtension(), cbica::getFilenamePath(), cbica::GetImageReader(), cbica::normPath(), and cbica::stringSplit().

Referenced by cbica::GetImageReader().

◆ GetImageWithOrientFix()

TImageType::Pointer cbica::GetImageWithOrientFix ( const typename TImageType::Pointer  inputImage)
inline

This is an inline function used to correct the orientation for correct visualization.

Parameters
inputImageThe input image
Returns
TImageType::Pointer templated over the same as requested by user

References cbica::GetImageWithOrientFix().

Referenced by cbica::GetImageWithOrientFix().

◆ ReadImage()

TImageType::Pointer cbica::ReadImage ( const std::string &  fName,
const std::string &  supportedExtensions = ".nii.gz,.nii,.dcm",
const std::string &  delimitor = "," 
)

Get the itk::Image from input file name.

Usage:

typedef itk::Image< float, 3 > ExpectedImageType;
std::string inputFileName = parser.getParameterValue("inputImage");
ExpectedImageType::Pointer inputImage_1 = ReadImage< ExpectedImageType >(inputFileName);
ExpectedImageType::Pointer inputImage_2 = ReadImage< ExpectedImageType >(inputFileName, ".nii.gz,.img");
DoAwesomeStuffWithImage( inputImage );
Parameters
fNamename of the image
supportedExtensionsSupported extensions, defaults to ".nii.gz,.nii"
Returns
itk::ImageFileReader::Pointer templated over the same as requested by user

References DicomIOManager< T >::GetITKImage(), DicomIOManager< T >::LoadDicom(), cbica::ReadImage(), and DicomIOManager< T >::SetDirectoryPath().

Referenced by cbica::ReadImage().

◆ ReadImageWithOrientFix()

TImageType::Pointer cbica::ReadImageWithOrientFix ( const std::string &  fName,
const std::string &  supportedExtensions = ".nii.gz,.nii",
const std::string &  delimitor = "," 
)

The reads the image according to the appropriate extension and outputs the result in ITK's RAI orientation for visualization.

Usage:

using ExpectedImageType = itk::Image< float, 3 >;
std::string inputFileName = parser.getParameterValue("inputImage");
auto inputImage_1 = ReadImageWithOrientFix< ExpectedImageType >(inputFileName);
auto inputImage_2 = ReadImageWithOrientFix< ExpectedImageType >(inputFileName, ".nii.gz,.img");
DoAwesomeStuffWithImage( inputImage );
Parameters
fNameFile name of the image
supportedExtensionsSupported extensions, defaults to ".nii.gz,.nii"
Returns
TImageType::Pointer templated over the same as requested by user

References cbica::getFilenameExtension(), and cbica::ReadImageWithOrientFix().

Referenced by cbica::ReadImageWithOrientFix().

◆ WriteDicomImage()

void cbica::WriteDicomImage ( const typename itk::ImageSeriesReader< ComputedImageType >::Pointer  inputImageReader,
const typename ComputedImageType::Pointer  imageToWrite,
const std::string &  dirName 
)

Write the itk::Image as a DICOM to the specified directory.

Usage:

typedef itk::Image< float, 3 > ComputedImageType;
typedef itk::Image< unsigned char, 3 > WrittenImageType;
itk::ImageSeriesReader< ComputedImageType >::Pointer inputImageReader = GetDicomImageReader< ComputedImageType >(inputDirName);
ComputedImageType::Pointer imageToWrite = GetImageAfterProcessing( inputImageReader->GetOutput() );
WriteImage< ComputedImageType, WrittenImageType >(inputImageReader, imageToWrite, dirNameToWriteImage); // casts imageToWrite to WrittenImageType
WriteImage< ComputedImageType >(inputImageReader, imageToWrite, dirNameToWriteImage); // writes imageToWrite as ComputedImageType
// at this point, the image has already been written
Parameters
inputImageReaderThe image reader for DICOM - this is necessary to populate the DICOM dictionary properly
imageToWritePointer to processed image data which is to be written
dirNameFile containing the image
Returns
itk::Image of specified pixel and dimension type

References cbica::createDir(), and cbica::isDir().

◆ WriteImage()

void cbica::WriteImage ( typename ComputedImageType::Pointer  imageToWrite,
const std::string &  fileName 
)

Get the itk::Image from input dir name.

Usage:

typedef itk::Image< float, 3 > ExpectedImageType;
std::string inputDirName = parser.getParameterValue("inputDirName");
ExpectedImageType::Pointer inputImage_1 = ReadDicomImage< ExpectedImageType >(inputFileName); // reads MRI and perfusion data by default tags "0008|0021,0020|0012"
ExpectedImageType::Pointer inputImage_2 = ReadDicomImage< ExpectedImageType >(inputDirName, "0008|0021")->GetOutput(); // only reads images with tag "0008|0021"
DoAwesomeStuffWithImage( inputImage );
Parameters
fNamename of the image
supportedExtensionsSupported extensions
Returns
itk::ImageFileReader::Pointer templated over the same as requested by userGet the itk::Image from input dir name

Usage:

typedef itk::Image< float, 3 > ExpectedImageType;
std::string inputDirName = parser.getParameterValue("inputDirName");
ExpectedImageType::Pointer inputImage_1 = ReadDicomImage< ExpectedImageType >(inputFileName); // reads MRI and perfusion data by default tags "0008|0021,0020|0012"
ExpectedImageType::Pointer inputImage_2 = ReadDicomImage< ExpectedImageType >(inputDirName, "0008|0021")->GetOutput(); // only reads images with tag "0008|0021"
DoAwesomeStuffWithImage( inputImage );

This function calls ReadDicomImage<> internally

Parameters
fNamename of the image
supportedExtensionsSupported extensions
Returns
itk::ImageFileReader::Pointer templated over the same as requested by userWrite the itk::Image to the file name

Usage:

typedef itk::Image< float, 3 > ComputedImageType;
typedef itk::Image< unsigned char, 3 > WrittenImageType;
ComputedImageType::Pointer imageToWrite = ComputedImageType::New();
imageToWrite = GetImageSomehow();
WriteImage< ComputedImageType >(imageToWrite, fileNameToWriteImage); // casts imageToWrite to WrittenImageType
WriteImage< ComputedImageType, WrittenImageType >(imageToWrite, fileNameToWriteImage);  // writes imageToWrite as ComputedImageType
// at this point, the image has already been written
Parameters
inputImagePointer to processed image data which is to be written
fileNameFile containing the image
Returns
itk::Image of specified pixel and dimension type

References cbica::getFilenameExtension(), and cbica::WriteImage().

Referenced by cbica::WriteImage().