X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FvvImageReader.txx;h=9b0e21ae6c17f8fd498008272c53fa84a9aaaa5c;hb=c7a6bc15ce7ef75426e8f1db97453635fa430370;hp=3a38719c2d4a16eedaed620bccd77ebb3aa58510;hpb=8df51d54a1774008d30619248a3887b8afbe411b;p=clitk.git diff --git a/common/vvImageReader.txx b/common/vvImageReader.txx index 3a38719..9b0e21a 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") @@ -162,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; @@ -184,17 +187,90 @@ void vvImageReader::UpdateWithDimAndInputPixelType() 0.,0.,1.,0., 0.,-1.,0.,0., 0.,0.,0.,1.}; + // TODO SR and BP: check on the list of transforms and not the first only int i; - for(i=0; i<16 && m[i]==mImage->GetTransform()->GetMatrix()->GetElement(i%4, i/4); i++); + for(i=0; i<16 && m[i]==mImage->GetTransform()[0]->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(); + mImage->GetTransform()[0]->Identity(); } } } //---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +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()[0]->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()[0]->Identity(); + } + } +} +//---------------------------------------------------------------------------- #endif /* end #define vvImageReader_TXX */