]> Creatis software - clitk.git/blobdiff - itk/clitkSliceBySliceRelativePositionFilter.txx
Correct PrintOptions
[clitk.git] / itk / clitkSliceBySliceRelativePositionFilter.txx
index bdffecb71bc295f1b9590e3b046a229634dfc31a..0548146d2051ffe0b61289dc2d07fbf52d6af95b 100644 (file)
 template <class ImageType>
 clitk::SliceBySliceRelativePositionFilter<ImageType>::
 SliceBySliceRelativePositionFilter():
-  clitk::FilterBase(),
-  itk::ImageToImageFilter<ImageType, ImageType>()
+  clitk::AddRelativePositionConstraintToLabelImageFilter<ImageType>()
 {
-  this->SetNumberOfRequiredInputs(2);
   SetDirection(2);
-  SetObjectBackgroundValue(0);  
-  SetFuzzyThreshold(0.6);
-  SetOrientationTypeString("Left");
-  SetIntermediateSpacing(10);
-  ResampleBeforeRelativePositionFilterOff();
-  UniqueConnectedComponentBySliceOff();
-  NotFlagOff();
+  UniqueConnectedComponentBySliceFlagOff();
+  SetIgnoreEmptySliceObjectFlag(false);
+  UseTheLargestObjectCCLFlagOff();
+  this->VerboseStepFlagOff();
+  this->WriteStepFlagOff();
+  this->SetCombineWithOrFlag(false);
+  ObjectCCLSelectionFlagOff();
+  SetObjectCCLSelectionDimension(0);
+  SetObjectCCLSelectionDirection(1);
+  ObjectCCLSelectionIgnoreSingleCCLFlagOff();
 }
 //--------------------------------------------------------------------
 
@@ -68,6 +69,33 @@ SetInputObject(const ImageType * image)
 //--------------------------------------------------------------------
   
 
+//--------------------------------------------------------------------
+template <class ImageType>
+void 
+clitk::SliceBySliceRelativePositionFilter<ImageType>::
+PrintOptions(std::ostream & os) 
+{
+  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;    
+}
+//--------------------------------------------------------------------
+
+
 //--------------------------------------------------------------------
 template <class ImageType>
 void 
@@ -91,6 +119,10 @@ void
 clitk::SliceBySliceRelativePositionFilter<ImageType>::
 GenerateOutputInformation() 
 {
+  if (this->GetVerboseOptionFlag()) {
+    PrintOptions();
+  }
+
   // Get input pointer
   input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
   object = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
@@ -98,9 +130,9 @@ GenerateOutputInformation()
   //--------------------------------------------------------------------
   // Resample object to the same spacing than input
   if (!clitk::HaveSameSpacing<ImageType, ImageType>(object, input)) {
-    StartNewStep("Resample object to the same spacing than input");
+    this->StartNewStep("Resample object to the same spacing than input");
     m_working_object = clitk::ResampleImageSpacing<ImageType>(object, input->GetSpacing());
-    StopCurrentStep<ImageType>(m_working_object);
+    this->template StopCurrentStep<ImageType>(m_working_object);
   }
   else {
     m_working_object = object;
@@ -109,11 +141,11 @@ GenerateOutputInformation()
   //--------------------------------------------------------------------
   // Pad object to the same size than input
   if (!clitk::HaveSameSizeAndSpacing<ImageType, ImageType>(m_working_object, input)) {
-    StartNewStep("Pad object to the same size than input");
+    this->StartNewStep("Pad object to the same size than input");
     m_working_object = clitk::ResizeImageLike<ImageType>(m_working_object, 
-                                                          input, 
-                                                          GetObjectBackgroundValue());
-    StopCurrentStep<ImageType>(m_working_object);
+                                                         input, 
+                                                         this->GetObjectBackgroundValue());
+    this->template StopCurrentStep<ImageType>(m_working_object);
   }
   else {
   }
@@ -128,7 +160,7 @@ GenerateOutputInformation()
 
   //--------------------------------------------------------------------
   // Extract input slices
-  StartNewStep("Extract input slices");
+  this->StartNewStep("Extract input slices");
   typedef clitk::ExtractSliceFilter<ImageType> ExtractSliceFilterType;
   typename ExtractSliceFilterType::Pointer extractSliceFilter = ExtractSliceFilterType::New();
   extractSliceFilter->SetInput(input);
@@ -137,74 +169,127 @@ GenerateOutputInformation()
   typedef typename ExtractSliceFilterType::SliceType SliceType;
   std::vector<typename SliceType::Pointer> mInputSlices;
   extractSliceFilter->GetOutputSlices(mInputSlices);
-  StopCurrentStep<SliceType>(mInputSlices[0]);
+  this->template StopCurrentStep<SliceType>(mInputSlices[0]);
   
   //--------------------------------------------------------------------
   // Extract object slices
-  StartNewStep("Extract object slices");
+  this->StartNewStep("Extract object slices");
   extractSliceFilter = ExtractSliceFilterType::New();
   extractSliceFilter->SetInput(m_working_object);//object);
   extractSliceFilter->SetDirection(GetDirection());
   extractSliceFilter->Update();
   std::vector<typename SliceType::Pointer> mObjectSlices;
   extractSliceFilter->GetOutputSlices(mObjectSlices);
-  StopCurrentStep<SliceType>(mObjectSlices[0]);
+  this->template StopCurrentStep<SliceType>(mObjectSlices[0]);
 
   //--------------------------------------------------------------------
   // Perform slice by slice relative position
-  StartNewStep("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->VerboseStepOff();
-    relPosFilter->WriteStepOff();
-    relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
-    relPosFilter->SetInput(mInputSlices[i]); 
-    relPosFilter->SetInputObject(mObjectSlices[i]); 
-    relPosFilter->SetNotFlag(GetNotFlag());
-    relPosFilter->SetOrientationTypeString(this->GetOrientationTypeString());
-    relPosFilter->SetIntermediateSpacing(this->GetIntermediateSpacing());
-    relPosFilter->SetResampleBeforeRelativePositionFilter(this->GetResampleBeforeRelativePositionFilter());
-    relPosFilter->SetFuzzyThreshold(this->GetFuzzyThreshold());
-    relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
-    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);
-    }
+    
+    // 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)) {
 
-  }
+      // Select or not a single CCL ?
+      if (GetUseTheLargestObjectCCLFlag()) {
+        mObjectSlices[i] = KeepLabels<SliceType>(mObjectSlices[i], 0, 1, 1, 1, true);
+      }
 
-  typedef itk::JoinSeriesImageFilter<SliceType, ImageType> JoinSeriesFilterType;
-  typename JoinSeriesFilterType::Pointer joinFilter = JoinSeriesFilterType::New();
-  joinFilter->SetOrigin(input->GetOrigin()[GetDirection()]);
-  joinFilter->SetSpacing(input->GetSpacing()[GetDirection()]);
-  for(unsigned int i=0; i<mInputSlices.size(); i++) {
-    joinFilter->PushBackInput(mInputSlices[i]);
+      // 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));
+        //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()); 
+      relPosFilter->Update();
+      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
+        }
+      */
+    }
   }
-  joinFilter->Update();
-  m_working_input = joinFilter->GetOutput();
-  StopCurrentStep<ImageType>(m_working_input);
+
+  // Join the slices
+  m_working_input = clitk::JoinSlices<ImageType>(mInputSlices, input, GetDirection());
+  this->template StopCurrentStep<ImageType>(m_working_input);
 
   //--------------------------------------------------------------------
   // Step 7: autocrop
-  if (GetAutoCropFlag()) {
-    StartNewStep("Final AutoCrop");
+  if (this->GetAutoCropFlag()) {
+    this->StartNewStep("Final AutoCrop");
     typedef clitk::AutoCropFilter<ImageType> CropFilterType;
     typename CropFilterType::Pointer cropFilter = CropFilterType::New();
     cropFilter->SetInput(m_working_input);
     cropFilter->ReleaseDataFlagOff();
     cropFilter->Update();   
     m_working_input = cropFilter->GetOutput();
-    StopCurrentStep<ImageType>(m_working_input);    
+    this->template StopCurrentStep<ImageType>(m_working_input);    
   }
 
   // Update output info