Skip to content

Commit dc444c8

Browse files
committed
Expanding IO capabilities and reducing code redundancy
1 parent 1eb6593 commit dc444c8

7 files changed

+105
-210
lines changed

IGR3D_MABMIS_Testing.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ void LabelFusion(std::string curSampleImgName, std::string outSampleSegName, std
645645
InternalImageType::Pointer curSampleImgPtr = 0;
646646

647647
imgoperator->ReadImage(curSampleImgName, curSampleImgPtr);
648+
const itk::ImageIOBase::IOComponentType ioType = imgoperator->GetIOPixelType(curSampleImgName);
648649

649650
// output sample label image pointer
650651
InternalImageType::Pointer outSampleSegPtr = 0;
@@ -677,7 +678,7 @@ void LabelFusion(std::string curSampleImgName, std::string outSampleSegName, std
677678
GaussianWeightedLabelFusion(curSampleImgPtr, outSampleSegPtr, warpedImgPtrs, warpedSegPtrs, numOfAtlases);
678679

679680
// output segmentation image
680-
imgoperator->WriteImage(outSampleSegName, outSampleSegPtr);
681+
imgoperator->WriteImage(outSampleSegName, outSampleSegPtr, ioType);
681682

682683
delete[] warpedImgPtrs;
683684
delete[] warpedSegPtrs;
@@ -1372,7 +1373,7 @@ void RegistrationOntoTreeRoot(vnl_vector<int> itree, // the incremental
13721373
// apply deformation field on seg file
13731374
if( isEvaluate )
13741375
{
1375-
dfoperator->ApplyDeformationFieldAndWriteWithFileNames(
1376+
dfoperator->ApplyDeformationFieldAndWriteWithTypeWithFileNames(
13761377
rootSegmentFileName,
13771378
invDeformationFileName,
13781379
deformedSegmentFileName, false);
@@ -1526,7 +1527,7 @@ void PairwiseRegistrationOnTreeViaRoot(int root,
15261527

15271528
if( isEvaluate )
15281529
{
1529-
dfoperator->ApplyDeformationFieldAndWriteWithFileNames(
1530+
dfoperator->ApplyDeformationFieldAndWriteWithTypeWithFileNames(
15301531
movingSegmentFileName,
15311532
dfFileName,
15321533
deformedSegmentFileName, false);

IGR3D_MABMIS_Training.cxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ int RegistrationBetweenRootandAtlases(int root, std::vector<std::string> imageFi
682682

683683
if( isEvaluate )
684684
{
685-
dfoperator->ApplyDeformationFieldAndWriteWithFileNames(
685+
dfoperator->ApplyDeformationFieldAndWriteWithTypeWithFileNames(
686686
fixedImageFileName,
687687
invDeformationFileName,
688688
invDeformedSegmentFileName, false);
@@ -785,9 +785,10 @@ std::vector<std::string> GenerateSimulatedData(int root, std::vector<std::string
785785
dfoperator->InverseDeformationField3D(df, invdf);
786786
InternalImageType::Pointer rootImg = 0;
787787
imgoperator->ReadImage(imageFiles[root], rootImg);
788+
const itk::ImageIOBase::IOComponentType ioType = imgoperator->GetIOPixelType(imageFiles[root]);
788789
InternalImageType::Pointer simImg = 0;
789790
dfoperator->ApplyDeformationField(rootImg, invdf, simImg, true);
790-
imgoperator->WriteImage(simulateTemplateFileNames[i], simImg);
791+
imgoperator->WriteImage(simulateTemplateFileNames[i], simImg, ioType);
791792
}
792793
std::cout << std::endl;
793794

itkMABMISDeformationFieldFilter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ class MABMISDeformationFieldFilter : public ImageToImageFilter<TInputImage, TOut
107107
void ApplyDeformationField(InternalImageType::Pointer movingImage, DeformationFieldType::Pointer deformationField,
108108
InternalImageType::Pointer & deformedImage, bool isLinearInterpolator);
109109

110-
void ApplyDeformationFieldAndWriteWithFileNames(std::string movingImageName, std::string deformationFieldFileName,
111-
std::string deformedImageName, bool isLinearInterpolator);
112-
113110
void ApplyDeformationFieldAndWriteWithTypeWithFileNames(std::string movingImageFileName,
114111
std::string deformationFieldFileName,
115112
std::string deformedImageFileName, bool isLinear);

itkMABMISDeformationFieldFilter.hxx

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -157,62 +157,15 @@ MABMISDeformationFieldFilter<TInputImage, TOutputImage>
157157
}
158158
}
159159

160-
// apply deformation field on image and write deformed image
161-
template <class TInputImage, class TOutputImage>
162-
void
163-
MABMISDeformationFieldFilter<TInputImage, TOutputImage>
164-
::ApplyDeformationFieldAndWriteWithFileNames(std::string movingImageName, std::string deformationFieldFileName,
165-
std::string deformedImageName, bool isLinearInterpolator)
166-
{
167-
typename ImageOperationType::Pointer imageoperator = ImageOperationType::New();
168-
169-
DeformationFieldType::Pointer deformationField = DeformationFieldType::New();
170-
ReadDeformationField(deformationFieldFileName, deformationField);
171-
172-
InternalImageType::Pointer movingImage = InternalImageType::New();
173-
imageoperator->ReadImage(movingImageName, movingImage);
174-
175-
InternalImageType::Pointer deformedImage = InternalImageType::New();
176-
177-
ApplyDeformationField(movingImage, deformationField, deformedImage, isLinearInterpolator);
178-
179-
imageoperator->WriteImage(deformedImageName, deformedImage);
180-
}
181-
182160
template <class TInputImage, class TOutputImage>
183161
void
184162
MABMISDeformationFieldFilter<TInputImage, TOutputImage>
185163
::ApplyDeformationFieldAndWriteWithTypeWithFileNames(std::string movingImageFileName,
186164
std::string deformationFieldFileName,
187165
std::string deformedImageFileName, bool isLinear)
188166
{
189-
itk::ImageIOBase::Pointer imageIO;
167+
const itk::ImageIOBase::IOComponentType ioType = imgoperator->GetIOPixelType(movingImageFileName);
190168

191-
try
192-
{
193-
imageIO = itk::ImageIOFactory::CreateImageIO(movingImageFileName.c_str(), itk::ImageIOFactory::ReadMode);
194-
if( imageIO )
195-
{
196-
imageIO->SetFileName(movingImageFileName);
197-
imageIO->ReadImageInformation();
198-
}
199-
else
200-
{
201-
std::cout << "Could not read the image information of " << movingImageFileName << "." << std::endl;
202-
exit( EXIT_FAILURE );
203-
}
204-
}
205-
catch( itk::ExceptionObject& err )
206-
{
207-
std::cout << "Could not read the image information of " << movingImageFileName << "." << std::endl;
208-
std::cout << err << std::endl;
209-
exit( EXIT_FAILURE );
210-
}
211-
// {UNKNOWNCOMPONENTTYPE,UCHAR,CHAR,USHORT,SHORT,UINT,INT,ULONG,LONG,FLOAT,DOUBLE} IOComponentType;
212-
// 0 1 2 3 4 5 6 7 8 9 10
213-
const int input_type = imageIO->GetComponentType(); // 9:float, 10:double
214-
215-
//
216169
DeformationFieldType::Pointer deformationField = DeformationFieldType::New();
217170
ReadDeformationField(deformationFieldFileName, deformationField);
218171

@@ -223,31 +176,9 @@ MABMISDeformationFieldFilter<TInputImage, TOutputImage>
223176

224177
ApplyDeformationField(movingImage, deformationField, deformedImage, isLinear);
225178

226-
if( input_type == 1 ) // UCHAR
227-
{
228-
imgoperator->WriteImageUCHAR(deformedImageFileName, deformedImage);
229-
}
230-
else if( input_type == 4 ) // SHORT
231-
{
232-
imgoperator->WriteImageSHORT(deformedImageFileName, deformedImage);
233-
}
234-
else if( input_type == 6 ) // INT
235-
{
236-
imgoperator->WriteImageINT(deformedImageFileName, deformedImage);
237-
}
238-
else if( input_type == 9 || input_type == 10 ) // FLOAT || DOUBLE
239-
{
240-
imgoperator->WriteImageFLOAT(deformedImageFileName, deformedImage);
241-
}
242-
else
243-
{
244-
std::cerr << "input_type type not supported " << input_type << std::endl;
245-
}
179+
imgoperator->WriteImage(deformedImageFileName, deformedImage, ioType);
246180
}
247181

248-
// end
249-
//
250-
251182
template <class TInputImage, class TOutputImage>
252183
void
253184
MABMISDeformationFieldFilter<TInputImage, TOutputImage>

itkMABMISImageOperationFilter.h

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,18 @@ class MABMISImageOperationFilter : public ImageToImageFilter<TInputImage, TOutpu
4242
typedef itk::Vector<InternalPixelType, ImageDimension> VectorPixelType;
4343

4444
// basic image type
45-
typedef itk::Image<CharPixelType, ImageDimension> CharImageType;
46-
typedef itk::Image<IntPixelType, ImageDimension> IntImageType;
47-
typedef itk::Image<ShortPixelType, ImageDimension> ShortImageType;
48-
typedef itk::Image<FloatPixelType, ImageDimension> FloatImageType;
4945
typedef itk::Image<InternalPixelType, ImageDimension> InternalImageType;
5046
typedef itk::Image<VectorPixelType, ImageDimension> DeformationFieldType;
5147

5248
// basic iterator type
5349
typedef itk::ImageRegionIterator<DeformationFieldType> DeformationFieldIteratorType;
5450
typedef itk::ImageRegionIterator<InternalImageType> InternalImageIteratorType;
55-
typedef itk::ImageRegionIterator<CharImageType> CharImageIteratorType;
5651

5752
// basic image reader/writer related type
58-
typedef itk::ImageFileReader<CharImageType> CharImageReaderType;
5953
typedef itk::ImageFileReader<InternalImageType> InternalImageReaderType;
6054
typedef itk::ImageFileWriter<InternalImageType> InternalImageWriterType;
61-
typedef itk::CastImageFilter<InternalImageType, CharImageType> Internal2CharCastFilterType;
62-
typedef itk::CastImageFilter<InternalImageType, FloatImageType> Internal2FloatCastFilterType;
63-
typedef itk::CastImageFilter<InternalImageType, IntImageType> Internal2IntCastFilterType;
64-
typedef itk::CastImageFilter<InternalImageType, ShortImageType> Internal2ShortCastFilterType;
6555

6656
typedef itk::WarpImageFilter<InternalImageType, InternalImageType, DeformationFieldType> InternalWarpFilterType;
67-
typedef itk::ImageFileWriter<CharImageType> CharImageWriterType;
68-
typedef itk::ImageFileWriter<IntImageType> IntImageWriterType;
69-
typedef itk::ImageFileWriter<FloatImageType> FloatImageWriterType;
70-
typedef itk::ImageFileWriter<ShortImageType> ShortImageWriterType;
7157

7258
typedef itk::ImageFileReader<DeformationFieldType> DeformationFieldReaderType;
7359
typedef itk::ImageFileWriter<DeformationFieldType> DeformationFieldWriterType;
@@ -78,24 +64,22 @@ class MABMISImageOperationFilter : public ImageToImageFilter<TInputImage, TOutpu
7864
/** Run-time type information (and related methods). */
7965
itkTypeMacro(MABMISImageOperationFilter, ImageToImageFilter);
8066

81-
void PairwiseDistanceAmongImages(std::vector<std::string> imageFileNames, int totalNumber,
67+
void PairwiseDistanceAmongImages(const std::vector<std::string>& imageFileNames, int totalNumber,
8268
vnl_matrix<double>& distanceMatrix);
8369

84-
double calculateDistanceMSD(std::string& filename1, std::string& filename2);
70+
double calculateDistanceMSD(const std::string& filename1, const std::string& filename2);
8571

86-
int ReadImage(std::string filename, InternalImageType::Pointer& image);
72+
int ReadImage(const std::string filename, InternalImageType::Pointer& image);
8773

88-
void WriteImageUCHAR(std::string filename, InternalImageType::Pointer image);
74+
itk::ImageIOBase::IOComponentType GetIOPixelType(const std::string& filename);
8975

90-
void WriteImageINT(std::string filename, InternalImageType::Pointer image);
76+
/** Write the image to disk, after casting it the the provided pixel type. */
77+
template <class PixelType>
78+
int WriteImage(const std::string filename, InternalImageType::Pointer image);
9179

92-
void WriteImageSHORT(std::string filename, InternalImageType::Pointer image);
80+
int WriteImage(const std::string filename, InternalImageType::Pointer image, itk::ImageIOBase::IOComponentType ioType);
9381

94-
void WriteImageFLOAT(std::string filename, InternalImageType::Pointer image);
95-
96-
void WriteImage(std::string filename, InternalImageType::Pointer image);
97-
98-
void WriteImage(std::string filename, InternalImageType::Pointer image, char* outputType);
82+
void WriteImage(const std::string filename, InternalImageType::Pointer image, char* outputType);
9983

10084
private:
10185
MABMISImageOperationFilter(const Self &); // purposely not implemented

0 commit comments

Comments
 (0)