From be468bdd8d0ce316c0bbc71d860c9546c0d01d3b Mon Sep 17 00:00:00 2001 From: dsarrut Date: Wed, 15 Dec 2010 08:48:55 +0000 Subject: [PATCH] resizeImageLike --- itk/clitkSegmentationUtils.h | 71 ++++++++++++---- itk/clitkSegmentationUtils.txx | 151 ++++++++++++++++++++++++++------- 2 files changed, 174 insertions(+), 48 deletions(-) diff --git a/itk/clitkSegmentationUtils.h b/itk/clitkSegmentationUtils.h index 6959883..3b388cb 100644 --- a/itk/clitkSegmentationUtils.h +++ b/itk/clitkSegmentationUtils.h @@ -19,9 +19,12 @@ #ifndef CLITKSEGMENTATIONUTILS_H #define CLITKSEGMENTATIONUTILS_H +// clitk #include "clitkCommon.h" #include "clitkAutoCropFilter.h" #include "clitkLabelizeParameters.h" + +// itk #include namespace clitk { @@ -31,7 +34,7 @@ namespace clitk { void ComputeBBFromImageRegion(typename ImageType::Pointer image, typename ImageType::RegionType region, typename itk::BoundingBox::Pointer bb); + ImageType::ImageDimension>::Pointer bb); //-------------------------------------------------------------------- template @@ -66,7 +69,7 @@ namespace clitk { //-------------------------------------------------------------------- template typename TImageType::Pointer - Labelize(typename TImageType::Pointer input, + Labelize(const TImageType * input, typename TImageType::PixelType BG, bool isFullyConnected, int minimalComponentSize); @@ -84,23 +87,23 @@ namespace clitk { //-------------------------------------------------------------------- template - typename ImageType::Pointer - AutoCrop(typename ImageType::Pointer input, - typename ImageType::PixelType BG) { - typedef clitk::AutoCropFilter AutoCropFilterType; - typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New(); - autoCropFilter->SetInput(input); - autoCropFilter->Update(); - return autoCropFilter->GetOutput(); + typename ImageType::Pointer + AutoCrop(typename ImageType::Pointer input, + typename ImageType::PixelType BG) { + typedef clitk::AutoCropFilter AutoCropFilterType; + typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New(); + autoCropFilter->SetInput(input); + autoCropFilter->SetBackgroundValue(BG); + autoCropFilter->Update(); + return autoCropFilter->GetOutput(); } - //-------------------------------------------------------------------- //-------------------------------------------------------------------- template typename TImageType::Pointer - KeepLabels(typename TImageType::Pointer input, + KeepLabels(const TImageType * input, typename TImageType::PixelType BG, typename TImageType::PixelType FG, typename TImageType::PixelType firstKeep, @@ -122,10 +125,48 @@ namespace clitk { //-------------------------------------------------------------------- template typename ImageType::Pointer - EnlargeImageLike(typename ImageType::Pointer input, - typename ImageType::Pointer like, - typename ImageType::PixelType BG); + ResizeImageLike(typename ImageType::Pointer input, + typename ImageType::Pointer like, + typename ImageType::PixelType BG); + + + //-------------------------------------------------------------------- + template + typename MaskImageType::Pointer + SliceBySliceRelativePosition(const MaskImageType * input, + const MaskImageType * object, + int direction, + double threshold, + std::string orientation, + bool uniqueConnectedComponent=false, + double spacing=-1, + bool notflag=false); + + //-------------------------------------------------------------------- + // In a binary image, search for the point belonging to the FG that + // is the most exterma in the direction 'direction' (or in the + // opposite if notFlag is given). if 'point' and 'distanceMax' are + // given, do not consider points that are away from 'point' more + // than 'distanceMax' + template + typename SliceType::PointType + FindExtremaPointInAGivenDirection(const SliceType * input, + typename SliceType::PixelType bg, + int direction, + bool notFlag, + typename SliceType::PointType point, + double distanceMax); + + //-------------------------------------------------------------------- + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + CropImageAlongOneAxis(typename ImageType::Pointer image, + int dim, double min, double max, + bool autoCrop = false, + typename ImageType::PixelType BG=0); + //-------------------------------------------------------------------- } #include "clitkSegmentationUtils.txx" diff --git a/itk/clitkSegmentationUtils.txx b/itk/clitkSegmentationUtils.txx index aef202f..c70edd5 100644 --- a/itk/clitkSegmentationUtils.txx +++ b/itk/clitkSegmentationUtils.txx @@ -18,6 +18,8 @@ // clitk #include "clitkSetBackgroundImageFilter.h" +#include "clitkSliceBySliceRelativePositionFilter.h" +#include "clitkCropLikeImageFilter.h" // itk #include @@ -30,7 +32,7 @@ template void clitk::ComputeBBFromImageRegion(typename ImageType::Pointer image, typename ImageType::RegionType region, typename itk::BoundingBox::Pointer bb) { + ImageType::ImageDimension>::Pointer bb) { typedef typename ImageType::IndexType IndexType; IndexType firstIndex; IndexType lastIndex; @@ -40,7 +42,7 @@ void clitk::ComputeBBFromImageRegion(typename ImageType::Pointer image, } typedef itk::BoundingBox BBType; + ImageType::ImageDimension> BBType; typedef typename BBType::PointType PointType; PointType lastPoint; PointType firstPoint; @@ -153,7 +155,7 @@ int clitk::GetNumberOfConnectedComponentLabels(typename ImageType::Pointer input //-------------------------------------------------------------------- template typename ImageType::Pointer -clitk::Labelize(typename ImageType::Pointer input, +clitk::Labelize(const ImageType * input, typename ImageType::PixelType BG, bool isFullyConnected, int minimalComponentSize) { @@ -206,7 +208,7 @@ clitk::RemoveLabels(typename ImageType::Pointer input, //-------------------------------------------------------------------- template typename ImageType::Pointer -clitk::KeepLabels(typename ImageType::Pointer input, +clitk::KeepLabels(const ImageType * input, typename ImageType::PixelType BG, typename ImageType::PixelType FG, typename ImageType::PixelType firstKeep, @@ -251,40 +253,123 @@ clitk::LabelizeAndSelectLabels(typename ImageType::Pointer input, //-------------------------------------------------------------------- template typename ImageType::Pointer -clitk::EnlargeImageLike(typename ImageType::Pointer input, - typename ImageType::Pointer like, - typename ImageType::PixelType backgroundValue) +clitk::ResizeImageLike(typename ImageType::Pointer input, + typename ImageType::Pointer like, + typename ImageType::PixelType backgroundValue) { - if (!HaveSameSpacing(input, like)) { - FATAL("Images must have the same spacing"); - } + typedef clitk::CropLikeImageFilter CropFilterType; + typename CropFilterType::Pointer cropFilter = CropFilterType::New(); + cropFilter->SetInput(input); + cropFilter->SetCropLikeImage(like); + cropFilter->SetBackgroundValue(backgroundValue); + cropFilter->Update(); + return cropFilter->GetOutput(); +} +//-------------------------------------------------------------------- - typename ImageType::Pointer output = ImageType::New(); - typename ImageType::SizeType size; - for(unsigned int i=0; iGetLargestPossibleRegion().GetSize()[i]*like->GetSpacing()[i])/ - (double)like->GetSpacing()[i]); + +//-------------------------------------------------------------------- +template +typename MaskImageType::Pointer +clitk::SliceBySliceRelativePosition(const MaskImageType * input, + const MaskImageType * object, + int direction, + double threshold, + std::string orientation, + bool uniqueConnectedComponent, + double spacing, + bool notflag) +{ + typedef clitk::SliceBySliceRelativePositionFilter SliceRelPosFilterType; + typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New(); + sliceRelPosFilter->VerboseStepOff(); + sliceRelPosFilter->WriteStepOff(); + sliceRelPosFilter->SetInput(input); + sliceRelPosFilter->SetInputObject(object); + sliceRelPosFilter->SetDirection(direction); + sliceRelPosFilter->SetFuzzyThreshold(threshold); + sliceRelPosFilter->SetOrientationTypeString(orientation); + sliceRelPosFilter->SetResampleBeforeRelativePositionFilter((spacing != -1)); + sliceRelPosFilter->SetIntermediateSpacing(spacing); + sliceRelPosFilter->SetUniqueConnectedComponentBySlice(uniqueConnectedComponent); + sliceRelPosFilter->SetNotFlag(notflag); + // sliceRelPosFilter->SetAutoCropFlag(true); ?? + sliceRelPosFilter->Update(); + return sliceRelPosFilter->GetOutput(); +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +template +typename SliceType::PointType +clitk::FindExtremaPointInAGivenDirection(const SliceType * input, + typename SliceType::PixelType bg, + int direction, + bool notFlag, + typename SliceType::PointType point, + double distanceMax) +{ + /* + loop over input pixels, store the index in the fg that is max + according to the given direction. + */ + typedef itk::ImageRegionConstIteratorWithIndex IteratorType; + IteratorType iter(input, input->GetLargestPossibleRegion()); + iter.GoToBegin(); + typename SliceType::IndexType max = input->GetLargestPossibleRegion().GetIndex(); + if (notFlag) max = max+input->GetLargestPossibleRegion().GetSize(); + while (!iter.IsAtEnd()) { + if (iter.Get() != bg) { + bool test = iter.GetIndex()[direction] > max[direction]; + if (notFlag) test = !test; + if (test) { + typename SliceType::PointType p; + input->TransformIndexToPhysicalPoint(iter.GetIndex(), p); + if ((distanceMax==0) || (p.EuclideanDistanceTo(point) < distanceMax)) { + max = iter.GetIndex(); + } + } + } + ++iter; } - // DD(size); + typename SliceType::PointType p; + input->TransformIndexToPhysicalPoint(max, p); + return p; +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +template +typename ImageType::Pointer +clitk::CropImageAlongOneAxis(typename ImageType::Pointer image, + int dim, double min, double max, + bool autoCrop, + typename ImageType::PixelType BG) +{ + // Compute region size typename ImageType::RegionType region; + typename ImageType::SizeType size = image->GetLargestPossibleRegion().GetSize(); + typename ImageType::PointType p = image->GetOrigin(); + p[dim] = min; + typename ImageType::IndexType start; + image->TransformPhysicalPointToIndex(p, start); + p[dim] = max; + typename ImageType::IndexType end; + image->TransformPhysicalPointToIndex(p, end); + size[dim] = fabs(end[dim]-start[dim]); + region.SetIndex(start); region.SetSize(size); - output->SetRegions(region); - output->SetSpacing(like->GetSpacing()); - output->SetOrigin(like->GetOrigin()); - output->Allocate(); - output->FillBuffer(backgroundValue); - typedef itk::PasteImageFilter PasteFilterType; - typename PasteFilterType::Pointer pasteFilter = PasteFilterType::New(); - typename PasteFilterType::InputImageIndexType index; - for(unsigned int i=0; iGetOrigin()[i] - like->GetOrigin()[i])/(double)input->GetSpacing()[i]); + // Perform Crop + typedef itk::RegionOfInterestImageFilter CropFilterType; + typename CropFilterType::Pointer cropFilter = CropFilterType::New(); + cropFilter->SetInput(image); + cropFilter->SetRegionOfInterest(region); + cropFilter->Update(); + typename ImageType::Pointer result = cropFilter->GetOutput(); + // Auto Crop + if (autoCrop) { + result = clitk::AutoCrop(result, BG); } - // DD(index); - pasteFilter->SetSourceImage(input); - pasteFilter->SetDestinationImage(output); - pasteFilter->SetDestinationIndex(index); - pasteFilter->SetSourceRegion(input->GetLargestPossibleRegion()); - pasteFilter->Update(); - return pasteFilter->GetOutput(); + return result; } //-------------------------------------------------------------------- -- 2.47.1