X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FvvImageReader.txx;h=6a4333ff3c59c055c704117954747e7f656a133b;hb=98043e0ab5bd602ef73ad163b667491a5d5142a9;hp=7607ab3a43cf27fc6b90c06ac926775b3f116e72;hpb=1c3533dee871b6d396c91c07f6c73a0dc46b2cd3;p=clitk.git diff --git a/common/vvImageReader.txx b/common/vvImageReader.txx index 7607ab3..6a4333f 100644 --- a/common/vvImageReader.txx +++ b/common/vvImageReader.txx @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -34,8 +35,13 @@ template void vvImageReader::UpdateWithDim(std::string InputPixelType) { - if (mType == VECTORFIELD) - UpdateWithDimAndInputPixelType,VImageDimension>(); + if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME) + { + if (VImageDimension == 4) + UpdateWithDimAndInputVectorPixelType,VImageDimension>(); + else + UpdateWithDimAndInputVectorPixelType,VImageDimension>(); + } else if (InputPixelType == "short") UpdateWithDimAndInputPixelType(); else if (InputPixelType == "unsigned_short") @@ -67,7 +73,7 @@ void vvImageReader::UpdateWithDim(std::string InputPixelType) template void vvImageReader::UpdateWithDimAndInputPixelType() { - itk::AnalyzeImageIO *analyzeImageIO; + itk::AnalyzeImageIO *analyzeImageIO = NULL; if (mType == MERGEDWITHTIME) // In this case we can load the images // one at the time to avoid excessive @@ -118,6 +124,9 @@ void vvImageReader::UpdateWithDimAndInputPixelType() filter->SetExtractionRegion(extractedRegion); filter->SetInput(reader->GetOutput()); filter->ReleaseDataFlagOn(); +#if ITK_VERSION_MAJOR == 4 + filter->SetDirectionCollapseToSubmatrix(); +#endif try { mImage->AddItkImage(filter->GetOutput()); } catch ( itk::ExceptionObject & err ) { @@ -136,7 +145,12 @@ void vvImageReader::UpdateWithDimAndInputPixelType() try { if (mType == IMAGEWITHTIME) - mImage=vvImageFromITK(reader->GetOutput(),true); + { + std::cerr << "We should never come here:" << std::endl + << " Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl + << " You are probably looking for MERGEDWITHTIME Type." << std::endl; + return; + } else mImage=vvImageFromITK(reader->GetOutput()); } catch ( itk::ExceptionObject & err ) { @@ -154,10 +168,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType() reader->ReleaseDataFlagOn(); try { - if (mType == IMAGEWITHTIME) - mImage=vvImageFromITK(reader->GetOutput(),true); - else - mImage=vvImageFromITK(reader->GetOutput()); + mImage = vvImageFromITK(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME); } catch ( itk::ExceptionObject & err ) { std::cerr << "Error while reading " << mInputFilenames[0].c_str() << " " << err << std::endl; @@ -172,11 +183,14 @@ void vvImageReader::UpdateWithDimAndInputPixelType() // For unknown analyze orientations, we set identity if(analyzeImageIO) { - const double m[16] = {1.,0.,0.,0.,0.,0.,1.,0.,0.,-1.,0.,0.,0.,0.,0.,1.}; + const double m[16] = {1.,0.,0.,0., + 0.,0.,1.,0., + 0.,-1.,0.,0., + 0.,0.,0.,1.}; int i; - for(i=0; m[i]==mImage->GetTransform()->GetMatrix()->GetElement(i%4, i/4) && i<16; i++); + for(i=0; i<16 && m[i]==mImage->GetTransform()->GetMatrix()->GetElement(i%4, i/4); i++); if(i==16) { - itkWarningMacro(<< "Analyze image file format detected with unknown orientation." + itkWarningMacro(<< "Analyze image file format detected with unknown orientation. " << "Forcing identity orientation, use other file format if not ok."); mImage->GetTransform()->Identity(); } @@ -184,6 +198,78 @@ void vvImageReader::UpdateWithDimAndInputPixelType() } //---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +template +void vvImageReader::UpdateWithDimAndInputVectorPixelType() +{ + itk::AnalyzeImageIO *analyzeImageIO = NULL; + + typedef itk::Image< InputPixelType, VImageDimension > InputImageType; + typename InputImageType::Pointer input; + + if (mInputFilenames.size() > 1) { + typedef itk::ImageSeriesReader ReaderType; + typename ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileNames(mInputFilenames); + reader->ReleaseDataFlagOn(); + try { + reader->Update(); + input = reader->GetOutput(); + } catch ( itk::ExceptionObject & err ) { + std::cerr << "Error while reading image series:" << err << std::endl; + std::stringstream error; + error << err; + mLastError = error.str(); + return; + } + } else { + typedef itk::ImageFileReader ReaderType; + typename ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(mInputFilenames[0]); + reader->ReleaseDataFlagOn(); + try { + reader->Update(); + input = reader->GetOutput(); + } catch ( itk::ExceptionObject & err ) { + std::cerr << "Error while reading " << mInputFilenames[0].c_str() + << " " << err << std::endl; + std::stringstream error; + error << err; + mLastError = error.str(); + return; + } + analyzeImageIO = dynamic_cast( reader->GetImageIO() ); + } + + typedef itk::Image< itk::Vector, VImageDimension > VectorImageType; + typedef itk::FlexibleVectorCastImageFilter CasterType; + typename VectorImageType::Pointer casted_input; + typename CasterType::Pointer caster = CasterType::New(); + caster->SetInput(input); + caster->Update(); + casted_input = caster->GetOutput(); + + mImage = vvImageFromITK >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME); + + // For unknown analyze orientations, we set identity + if (analyzeImageIO) + { + const double m[16] = {1.,0.,0.,0., + 0.,0.,1.,0., + 0.,-1.,0.,0., + 0.,0.,0.,1.}; + int i; + for (i = 0; i < 16 && m[i] == mImage->GetTransform()->GetMatrix()->GetElement(i % 4, i / 4); i++) + ; + if (i == 16) + { + itkWarningMacro(<< "Analyze image file format detected with unknown orientation. " + << "Forcing identity orientation, use other file format if not ok."); + mImage->GetTransform()->Identity(); + } + } +} +//---------------------------------------------------------------------------- #endif /* end #define vvImageReader_TXX */