X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FvvImageReader.txx;h=2eba11c81bc628f107d7f738c0a6d750fba7ef8f;hb=f2abd66846f46f61e6f16e339da46515525826b1;hp=6a4333ff3c59c055c704117954747e7f656a133b;hpb=98043e0ab5bd602ef73ad163b667491a5d5142a9;p=clitk.git diff --git a/common/vvImageReader.txx b/common/vvImageReader.txx index 6a4333f..2eba11c 100644 --- a/common/vvImageReader.txx +++ b/common/vvImageReader.txx @@ -23,8 +23,8 @@ #include #include #include -#include #include +#include "itkVectorImageToImageAdaptor.h" #include @@ -61,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(); } @@ -73,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 @@ -89,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; @@ -97,7 +96,6 @@ void vvImageReader::UpdateWithDimAndInputPixelType() mLastError = error.str(); return; } - analyzeImageIO = dynamic_cast( reader->GetImageIO() ); } } else if (mType == SLICED) { mImage=vvImage::New(); @@ -124,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; @@ -177,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(); } //---------------------------------------------------------------------------- @@ -202,8 +267,6 @@ void vvImageReader::UpdateWithDimAndInputPixelType() template void vvImageReader::UpdateWithDimAndInputVectorPixelType() { - itk::AnalyzeImageIO *analyzeImageIO = NULL; - typedef itk::Image< InputPixelType, VImageDimension > InputImageType; typename InputImageType::Pointer input; @@ -238,7 +301,6 @@ void vvImageReader::UpdateWithDimAndInputVectorPixelType() mLastError = error.str(); return; } - analyzeImageIO = dynamic_cast( reader->GetImageIO() ); } typedef itk::Image< itk::Vector, VImageDimension > VectorImageType; @@ -250,24 +312,6 @@ void vvImageReader::UpdateWithDimAndInputVectorPixelType() 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(); - } - } } //----------------------------------------------------------------------------