]> Creatis software - clitk.git/commitdiff
resizeImageLike
authordsarrut <dsarrut>
Wed, 15 Dec 2010 08:48:55 +0000 (08:48 +0000)
committerdsarrut <dsarrut>
Wed, 15 Dec 2010 08:48:55 +0000 (08:48 +0000)
itk/clitkSegmentationUtils.h
itk/clitkSegmentationUtils.txx

index 6959883a98b38e946ffff01928ace77fa29c2652..3b388cba025d1bad9b1a5e40215cab91d6ffb86c 100644 (file)
 #ifndef CLITKSEGMENTATIONUTILS_H
 #define CLITKSEGMENTATIONUTILS_H
 
+// clitk
 #include "clitkCommon.h"
 #include "clitkAutoCropFilter.h"
 #include "clitkLabelizeParameters.h"
+
+// itk
 #include <itkBoundingBox.h>
 
 namespace clitk {
@@ -31,7 +34,7 @@ namespace clitk {
   void ComputeBBFromImageRegion(typename ImageType::Pointer image, 
                                 typename ImageType::RegionType region,
                                 typename itk::BoundingBox<unsigned long, 
-                                ImageType::ImageDimension>::Pointer bb);
+                                                          ImageType::ImageDimension>::Pointer bb);
   
   //--------------------------------------------------------------------
   template<int Dimension>
@@ -66,7 +69,7 @@ namespace clitk {
   //-------------------------------------------------------------------- 
   template<class TImageType>
   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<class ImageType>
-    typename ImageType::Pointer
-    AutoCrop(typename ImageType::Pointer input, 
-            typename ImageType::PixelType BG) {
-      typedef clitk::AutoCropFilter<ImageType> 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<ImageType> AutoCropFilterType;
+    typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New();
+    autoCropFilter->SetInput(input);
+    autoCropFilter->SetBackgroundValue(BG);
+    autoCropFilter->Update();   
+    return autoCropFilter->GetOutput();
   }
-
   //--------------------------------------------------------------------
 
 
   //--------------------------------------------------------------------
   template<class TImageType>
   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<class ImageType>
   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<class MaskImageType>
+  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<class SliceType>
+  typename SliceType::PointType 
+  FindExtremaPointInAGivenDirection(const SliceType * input, 
+                                    typename SliceType::PixelType bg, 
+                                    int direction, 
+                                    bool notFlag, 
+                                    typename SliceType::PointType point,
+                                    double distanceMax);
+
+  //--------------------------------------------------------------------
 
+  //--------------------------------------------------------------------
+  template<class ImageType>
+  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"
index aef202f00ec849a3cb6e3428b59680c339864135..c70edd55ec0cd3f927e52f685ea5b2c4f7ced839 100644 (file)
@@ -18,6 +18,8 @@
 
 // clitk
 #include "clitkSetBackgroundImageFilter.h"
+#include "clitkSliceBySliceRelativePositionFilter.h"
+#include "clitkCropLikeImageFilter.h"
 
 // itk
 #include <itkConnectedComponentImageFilter.h>
@@ -30,7 +32,7 @@ template<class ImageType>
 void clitk::ComputeBBFromImageRegion(typename ImageType::Pointer image, 
                                      typename ImageType::RegionType region,
                                      typename itk::BoundingBox<unsigned long, 
-                                     ImageType::ImageDimension>::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<unsigned long, 
-    ImageType::ImageDimension> 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<class ImageType>
 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<class ImageType>
 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<class ImageType>
 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<ImageType, ImageType>(input, like)) {
-    FATAL("Images must have the same spacing");
-  }
+  typedef clitk::CropLikeImageFilter<ImageType> 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; i<ImageType::ImageDimension; i++) {
-    size[i] = lrint((like->GetLargestPossibleRegion().GetSize()[i]*like->GetSpacing()[i])/
-                    (double)like->GetSpacing()[i]);
+
+//--------------------------------------------------------------------
+template<class MaskImageType>
+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<MaskImageType> 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<class SliceType>
+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<SliceType> 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<class ImageType>
+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<ImageType,ImageType> PasteFilterType;
-  typename PasteFilterType::Pointer pasteFilter = PasteFilterType::New();
-  typename PasteFilterType::InputImageIndexType index;
-  for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
-    index[i] = lrint((input->GetOrigin()[i] - like->GetOrigin()[i])/(double)input->GetSpacing()[i]);
+  // Perform Crop
+  typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> 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<ImageType>(result, BG);
   }
-  // DD(index);
-  pasteFilter->SetSourceImage(input);
-  pasteFilter->SetDestinationImage(output);
-  pasteFilter->SetDestinationIndex(index);
-  pasteFilter->SetSourceRegion(input->GetLargestPossibleRegion());
-  pasteFilter->Update();
-  return pasteFilter->GetOutput();  
+  return result;
 }
 //--------------------------------------------------------------------