]> Creatis software - clitk.git/blobdiff - itk/clitkSliceBySliceRelativePositionFilter.txx
Correct angle units
[clitk.git] / itk / clitkSliceBySliceRelativePositionFilter.txx
index 36564cc945d9666ed672d088aceee39d6cdf59a7..0df2987312797c94f34fb72365359fda82b4043a 100644 (file)
@@ -17,6 +17,7 @@
   ======================================================================-====*/
 
 // clitk
+#include "clitkCropLikeImageFilter.h"
 #include "clitkSegmentationUtils.h"
 #include "clitkExtractSliceFilter.h"
 #include "clitkResampleImageWithOptionsFilter.h"
@@ -31,7 +32,16 @@ SliceBySliceRelativePositionFilter():
   clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>()
 {
   SetDirection(2);
-  UniqueConnectedComponentBySliceOff();
+  UniqueConnectedComponentBySliceFlagOff();
+  SetIgnoreEmptySliceObjectFlag(false);
+  UseTheLargestObjectCCLFlagOff();
+  this->VerboseStepFlagOff();
+  this->WriteStepFlagOff();
+  this->SetCombineWithOrFlag(false);
+  ObjectCCLSelectionFlagOff();
+  SetObjectCCLSelectionDimension(0);
+  SetObjectCCLSelectionDirection(1);
+  ObjectCCLSelectionIgnoreSingleCCLFlagOff();
 }
 //--------------------------------------------------------------------
 
@@ -64,19 +74,28 @@ SetInputObject(const ImageType * image)
 template <class ImageType>
 void 
 clitk::SliceBySliceRelativePositionFilter<ImageType>::
-PrintOptions() 
+PrintOptions(std::ostream & os
 {
-  DD(this->GetDirection());
-  DD((int)this->GetObjectBackgroundValue());
-  DDV(this->GetOrientationTypeString(), (uint)this->GetNumberOfAngles());
-  DD(this->GetResampleBeforeRelativePositionFilter());
-  DD(this->GetIntermediateSpacing());
-  DD(this->GetFuzzyThreshold());
-  DD(this->GetUniqueConnectedComponentBySlice());
-  DD(this->GetAutoCropFlag());
-  DD(this->GetInverseOrientationFlag());
-  DD(this->GetRemoveObjectFlag());
-  DD(this->GetCombineWithOrFlag());
+  os << "Slice direction = " << this->GetDirection() << std::endl
+     << "BG value        = " << this->GetBackgroundValue() << std::endl;
+  for(int i=0; i<this->GetNumberOfAngles(); i++) {
+    os << "Orientation     = " << this->GetOrientationTypeString()[i] << std::endl;
+    os << "Angles     = " << clitk::rad2deg(this->GetAngle1InRad(i)) 
+       << " " << clitk::rad2deg(this->GetAngle2InRad(i)) << std::endl;
+  }
+  os << "InverseOrientationFlag  = " << this->GetInverseOrientationFlag() << std::endl        
+     << "SpacingFlag     = " << this->GetIntermediateSpacingFlag() << std::endl
+     << "Spacing         = " << this->GetIntermediateSpacing() << std::endl
+     << "FuzzyThreshold  = " << this->GetFuzzyThreshold() << std::endl
+     << "UniqueConnectedComponentBySliceFlag  = " << this->GetUniqueConnectedComponentBySliceFlag() << std::endl
+     << "AutoCropFlag    = " << this->GetAutoCropFlag() << std::endl    
+     << "RemoveObjectFlag= " << this->GetRemoveObjectFlag() << std::endl    
+     << "CombineWithOrFlag = " << this->GetCombineWithOrFlag() << std::endl    
+     << "UseTheLargestObjectCCLFlag = " << this->GetUseTheLargestObjectCCLFlag() << std::endl    
+     << "ObjectCCLSelectionFlag = " << this->GetObjectCCLSelectionFlag() << std::endl    
+     << "ObjectCCLSelectionDimension = " << this->GetObjectCCLSelectionDimension() << std::endl    
+     << "ObjectCCLSelectionIgnoreSingleCCLFlag = " << this->GetObjectCCLSelectionIgnoreSingleCCLFlag() << std::endl    
+     << "IgnoreEmptySliceObjectFlag = " << this->GetIgnoreEmptySliceObjectFlag() << std::endl;    
 }
 //--------------------------------------------------------------------
 
@@ -128,8 +147,8 @@ GenerateOutputInformation()
   if (!clitk::HaveSameSizeAndSpacing<ImageType, ImageType>(m_working_object, input)) {
     this->StartNewStep("Pad object to the same size than input");
     m_working_object = clitk::ResizeImageLike<ImageType>(m_working_object, 
-                                                          input, 
-                                                          this->GetObjectBackgroundValue());
+                                                         input, 
+                                                         this->GetObjectBackgroundValue());
     this->template StopCurrentStep<ImageType>(m_working_object);
   }
   else {
@@ -167,43 +186,135 @@ GenerateOutputInformation()
   extractSliceFilter->GetOutputSlices(mObjectSlices);
   this->template StopCurrentStep<SliceType>(mObjectSlices[0]);
 
+  //--------------------------------------------------------------------
+  // Prepare fuzzy slices (if needed)
+  std::vector<typename FloatSliceType::Pointer> mFuzzyMapSlices;
+  mFuzzyMapSlices.resize(mInputSlices.size());
+
   //--------------------------------------------------------------------
   // Perform slice by slice relative position
   this->StartNewStep("Perform slice by slice relative position");
   for(unsigned int i=0; i<mInputSlices.size(); i++) {
-    // Select main CC in each object slice (required ?)
-    mObjectSlices[i] = Labelize<SliceType>(mObjectSlices[i], 0, true, 1);
-    mObjectSlices[i] = KeepLabels<SliceType>(mObjectSlices[i], 0, 1, 1, 1, true);
-
-    // Relative position
-    typedef clitk::AddRelativePositionConstraintToLabelImageFilter<SliceType> RelPosFilterType;
-    typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New();
-    relPosFilter->VerboseStepFlagOff();
-    relPosFilter->WriteStepFlagOff();
-    relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
-    relPosFilter->SetBackgroundValue(this->GetBackgroundValue());
-    relPosFilter->SetInput(mInputSlices[i]); 
-    relPosFilter->SetInputObject(mObjectSlices[i]); 
-    relPosFilter->SetRemoveObjectFlag(this->GetRemoveObjectFlag());
-    for(int j=0; j<this->GetNumberOfAngles(); j++) {
-      relPosFilter->AddOrientationTypeString(this->GetOrientationTypeString(j));
+    
+    // Count the number of CCL (allow to ignore empty slice)
+    int nb=0;
+    mObjectSlices[i] = LabelizeAndCountNumberOfObjects<SliceType>(mObjectSlices[i], 0, true, 1, nb);
+
+    // If no object and empty slices :
+    if ((nb==0) && (this->GetFuzzyMapOnlyFlag())) {
+      typename FloatSliceType::Pointer one = FloatSliceType::New();
+      one->CopyInformation(mObjectSlices[0]);
+      one->SetRegions(mObjectSlices[0]->GetLargestPossibleRegion());
+      one->Allocate();
+      one->FillBuffer(2.0);
+      mFuzzyMapSlices[i] = one;
     }
-    relPosFilter->SetInverseOrientationFlag(this->GetInverseOrientationFlag());
-    //relPosFilter->SetOrientationType(this->GetOrientationType());
-    relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing());
-    relPosFilter->SetResampleBeforeRelativePositionFilter(this->GetResampleBeforeRelativePositionFilter());
-    relPosFilter->SetFuzzyThreshold(this->GetFuzzyThreshold());
-    relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
-    relPosFilter->SetCombineWithOrFlag(this->GetCombineWithOrFlag()); 
-    relPosFilter->Update();
-    mInputSlices[i] = relPosFilter->GetOutput();
-
-    // Select main CC if needed
-    if (GetUniqueConnectedComponentBySlice()) {
-      mInputSlices[i] = Labelize<SliceType>(mInputSlices[i], 0, true, 1);
-      mInputSlices[i] = KeepLabels<SliceType>(mInputSlices[i], 0, 1, 1, 1, true);
+    else {
+      if ((!GetIgnoreEmptySliceObjectFlag()) || (nb!=0)) {
+
+        // Select or not a single CCL ?
+        if (GetUseTheLargestObjectCCLFlag()) {
+          mObjectSlices[i] = KeepLabels<SliceType>(mObjectSlices[i], 0, 1, 1, 1, true);
+        }
+
+        // Select a single according to a position if more than one CCL
+        if (GetObjectCCLSelectionFlag()) {
+          // if several CCL, choose the most extrema according a direction, 
+          // if not -> should we consider this slice ? 
+          if (nb<2) {
+            if (GetObjectCCLSelectionIgnoreSingleCCLFlag()) {
+              mObjectSlices[i] = SetBackground<SliceType, SliceType>(mObjectSlices[i], mObjectSlices[i], 
+                                                                     1, this->GetBackgroundValue(), 
+                                                                     true);
+            }
+          }
+          int dim = GetObjectCCLSelectionDimension();
+          int direction = GetObjectCCLSelectionDirection();
+          std::vector<typename SliceType::PointType> centroids;
+          ComputeCentroids<SliceType>(mObjectSlices[i], this->GetBackgroundValue(), centroids);
+          uint index=1;
+          for(uint j=1; j<centroids.size(); j++) {
+            if (direction == 1) {
+              if (centroids[j][dim] > centroids[index][dim]) index = j;
+            }
+            else {
+              if (centroids[j][dim] < centroids[index][dim]) index = j;
+            }
+          }
+          for(uint v=1; v<centroids.size(); v++) {
+            if (v != index) {
+              mObjectSlices[i] = SetBackground<SliceType, SliceType>(mObjectSlices[i], mObjectSlices[i], 
+                                                                     (char)v, this->GetBackgroundValue(), 
+                                                                     true);
+            }
+          }
+        } // end GetbjectCCLSelectionFlag = true
+
+        // Relative position
+        typedef clitk::AddRelativePositionConstraintToLabelImageFilter<SliceType> RelPosFilterType;
+        typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New();
+
+        relPosFilter->VerboseStepFlagOff();
+        relPosFilter->WriteStepFlagOff();
+        relPosFilter->SetBackgroundValue(this->GetBackgroundValue());
+        relPosFilter->SetInput(mInputSlices[i]); 
+        relPosFilter->SetInputObject(mObjectSlices[i]); 
+        relPosFilter->SetRemoveObjectFlag(this->GetRemoveObjectFlag());
+        // This flag (InverseOrientation) *must* be set before
+        // AddOrientation because AddOrientation can change it.
+        relPosFilter->SetInverseOrientationFlag(this->GetInverseOrientationFlag());
+        for(int j=0; j<this->GetNumberOfAngles(); j++) {
+          //          relPosFilter->AddOrientationTypeString(this->GetOrientationTypeString(j));
+          relPosFilter->AddAnglesInRad(this->GetAngle1InRad(j), this->GetAngle2InRad(j));
+          // DD(this->GetOrientationTypeString(j));
+        }
+        // DD(this->GetInverseOrientationFlag());
+        //relPosFilter->SetOrientationType(this->GetOrientationType());
+        relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing());
+        relPosFilter->SetIntermediateSpacingFlag(this->GetIntermediateSpacingFlag());
+        relPosFilter->SetFuzzyThreshold(this->GetFuzzyThreshold());
+        relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
+        relPosFilter->SetCombineWithOrFlag(this->GetCombineWithOrFlag()); 
+
+        // should we stop after fuzzy map ?
+        relPosFilter->SetFuzzyMapOnlyFlag(this->GetFuzzyMapOnlyFlag());
+      
+        // Go !
+        relPosFilter->Update();
+
+        // If we stop after the fuzzy map, store the fuzzy slices
+        if (this->GetFuzzyMapOnlyFlag()) {
+          mFuzzyMapSlices[i] = relPosFilter->GetFuzzyMap();
+          // writeImage<FloatSliceType>(mFuzzyMapSlices[i], "slice_"+toString(i)+".mha");
+        }
+        else  {
+          mInputSlices[i] = relPosFilter->GetOutput();
+          // Select main CC if needed
+          if (GetUniqueConnectedComponentBySliceFlag()) {
+            mInputSlices[i] = Labelize<SliceType>(mInputSlices[i], 0, true, 1);
+            mInputSlices[i] = KeepLabels<SliceType>(mInputSlices[i], 0, 1, 1, 1, true);
+          }
+        
+        }
+
+      }
+      /*
+      // Select unique CC according to the most in a given direction
+      if (GetUniqueConnectedComponentBySliceAccordingToADirection()) {
+      int nb;
+      mInputSlices[i] = LabelizeAndCountNumberOfObjects<SliceType>(mInputSlices[i], 0, true, 1, nb);
+      std::vector<typename ImageType::PointType> & centroids;
+      ComputeCentroids
+      }
+      */
     }
+  }
 
+  // Join the fuzzy map if needed
+  if (this->GetFuzzyMapOnlyFlag()) {
+    this->m_FuzzyMap = clitk::JoinSlices<FloatImageType>(mFuzzyMapSlices, input, GetDirection());
+    this->template StopCurrentStep<FloatImageType>(this->m_FuzzyMap);
+    return;
   }
 
   // Join the slices
@@ -240,6 +351,7 @@ GenerateData()
   //--------------------------------------------------------------------  
   // Final Step -> set output
   //this->SetNthOutput(0, m_working_input);
+  if (this->GetFuzzyMapOnlyFlag()) return; // no output in this case
   this->GraftOutput(m_working_input);
   return;
 }