X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=itk%2FclitkCropLikeImageFilter.txx;h=6399ec6ac7e5419ca870e6e7cd61bf6b072810ef;hb=8d51bd1cb7e9ca416b46677dcd73ba76d107a587;hp=90190c61b83eaae08c92fe02a2e1a2b08fbc8790;hpb=523a9f961d83eaed5ec3365a07b9f6be35b538f0;p=clitk.git diff --git a/itk/clitkCropLikeImageFilter.txx b/itk/clitkCropLikeImageFilter.txx index 90190c6..6399ec6 100644 --- a/itk/clitkCropLikeImageFilter.txx +++ b/itk/clitkCropLikeImageFilter.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,17 +14,14 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html - ======================================================================-====*/ + ===========================================================================**/ #ifndef CLITKCROPLIKEIMAGEFILTER_TXX #define CLITKCROPLIKEIMAGEFILTER_TXX // clitk #include "clitkCommon.h" - -// itk -#include "itkRegionOfInterestImageFilter.h" - +#include "clitkPasteImageFilter.h" //-------------------------------------------------------------------- template @@ -33,6 +30,7 @@ CropLikeImageFilter():itk::ImageToImageFilter() { this->SetNumberOfRequiredInputs(1); m_LikeImage = NULL; m_LikeFilenameIsGiven = false; + this->SetBackgroundValue(typename PixelTraits::ValueType(0)); m_CropAlongThisDimension.resize(ImageType::ImageDimension); for(uint i=0; i * like, int axe template void clitk::CropLikeImageFilter:: -SetInput(const ImageType * image) { - // Process object is not const-correct so the const casting is required. - this->SetNthInput(0, const_cast( image )); +GenerateInputRequestedRegion() { + // Needed because output region can be larger than input + ImagePointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); + input->SetRequestedRegion(input->GetLargestPossibleRegion()); } //-------------------------------------------------------------------- - + //-------------------------------------------------------------------- template void clitk::CropLikeImageFilter:: -GenerateOutputInformation() { +GenerateOutputInformation() { // Get input pointers ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); // Get output pointer ImagePointer output = this->GetOutput(0); - + // Get input info typename ImageType::SizeType likeSize; typename ImageType::IndexType likeStart; typename ImageType::PointType likeOrigin; typename ImageType::SpacingType likeSpacing; - if (m_LikeImage) { + if (m_LikeImage) { likeSize = m_LikeImage->GetLargestPossibleRegion().GetSize(); likeStart = m_LikeImage->GetLargestPossibleRegion().GetIndex(); likeOrigin = m_LikeImage->GetOrigin(); likeSpacing = m_LikeImage->GetSpacing(); + output->CopyInformation(m_LikeImage); } else { + // Only load the header (allows to use 'like' with any image type) if (m_LikeFilenameIsGiven) { itk::ImageIOBase::Pointer header = readImageHeader(m_LikeFilename); for(unsigned int i=0; iGetIORegion().GetSize()[i]; //GetDimensions(i); - likeStart[i] = header->GetIORegion().GetIndex()[i]; + likeSize[i] = header->GetDimensions(i); + likeStart[i] = 0;//header->GetIORegion().GetIndex()[i]; likeOrigin[i] = header->GetOrigin(i); likeSpacing[i] = header->GetSpacing(i); } @@ -125,33 +126,89 @@ GenerateOutputInformation() { } } - // Compute region - typename ImageType::SizeType size; - typename ImageType::IndexType start; + // Check spacing for(unsigned int i=0; iGetSpacing()[i]) { + clitkExceptionMacro("Images must have the same spacing, but input's spacing(" << i + <<") is " << input->GetSpacing()[i] << " while the spacing(" << i + << ") of 'like' is " << likeSpacing[i] << "."); + } + } + + // Check that we must crop along each dimension. If not, we use the + // size of the input image + for(unsigned int i=0; iGetLargestPossibleRegion().GetIndex()[i]; + likeSize[i] = input->GetLargestPossibleRegion().GetSize()[i]; + } + } + + // Define output region + m_OutputRegion.SetIndex(likeStart); + m_OutputRegion.SetSize(likeSize); + output->SetRegions(m_OutputRegion); + output->SetRequestedRegion(m_OutputRegion); + output->SetBufferedRegion(m_OutputRegion); + output->SetSpacing(likeSpacing); + output->SetOrigin(likeOrigin); + output->Allocate(); // Needed ? + + // get startpoint source/dest + // for each dim + // if source < dest -> start from dest, compute in source + // if source > dest -> start from source, compute in dest + m_StartDestIndex = output->GetLargestPossibleRegion().GetIndex(); + m_StartSourceIndex = input->GetLargestPossibleRegion().GetIndex(); + PointType m_StartPointInSource; + PointType m_StartPointInDest; + m_StartSourceIndex = input->GetLargestPossibleRegion().GetIndex(); + input->TransformIndexToPhysicalPoint(m_StartSourceIndex, m_StartPointInSource); + m_StartDestIndex = output->GetLargestPossibleRegion().GetIndex(); + output->TransformIndexToPhysicalPoint(m_StartDestIndex, m_StartPointInDest); + IndexType startDestInSource; + IndexType startSourceInDest; + input->TransformPhysicalPointToIndex(m_StartPointInDest, startDestInSource); + output->TransformPhysicalPointToIndex(m_StartPointInSource, startSourceInDest); + for(int i=0; iGetLargestPossibleRegion().GetSize()[i]; - ol = input->GetOrigin()[i]; + m_StartDestIndex[i] = startSourceInDest[i]; } - double oi = input->GetOrigin()[i]; - start[i] = lrint((ol-oi)/input->GetSpacing()[i]); - m_Origin[i] = likeOrigin[i]; - if (likeSpacing[i] != input->GetSpacing()[i]) { - clitkExceptionMacro("Images must have the same spacing, but input's spacing(" << i - <<") is " << input->GetSpacing()[i] << " while like's spacing(" << i - << ") is " << likeSpacing[i] << "."); + } + m_Region.SetIndex(m_StartSourceIndex); + + // Stop index + m_StopSourceIndex = input->GetLargestPossibleRegion().GetIndex()+ + input->GetLargestPossibleRegion().GetSize(); + m_StopDestIndex = output->GetLargestPossibleRegion().GetIndex()+ + output->GetLargestPossibleRegion().GetSize(); + PointType m_StopPointInSource; + PointType m_StopPointInDest; + input->TransformIndexToPhysicalPoint(m_StopSourceIndex, m_StopPointInSource); + output->TransformIndexToPhysicalPoint(m_StopDestIndex, m_StopPointInDest); + IndexType stopDestInSource; + IndexType stopSourceInDest; + input->TransformPhysicalPointToIndex(m_StopPointInDest, stopDestInSource); + output->TransformPhysicalPointToIndex(m_StopPointInSource, stopSourceInDest); + + for(int i=0; i m_StopPointInDest[i]) { + m_StopSourceIndex[i] = stopDestInSource[i]; + } + else { + m_StopDestIndex[i] = stopSourceInDest[i]; } } - m_Region.SetSize(size); - m_Region.SetIndex(start); - output->SetRegions(m_Region); - output->SetSpacing(input->GetSpacing()); + // Set size to the region we want to paste + SizeType s; + for(int i=0; i:: GenerateData() { // Get input pointers ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); + + // Get output pointer, fill with Background + ImagePointer output = this->GetOutput(0); + + output->FillBuffer(GetBackgroundValue()); - typedef itk::RegionOfInterestImageFilter CropFilterType; + // Paste image inside + typedef clitk::PasteImageFilter PasteFilterType; + typename PasteFilterType::Pointer pasteFilter = PasteFilterType::New(); + //pasteFilter->ReleaseDataFlagOn(); // change nothing ? + // pasteFilter->InPlaceOn(); // makt it seg fault + pasteFilter->SetSourceImage(input); + pasteFilter->SetDestinationImage(output); + pasteFilter->SetDestinationIndex(m_StartDestIndex); + pasteFilter->SetSourceRegion(m_Region); + pasteFilter->Update(); + + // Get (graft) output (SetNthOutput does not fit here because of Origin). + this->GraftOutput(pasteFilter->GetOutput()); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +typename ImageType::Pointer +clitk::ResizeImageLike(const ImageType * input, + const itk::ImageBase * like, + typename ImageType::PixelType backgroundValue) +{ + typedef clitk::CropLikeImageFilter CropFilterType; typename CropFilterType::Pointer cropFilter = CropFilterType::New(); cropFilter->SetInput(input); - cropFilter->SetReleaseDataFlag(this->GetReleaseDataFlag()); - cropFilter->SetRegionOfInterest(m_Region); + cropFilter->SetCropLikeImage(like); + cropFilter->SetBackgroundValue(backgroundValue); cropFilter->Update(); + return cropFilter->GetOutput(); +} +//-------------------------------------------------------------------- - // Get (graft) output (SetNthOutput does not fit here because of Origin). - this->GraftOutput(cropFilter->GetOutput()); + +//-------------------------------------------------------------------- +template +typename ImageType::Pointer +clitk::ResizeImageLike(const ImageType * input, + typename itk::ImageBase::RegionType * region, + typename ImageType::PixelType backgroundValue) +{ + typename ImageType::Pointer output = ImageType::New(); + output->CopyInformation(input); + typename ImageType::RegionType reg; + reg.SetIndex(region->GetIndex()); + reg.SetSize(region->GetSize()); + output->SetRegions(reg); + output->Allocate(); + return clitk::ResizeImageLike(input, output, backgroundValue); } //-------------------------------------------------------------------- - - -#endif //#define CLITKAUTOCROPFILTER + + +//-------------------------------------------------------------------- +template +typename ImageType::Pointer +clitk::ResizeImageLike(const ImageType * input, + typename itk::BoundingBox::Pointer bb, + typename ImageType::PixelType BG) +{ + typename ImageType::RegionType region; + clitk::ComputeRegionFromBB(input, bb, region); + typename ImageType::Pointer output = ImageType::New(); + output->CopyInformation(input); + output->SetRegions(region); + output->Allocate(); + return clitk::ResizeImageLike(input, output, BG); +} +//-------------------------------------------------------------------- + +#endif //#define CLITKCROPLIKEIMAGEFILTER_TXX