From 4aeb9af4e4ba8f56f45f5698ae39458943282ee6 Mon Sep 17 00:00:00 2001 From: Vivien Delmon Date: Fri, 3 Feb 2012 13:22:57 +0100 Subject: [PATCH] Correct bug when opening non 3D vector field - vvImageReader was opening vector field of nD vectors with the itk 3D reader. Now we open the vector field with the appropriate itk reader and then convert it to 3D vectors which are the only one supported by vv. --- common/vvImageReader.h | 2 + common/vvImageReader.txx | 79 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/common/vvImageReader.h b/common/vvImageReader.h index 2f1de53..dae05e4 100644 --- a/common/vvImageReader.h +++ b/common/vvImageReader.h @@ -85,6 +85,8 @@ protected: //==================================================================== template void UpdateWithDimAndInputPixelType(); + template + void UpdateWithDimAndInputVectorPixelType(); ///Input dimension and pixel type int mDim; std::string mInputPixelType; diff --git a/common/vvImageReader.txx b/common/vvImageReader.txx index 2d7d5b5..e15e6d7 100644 --- a/common/vvImageReader.txx +++ b/common/vvImageReader.txx @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -35,7 +36,7 @@ template void vvImageReader::UpdateWithDim(std::string InputPixelType) { if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME) - UpdateWithDimAndInputPixelType,VImageDimension>(); + UpdateWithDimAndInputVectorPixelType,VImageDimension>(); else if (InputPixelType == "short") UpdateWithDimAndInputPixelType(); else if (InputPixelType == "unsigned_short") @@ -162,10 +163,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType() reader->ReleaseDataFlagOn(); try { - if (mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME) - 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; @@ -195,6 +193,77 @@ 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::VectorCastImageFilter CasterType; + typename VectorImageType::Pointer casted_input; + typename CasterType::Pointer caster = CasterType::New(); + caster->SetInput(input); + 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 */ -- 2.45.1