]> Creatis software - clitk.git/blobdiff - itk/clitkSliceBySliceRelativePositionFilter.txx
Merge branch 'master' of /home/dsarrut/clitk3.server
[clitk.git] / itk / clitkSliceBySliceRelativePositionFilter.txx
index eafb41608e3e8ae68f1e30b10360d5d96607e830..0548146d2051ffe0b61289dc2d07fbf52d6af95b 100644 (file)
@@ -31,9 +31,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();
 }
 //--------------------------------------------------------------------
 
@@ -66,19 +73,25 @@ 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->GetIntermediateSpacingFlag());
-  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 << "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;    
 }
 //--------------------------------------------------------------------
 
@@ -173,11 +186,49 @@ GenerateOutputInformation()
   // 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 ?)
+    
+    // Count the number of CCL (allow to ignore empty slice)
     int nb=0;
     mObjectSlices[i] = LabelizeAndCountNumberOfObjects<SliceType>(mObjectSlices[i], 0, true, 1, nb);
     if ((!GetIgnoreEmptySliceObjectFlag()) || (nb!=0)) {
-      mObjectSlices[i] = KeepLabels<SliceType>(mObjectSlices[i], 0, 1, 1, 1, true);
+
+      // 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;
@@ -189,10 +240,14 @@ GenerateOutputInformation()
       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));
+        //DD(this->GetOrientationTypeString(j));
       }
-      relPosFilter->SetInverseOrientationFlag(this->GetInverseOrientationFlag());
+      //DD(this->GetInverseOrientationFlag());
       //relPosFilter->SetOrientationType(this->GetOrientationType());
       relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing());
       relPosFilter->SetIntermediateSpacingFlag(this->GetIntermediateSpacingFlag());
@@ -203,11 +258,20 @@ GenerateOutputInformation()
       mInputSlices[i] = relPosFilter->GetOutput();
 
       // Select main CC if needed
-      if (GetUniqueConnectedComponentBySlice()) {
+      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
+        }
+      */
     }
   }