X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=itk%2FclitkSegmentationUtils.h;h=5ac8c4c51e86c2d7b5345fcefda25ea8ba8b3ec0;hb=299afce0c4c4cc3524dccecf4ecffd46d7a66328;hp=3b388cba025d1bad9b1a5e40215cab91d6ffb86c;hpb=be468bdd8d0ce316c0bbc71d860c9546c0d01d3b;p=clitk.git diff --git a/itk/clitkSegmentationUtils.h b/itk/clitkSegmentationUtils.h index 3b388cb..5ac8c4c 100644 --- a/itk/clitkSegmentationUtils.h +++ b/itk/clitkSegmentationUtils.h @@ -23,44 +23,35 @@ #include "clitkCommon.h" #include "clitkAutoCropFilter.h" #include "clitkLabelizeParameters.h" +#include "clitkExtractSliceFilter.h" // itk #include +#include +#include -namespace clitk { +/* + According to + http://answerpot.com/showthread.php?357451-Itk::SmartPointer%20-%20problem%20making%20code%20const-correct + it is better to take raw pointer as argument instead of SmartPointer. +*/ - //-------------------------------------------------------------------- - template - void ComputeBBFromImageRegion(typename ImageType::Pointer image, - typename ImageType::RegionType region, - typename itk::BoundingBox::Pointer bb); - - //-------------------------------------------------------------------- - template - void ComputeBBIntersection(typename itk::BoundingBox::Pointer bbo, - typename itk::BoundingBox::Pointer bbi1, - typename itk::BoundingBox::Pointer bbi2); +namespace clitk { //-------------------------------------------------------------------- - template - void ComputeRegionFromBB(typename ImageType::Pointer image, - const typename itk::BoundingBox::Pointer bb, - typename ImageType::RegionType & region); - //-------------------------------------------------------------------- template typename TInternalImageType::Pointer SetBackground(const TInternalImageType * input, const TMaskInternalImageType * mask, typename TMaskInternalImageType::PixelType maskBG, - typename TInternalImageType::PixelType outValue); + typename TInternalImageType::PixelType outValue, + bool inPlace); //-------------------------------------------------------------------- //-------------------------------------------------------------------- template - int GetNumberOfConnectedComponentLabels(typename ImageType::Pointer input, + int GetNumberOfConnectedComponentLabels(const ImageType * input, typename ImageType::PixelType BG, bool isFullyConnected); //-------------------------------------------------------------------- @@ -69,17 +60,22 @@ namespace clitk { //-------------------------------------------------------------------- template typename TImageType::Pointer - Labelize(const TImageType * input, - typename TImageType::PixelType BG, - bool isFullyConnected, - int minimalComponentSize); + Labelize(const TImageType * input, typename TImageType::PixelType BG, + bool isFullyConnected, int minimalComponentSize); + template + typename TImageType::Pointer + LabelizeAndCountNumberOfObjects(const TImageType * input, + typename TImageType::PixelType BG, + bool isFullyConnected, + int minimalComponentSize, + int & nb); //-------------------------------------------------------------------- //-------------------------------------------------------------------- template typename ImageType::Pointer - RemoveLabels(typename ImageType::Pointer input, + RemoveLabels(const ImageType * input, typename ImageType::PixelType BG, std::vector & labelsToRemove); //-------------------------------------------------------------------- @@ -88,12 +84,14 @@ namespace clitk { //-------------------------------------------------------------------- template typename ImageType::Pointer - AutoCrop(typename ImageType::Pointer input, - typename ImageType::PixelType BG) { + AutoCrop(const ImageType * input, + typename ImageType::PixelType BG, + const bool useBorderFlag=false) { typedef clitk::AutoCropFilter AutoCropFilterType; typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New(); autoCropFilter->SetInput(input); autoCropFilter->SetBackgroundValue(BG); + autoCropFilter->SetUseBorder(useBorderFlag); autoCropFilter->Update(); return autoCropFilter->GetOutput(); } @@ -115,20 +113,13 @@ namespace clitk { //-------------------------------------------------------------------- template typename TImageType::Pointer - LabelizeAndSelectLabels(typename TImageType::Pointer input, + LabelizeAndSelectLabels(const TImageType * input, typename TImageType::PixelType BG, typename TImageType::PixelType FG, bool isFullyConnected, int minimalComponentSize, LabelizeParameters * param); - //-------------------------------------------------------------------- - template - typename ImageType::Pointer - ResizeImageLike(typename ImageType::Pointer input, - typename ImageType::Pointer like, - typename ImageType::PixelType BG); - //-------------------------------------------------------------------- template @@ -140,34 +131,381 @@ namespace clitk { std::string orientation, bool uniqueConnectedComponent=false, double spacing=-1, - bool notflag=false); + bool autocropflag=true, + bool singleObjectCCL=true); + template + typename MaskImageType::Pointer + SliceBySliceRelativePosition(const MaskImageType * input, + const MaskImageType * object, + int direction, + double threshold, + double angle, + bool inverseflag, + bool uniqueConnectedComponent=false, + double spacing=-1, + bool autocropflag=true, + bool singleObjectCCL=true); //-------------------------------------------------------------------- // 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); + // opposite if notFlag is given). + template + bool + FindExtremaPointInAGivenDirection(const ImageType * input, + typename ImageType::PixelType bg, + int direction, bool opposite, + typename ImageType::PointType & p); + + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + // Same as above but check that the found point is not more than + // 'distanceMax' away from 'refPoint' + template + bool + FindExtremaPointInAGivenDirection(const ImageType * input, + typename ImageType::PixelType bg, + int direction, bool opposite, + typename ImageType::PointType refPoint, + double distanceMax, + typename ImageType::PointType & p); //-------------------------------------------------------------------- //-------------------------------------------------------------------- template typename ImageType::Pointer - CropImageAlongOneAxis(typename ImageType::Pointer image, + CropImageAlongOneAxis(const ImageType * image, int dim, double min, double max, bool autoCrop = false, typename ImageType::PixelType BG=0); + template + typename ImageType::Pointer + CropImageRemoveGreaterThan(const ImageType * image, + int dim, double min, bool autoCrop = false, + typename ImageType::PixelType BG=0); + template + typename ImageType::Pointer + CropImageRemoveLowerThan(const ImageType * image, + int dim, double max,bool autoCrop = false, + typename ImageType::PixelType BG=0); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename itk::LabelMap< itk::ShapeLabelObject >::Pointer + ComputeLabelMap(const ImageType * image, + typename ImageType::PixelType BG, + bool computePerimeterFlag=false); + template + void + ComputeCentroids(const ImageType * image, + typename ImageType::PixelType BG, + std::vector & centroids); + template + void + ComputeCentroids2(const ImageType * image, + typename ImageType::PixelType BG, + std::vector & centroids); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + ExtractSlices(const ImageType * image, int direction, + std::vector::Pointer > & slices) + { + typedef ExtractSliceFilter ExtractSliceFilterType; + typedef typename ExtractSliceFilterType::SliceType SliceType; + typename ExtractSliceFilterType::Pointer + extractSliceFilter = ExtractSliceFilterType::New(); + extractSliceFilter->SetInput(image); + extractSliceFilter->SetDirection(direction); + extractSliceFilter->Update(); + extractSliceFilter->GetOutputSlices(slices); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + JoinSlices(std::vector::Pointer > & slices, + const itk::ImageBase * input, //const ImageType * input, + int direction) { + typedef typename itk::Image SliceType; + typedef itk::JoinSeriesImageFilter JoinSeriesFilterType; + typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New(); + joinFilter->SetOrigin(input->GetOrigin()[direction]); + joinFilter->SetSpacing(input->GetSpacing()[direction]); + for(unsigned int i=0; iPushBackInput(slices[i]); + } + joinFilter->Update(); + return joinFilter->GetOutput(); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + // Set of tools to manage 3D points and 2D points in slices + template + class PointsUtils + { + typedef typename ImageType::PointType PointType3D; + typedef typename ImageType::IndexType IndexType3D; + typedef typename ImageType::PixelType PixelType; + typedef typename ImageType::Pointer ImagePointer; + typedef typename ImageType::ConstPointer ImageConstPointer; + typedef itk::Image SliceType; + typedef typename SliceType::PointType PointType2D; + typedef typename SliceType::IndexType IndexType2D; + + typedef std::map MapPoint2DType; + typedef std::vector VectorPoint3DType; + typedef std::vector VectorPoint2DType; + + public: + static void Convert2DTo3D(const PointType2D & p2D, + const ImageType * image, + const int slice, + PointType3D & p3D); + static void Convert2DMapTo3DList(const MapPoint2DType & map, + const ImageType * image, + VectorPoint3DType & list); + static void Convert2DListTo3DList(const VectorPoint2DType & p, + int slice, + const ImageType * image, + VectorPoint3DType & list); + }; + + //-------------------------------------------------------------------- + template + void + WriteListOfLandmarks(std::vector points, + std::string filename); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + Dilate(const ImageType * image, double radiusInMM, + typename ImageType::PixelType BG, + typename ImageType::PixelType FG, + bool extendSupport); + template + typename ImageType::Pointer + Dilate(const ImageType * image, typename ImageType::SizeType radius, + typename ImageType::PixelType BG, + typename ImageType::PixelType FG, + bool extendSupport); + template + typename ImageType::Pointer + Dilate(const ImageType * image, typename ImageType::PointType radiusInMM, + typename ImageType::PixelType BG, + typename ImageType::PixelType FG, + bool extendSupport); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + Opening(const ImageType * image, typename ImageType::SizeType radius, + typename ImageType::PixelType BG, typename ImageType::PixelType FG); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void ConvertOption(std::string optionName, uint given, + ValueType * values, VectorType & p, + uint dim, bool required); +#define ConvertOptionMacro(OPTIONNAME, VAR, DIM, REQUIRED) \ + ConvertOption(#OPTIONNAME, OPTIONNAME##_given, OPTIONNAME##_arg, VAR, DIM, REQUIRED); + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + template + void + SliceBySliceSetBackgroundFromLineSeparation(ImageType * input, + std::vector & lA, + std::vector & lB, + typename ImageType::PixelType BG, + int mainDirection, + double offsetToKeep, + bool keepIfEqual=false); + template + void + SliceBySliceSetBackgroundFromLineSeparation_pt(ImageType * input, + std::vector & lA, + std::vector & lB, + typename ImageType::PixelType BG, + typename ImageType::PointType offsetToKeep, + bool keepIfEqual=false); //-------------------------------------------------------------------- -} + + + //-------------------------------------------------------------------- + template + void AndNot(ImageType * input, + const ImageType * object, + typename ImageType::PixelType BG=0); + template + void And(ImageType * input, + const ImageType * object, + typename ImageType::PixelType BG=0); + template + void Or(ImageType * input, + const ImageType * object, + typename ImageType::PixelType BG=0); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + Binarize(const ImageType * input, + typename ImageType::PixelType lower, + typename ImageType::PixelType upper, + typename ImageType::PixelType BG=0, + typename ImageType::PixelType FG=1); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + GetMinMaxPointPosition(const ImageType * input, + typename ImageType::PointType & min, + typename ImageType::PointType & max); + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + template + typename ImageType::PointType + FindExtremaPointInAGivenLine(const ImageType * input, + int dimension, bool inverse, + typename ImageType::PointType p, + typename ImageType::PixelType BG, + double distanceMax); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + bool + IsOnTheSameLineSide(PointType C, PointType A, PointType B, PointType like); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + SliceBySliceBuildLineSegmentAccordingToExtremaPosition(const ImageType * input, + typename ImageType::PixelType BG, + int sliceDimension, + int extremaDirection, + bool extremaOppositeFlag, + int lineDirection, + double margin, + std::vector & A, + std::vector & B); + template + void + SliceBySliceBuildLineSegmentAccordingToMinimalDistanceBetweenStructures(const ImageType * S1, + const ImageType * S2, + typename ImageType::PixelType BG, + int sliceDimension, + std::vector & A, + std::vector & B); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + SliceBySliceKeepMainCCL(const ImageType * input, + typename ImageType::PixelType BG, + typename ImageType::PixelType FG); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + Clone(const ImageType * input); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + SliceBySliceSetBackgroundFromSingleLine(const ImageType * input, + typename ImageType::PixelType BG, + typename ImageType::PointType & A, + typename ImageType::PointType & B, + int dim1, int dim2, bool removeLowerPartFlag); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + SliceBySliceSetBackgroundFromPoints(const ImageType * input, + typename ImageType::PixelType BG, + int sliceDim, + std::vector & A, + bool removeGreaterThanXFlag, + bool removeGreaterThanYFlag); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + FillRegionWithValue(ImageType * input, typename ImageType::PixelType value, + typename ImageType::RegionType & region); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + GetMinMaxBoundary(ImageType * input, typename ImageType::PointType & min, + typename ImageType::PointType & max); + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename itk::Image::Pointer//void + DistanceMap(const ImageType * input, typename ImageType::PixelType BG);//, + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + typename ImageType::PointType + ComputeClosestPoint(const ImageType * input, + const itk::Image * dmap, + typename ImageType::PixelType & BG); + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + template + typename ImageType::Pointer + RemoveNegativeIndexFromRegion(ImageType * input); + //-------------------------------------------------------------------- + + +} // end clitk namespace #include "clitkSegmentationUtils.txx"