X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=itk%2FclitkCropLikeImageFilter.txx;h=6399ec6ac7e5419ca870e6e7cd61bf6b072810ef;hb=02f230e65dbfde06e84da374bb085643e29b5090;hp=8b90fe30e704a1b1b61454608b01c477ceda89d6;hpb=709d3deddba827afcff948a7ebfe8d4908e72f8f;p=clitk.git diff --git a/itk/clitkCropLikeImageFilter.txx b/itk/clitkCropLikeImageFilter.txx index 8b90fe3..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,16 +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 "itkPasteImageFilter.h" +#include "clitkPasteImageFilter.h" //-------------------------------------------------------------------- template @@ -32,7 +30,7 @@ CropLikeImageFilter():itk::ImageToImageFilter() { this->SetNumberOfRequiredInputs(1); m_LikeImage = NULL; m_LikeFilenameIsGiven = false; - this->SetBackgroundValue(0); + this->SetBackgroundValue(typename PixelTraits::ValueType(0)); m_CropAlongThisDimension.resize(ImageType::ImageDimension); for(uint i=0; i void clitk::CropLikeImageFilter:: -GenerateOutputInformation() { - DD("GenerateOutputInformation"); +GenerateOutputInformation() { // Get input pointers ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); @@ -111,6 +108,7 @@ GenerateOutputInformation() { 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) @@ -132,10 +130,20 @@ GenerateOutputInformation() { for(unsigned int i=0; iGetSpacing()[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] << "."); + <<") 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); @@ -144,6 +152,7 @@ GenerateOutputInformation() { output->SetBufferedRegion(m_OutputRegion); output->SetSpacing(likeSpacing); output->SetOrigin(likeOrigin); + output->Allocate(); // Needed ? // get startpoint source/dest // for each dim @@ -199,6 +208,7 @@ GenerateOutputInformation() { for(int i=0; iGetOutput(0); - output->Allocate(); + output->FillBuffer(GetBackgroundValue()); // Paste image inside - typedef itk::PasteImageFilter PasteFilterType; + 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); @@ -225,10 +237,63 @@ GenerateData() { pasteFilter->Update(); // Get (graft) output (SetNthOutput does not fit here because of Origin). - // this->GraftOutput(cropFilter->GetOutput()); this->GraftOutput(pasteFilter->GetOutput()); } //-------------------------------------------------------------------- - -#endif //#define CLITKAUTOCROPFILTER + +//-------------------------------------------------------------------- +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->SetCropLikeImage(like); + cropFilter->SetBackgroundValue(backgroundValue); + cropFilter->Update(); + return 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); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +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