X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FvvImageReader.txx;h=2eba11c81bc628f107d7f738c0a6d750fba7ef8f;hb=b8e5890d37dfd64409b9694f73c0be164a089e64;hp=e15e6d7688ff4a6a202e8f9e62bd42d133128ca5;hpb=4aeb9af4e4ba8f56f45f5698ae39458943282ee6;p=clitk.git diff --git a/common/vvImageReader.txx b/common/vvImageReader.txx index e15e6d7..2eba11c 100644 --- a/common/vvImageReader.txx +++ b/common/vvImageReader.txx @@ -23,8 +23,8 @@ #include #include #include -#include -#include +#include +#include "itkVectorImageToImageAdaptor.h" #include @@ -36,7 +36,12 @@ template void vvImageReader::UpdateWithDim(std::string InputPixelType) { if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME) - UpdateWithDimAndInputVectorPixelType,VImageDimension>(); + { + if (VImageDimension == 4) + UpdateWithDimAndInputVectorPixelType,VImageDimension>(); + else + UpdateWithDimAndInputVectorPixelType,VImageDimension>(); + } else if (InputPixelType == "short") UpdateWithDimAndInputPixelType(); else if (InputPixelType == "unsigned_short") @@ -56,7 +61,7 @@ void vvImageReader::UpdateWithDim(std::string InputPixelType) else std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl; - if (CLITK_EXPERIMENTAL && mLastError.size()==0) { + if (mLastError.size()==0) { //ReadNkiImageTransform(); ReadMatImageTransform(); } @@ -68,8 +73,6 @@ 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 @@ -84,6 +87,7 @@ void vvImageReader::UpdateWithDimAndInputPixelType() reader->SetFileName(*i); try { mImage->AddItkImage(reader->GetOutput()); + mImage->ComputeScalarRangeBase(reader->GetOutput()); } catch ( itk::ExceptionObject & err ) { std::cerr << "Error while reading " << mInputFilenames[0].c_str() << " " << err << std::endl; @@ -92,7 +96,6 @@ void vvImageReader::UpdateWithDimAndInputPixelType() mLastError = error.str(); return; } - analyzeImageIO = dynamic_cast( reader->GetImageIO() ); } } else if (mType == SLICED) { mImage=vvImage::New(); @@ -119,17 +122,97 @@ 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()); + mImage->ComputeScalarRangeBase(filter->GetOutput()); } 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 (mType == VECTORPIXELIMAGE) { + mImage=vvImage::New(); + typedef itk::VectorImage< InputPixelType, VImageDimension-1 > InputImageType; + typedef itk::ImageFileReader ReaderType; + typedef itk::Image OutputImageType; + typename ReaderType::Pointer reader = ReaderType::New(); + reader->SetFileName(mInputFilenames[0]); + reader->Update(); + typename InputImageType::Pointer input= reader->GetOutput(); + + typedef itk::VectorImageToImageAdaptor ImageAdaptorType; + typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New(); + typename OutputImageType::Pointer output = OutputImageType::New(); + + adaptor->SetExtractComponentIndex(0); + adaptor->SetImage(input); + + //Create the output + typename OutputImageType::IndexType index; + index.Fill(0); + typename OutputImageType::SizeType size; + size.Fill(input->GetNumberOfComponentsPerPixel()); + typename OutputImageType::SpacingType spacing; + spacing.Fill(1); + typename OutputImageType::PointType origin; + origin.Fill(0); + typename OutputImageType::DirectionType direction; + direction.SetIdentity(); + for (unsigned int pixelDim=0; pixelDimGetLargestPossibleRegion().GetSize(pixelDim); + spacing[pixelDim]=input->GetSpacing()[pixelDim]; + origin[pixelDim]=input->GetOrigin()[pixelDim]; + for (unsigned int pixelDim2=0; pixelDim2GetDirection()[pixelDim][pixelDim2]; + } + } + typename OutputImageType::RegionType region; + region.SetSize(size); + region.SetIndex(index); + output->SetRegions(region); + output->SetOrigin(origin); + output->SetSpacing(spacing); + output->SetDirection(direction); + output->Allocate(); + + //Copy each channel + for (unsigned int pixelDim=0; pixelDimGetNumberOfComponentsPerPixel(); ++pixelDim) + { + adaptor->SetExtractComponentIndex(pixelDim); + + itk::ImageRegionIterator imageIterator(input,input->GetLargestPossibleRegion()); + + while(!imageIterator.IsAtEnd()) + { + typename OutputImageType::IndexType indexVector; + indexVector.Fill(0); + for (unsigned int indexDim=0; indexDimSetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex())); + ++imageIterator; + } + } + +/* if (VImageDimension == 4) + mType == VECTORPIXELIMAGEWITHTIME; + else + mType == VECTORPIXELIMAGE;*/ + + try { + mImage = vvImageFromITK(output, mType == VECTORPIXELIMAGEWITHTIME); + mImage->ComputeScalarRangeBase(output); + } catch ( itk::ExceptionObject & err ) { + std::cerr << "Error while slicing " << mInputFilenames[0].c_str() + << " " << err << std::endl; + return; + } } else { if (mInputFilenames.size() > 1) { typedef itk::Image< InputPixelType, VImageDimension > InputImageType; @@ -172,24 +255,11 @@ 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(); - } - } + if (mType == DICOM && !mPatientCoordinateSystem) + mImage->InitializeTransform(); } //---------------------------------------------------------------------------- @@ -197,8 +267,6 @@ void vvImageReader::UpdateWithDimAndInputPixelType() template void vvImageReader::UpdateWithDimAndInputVectorPixelType() { - itk::AnalyzeImageIO *analyzeImageIO = NULL; - typedef itk::Image< InputPixelType, VImageDimension > InputImageType; typename InputImageType::Pointer input; @@ -233,35 +301,17 @@ void vvImageReader::UpdateWithDimAndInputVectorPixelType() mLastError = error.str(); return; } - analyzeImageIO = dynamic_cast( reader->GetImageIO() ); } - + typedef itk::Image< itk::Vector, VImageDimension > VectorImageType; - typedef itk::VectorCastImageFilter CasterType; + 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(); - } - } } //----------------------------------------------------------------------------