X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FvvImageReader.txx;h=6a4333ff3c59c055c704117954747e7f656a133b;hb=f8d48c8481db84a9f2a9c140bf19c0c987d1bd59;hp=e3c2bca72d327e25743346b604dac59ba6c11bb0;hpb=62c20109b437bef1b9df091dfe7728a0fdf98bbc;p=clitk.git diff --git a/common/vvImageReader.txx b/common/vvImageReader.txx index e3c2bca..6a4333f 100644 --- a/common/vvImageReader.txx +++ b/common/vvImageReader.txx @@ -3,7 +3,7 @@ Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -14,7 +14,7 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html - ======================================================================-====*/ + ===========================================================================**/ #ifndef VVIMAGEREADER_TXX #define VVIMAGEREADER_TXX @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include @@ -33,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") @@ -66,6 +73,8 @@ void vvImageReader::UpdateWithDim(std::string InputPixelType) template void vvImageReader::UpdateWithDimAndInputPixelType() { + itk::AnalyzeImageIO *analyzeImageIO = NULL; + if (mType == MERGEDWITHTIME) // In this case we can load the images // one at the time to avoid excessive // memory use @@ -88,6 +97,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType() mLastError = error.str(); return; } + analyzeImageIO = dynamic_cast( reader->GetImageIO() ); } } else if (mType == SLICED) { mImage=vvImage::New(); @@ -114,14 +124,17 @@ 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 ) { + } catch ( itk::ExceptionObject & err ) { std::cerr << "Error while slicing " << mInputFilenames[0].c_str() << "(slice #" << mSlice << ") " << err << std::endl; return; } + analyzeImageIO = dynamic_cast( reader->GetImageIO() ); } else { if (mInputFilenames.size() > 1) { typedef itk::Image< InputPixelType, VImageDimension > InputImageType; @@ -132,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 ) { @@ -150,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; @@ -162,11 +177,99 @@ void vvImageReader::UpdateWithDimAndInputPixelType() mLastError = error.str(); return; } + analyzeImageIO = dynamic_cast( reader->GetImageIO() ); + } + } + + // 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(); } } } //---------------------------------------------------------------------------- +//---------------------------------------------------------------------------- +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 */