]> Creatis software - clitk.git/blobdiff - segmentation/clitkExtractLymphStationsFilter.txx
correct options scheme
[clitk.git] / segmentation / clitkExtractLymphStationsFilter.txx
index aab78d85c01730c9778c4be1cf81d99c28726728..ea514a6d643924af6834d3c3a921b82b98411d05 100644 (file)
@@ -34,6 +34,7 @@
 #include <itkRegionOfInterestImageFilter.h>
 #include <itkBinaryThresholdImageFilter.h>
 #include <itkImageSliceConstIteratorWithIndex.h>
+#include <itkImageSliceIteratorWithIndex.h>
 #include <itkBinaryThinningImageFilter.h>
 
 // itk ENST
@@ -44,16 +45,25 @@ template <class TImageType>
 clitk::ExtractLymphStationsFilter<TImageType>::
 ExtractLymphStationsFilter():
   clitk::FilterBase(),
-  itk::ImageToImageFilter<TImageType, TImageType>()
+  clitk::FilterWithAnatomicalFeatureDatabaseManagement(),
+  itk::ImageToImageFilter<TImageType, MaskImageType>()
 {
   this->SetNumberOfRequiredInputs(1);
-  SetBackgroundValueMediastinum(0);
-  SetBackgroundValueTrachea(0);
   SetBackgroundValue(0);
   SetForegroundValue(1);
 
-  SetIntermediateSpacing(6);
-  SetFuzzyThreshold1(0.6);
+  // Station 8
+  SetDistanceMaxToAnteriorPartOfTheSpine(10);
+  MaskImagePointType p;
+  p[0] = 15; p[1] = 2; p[2] = 1;
+  SetEsophagusDiltationForAnt(p);
+  p[0] = 5; p[1] = 10; p[2] = 1;
+  SetEsophagusDiltationForRight(p);
+  SetFuzzyThresholdForS8(0.5);
+
+  // Station 7
+  SetFuzzyThreshold(0.5);
+  SetStation7Filename("station7.mhd");
 }
 //--------------------------------------------------------------------
 
@@ -62,13 +72,54 @@ ExtractLymphStationsFilter():
 template <class TImageType>
 void 
 clitk::ExtractLymphStationsFilter<TImageType>::
-SetInputMediastinumLabelImage(const TImageType * image, ImagePixelType bg) {
-  this->SetNthInput(0, const_cast<TImageType *>(image));
-  m_BackgroundValueMediastinum = bg;
-  SetCarenaZPositionInMM(image->GetOrigin()[2]+image->GetLargestPossibleRegion().GetSize()[2]*image->GetSpacing()[2]);
-  SetMiddleLobeBronchusZPositionInMM(image->GetOrigin()[2]);
-  // DD(m_CarenaZPositionInMM);
-//   DD(m_MiddleLobeBronchusZPositionInMM);
+GenerateOutputInformation() { 
+  // Get inputs
+  LoadAFDB();
+  m_Input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
+  m_Mediastinum = GetAFDB()->template GetImage <MaskImageType>("Mediastinum");
+
+  // Extract Station8
+  StartNewStep("Station 8");
+  StartSubStep(); 
+  ExtractStation_8();
+  StopSubStep();
+
+  // Compute some interesting points in trachea
+  // ( ALTERNATIVE -> SKELETON ANALYSIS ? 
+  //    Pb : not sufficient for mostXX points ... ) 
+
+  /* ==> todo (but why ???)
+     ComputeTracheaCentroidsAboveCarina();
+     ComputeBronchusExtremaPointsBelowCarina();
+  */
+
+  if (0) { // temporary suppress
+    // Extract Station7
+    StartNewStep("Station 7");
+    StartSubStep();
+    ExtractStation_7();
+    StopSubStep();
+
+    // Extract Station4RL
+    StartNewStep("Station 4RL");
+    StartSubStep();
+    //ExtractStation_4RL();
+    StopSubStep();
+  }
+
+
+  //
+  //  typedef clitk::BooleanOperatorLabelImageFilter<MaskImageType> BFilter;
+  //BFilter::Pointer merge = BFilter::New();  
+  // writeImage<MaskImageType>(m_Output, "ouput.mhd");
+  //writeImage<MaskImageType>(m_Working_Support, "ws.mhd");
+  /*merge->SetInput1(m_Station7);
+    merge->SetInput2(m_Station4RL); // support
+    merge->SetOperationType(BFilter::AndNot); CHANGE OPERATOR
+    merge->SetForegroundValue(4);
+    merge->Update();
+    m_Output = merge->GetOutput();
+  */
 }
 //--------------------------------------------------------------------
 
@@ -77,193 +128,42 @@ SetInputMediastinumLabelImage(const TImageType * image, ImagePixelType bg) {
 template <class TImageType>
 void 
 clitk::ExtractLymphStationsFilter<TImageType>::
-SetInputTracheaLabelImage(const TImageType * image, ImagePixelType bg) {
-  this->SetNthInput(1, const_cast<TImageType *>(image));
-  m_BackgroundValueTrachea = bg;
+GenerateInputRequestedRegion() {
+  DD("GenerateInputRequestedRegion (nothing?)");
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
 template <class TImageType>
-template<class ArgsInfoType>
 void 
 clitk::ExtractLymphStationsFilter<TImageType>::
-SetArgsInfo(ArgsInfoType mArgsInfo)
-{
-  SetVerboseOption_GGO(mArgsInfo);
-  SetVerboseStep_GGO(mArgsInfo);
-  SetWriteStep_GGO(mArgsInfo);
-  SetVerboseWarningOff_GGO(mArgsInfo);
-  SetCarenaZPositionInMM_GGO(mArgsInfo);
-  SetMiddleLobeBronchusZPositionInMM_GGO(mArgsInfo);
-  SetIntermediateSpacing_GGO(mArgsInfo);
-  SetFuzzyThreshold1_GGO(mArgsInfo);
-  //SetBackgroundValueMediastinum_GGO(mArgsInfo);
+GenerateData() {
+  DD("GenerateData, graft output");
+
+  // Final Step -> graft output (if SetNthOutput => redo)
+  this->GraftOutput(m_ListOfStations["8"]);
 }
 //--------------------------------------------------------------------
-
+  
 
 //--------------------------------------------------------------------
 template <class TImageType>
 void 
 clitk::ExtractLymphStationsFilter<TImageType>::
-GenerateOutputInformation() { 
-  //  Superclass::GenerateOutputInformation();
-  
-  // Get input
-  m_mediastinum = dynamic_cast<const TImageType*>(itk::ProcessObject::GetInput(0));
-  m_trachea = dynamic_cast<const TImageType*>(itk::ProcessObject::GetInput(1));
-    
-  // ----------------------------------------------------------------
-  // ----------------------------------------------------------------
-  // Superior limit = carena
-  // Inferior limit = origine middle lobe bronchus
-  StartNewStep("Inf/Sup mediastinum limits with carena/bronchus");
-  ImageRegionType region = m_mediastinum->GetLargestPossibleRegion(); DD(region);
-  ImageSizeType size = region.GetSize();
-  ImageIndexType index = region.GetIndex();
-  DD(m_CarenaZPositionInMM);
-  DD(m_MiddleLobeBronchusZPositionInMM);
-  index[2] = floor((m_MiddleLobeBronchusZPositionInMM - m_mediastinum->GetOrigin()[2]) / m_mediastinum->GetSpacing()[2]);
-  size[2] = ceil((m_CarenaZPositionInMM-m_MiddleLobeBronchusZPositionInMM) / m_mediastinum->GetSpacing()[2]);
-  region.SetSize(size);
-  region.SetIndex(index);
-  DD(region);
-  typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
-  typename CropFilterType::Pointer cropFilter = CropFilterType::New();
-  cropFilter->SetInput(m_mediastinum);
-  cropFilter->SetRegionOfInterest(region);
-  cropFilter->Update();
-  m_working_image = cropFilter->GetOutput();
-  // Auto Crop (because following rel pos is faster)
-  m_working_image = clitk::AutoCrop<ImageType>(m_working_image, 0); 
-  StopCurrentStep<ImageType>(m_working_image);
-  m_output = m_working_image;
-
-  // ----------------------------------------------------------------
-  // ----------------------------------------------------------------
-  // Separate trachea in two CCL
-  StartNewStep("Separate trachea under carena");
-  // DD(region);
-  ImageRegionType trachea_region = m_trachea->GetLargestPossibleRegion();
-  for(int i=0; i<3; i++) {
-    index[i] = floor(((index[i]*m_mediastinum->GetSpacing()[i])+m_mediastinum->GetOrigin()[i]
-                      -m_trachea->GetOrigin()[i])/m_trachea->GetSpacing()[i]);
-    // DD(index[i]);
-    size[i] = ceil((size[i]*m_mediastinum->GetSpacing()[i])/m_trachea->GetSpacing()[i]);
-    //  DD(size[i]);
-    if (index[i] < 0) { 
-      size[i] += index[i];
-      index[i] = 0;       
-    }
-    if (size[i]+index[i] > (trachea_region.GetSize()[i] + trachea_region.GetIndex()[i])) {
-      size[i] = trachea_region.GetSize()[i] + trachea_region.GetIndex()[i] - index[i];
-    }
-  }
-  // DD(index);
-  //   DD(size);
-  region.SetIndex(index);
-  region.SetSize(size);  
-  //  typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> CropFilterType;
-  //  typename CropFilterType::Pointer 
-  cropFilter = CropFilterType::New();
- //  m_trachea.Print(std::cout);
-  cropFilter->SetInput(m_trachea);
-  cropFilter->SetRegionOfInterest(region);
-  cropFilter->Update();
-  m_working_trachea = cropFilter->GetOutput();
-
-  // Labelize and consider two main labels
-  m_working_trachea = Labelize<ImageType>(m_working_trachea, 0, true, 1);
-
-  // Detect wich label is at Left
-  typedef itk::ImageSliceConstIteratorWithIndex<ImageType> SliceIteratorType;
-  SliceIteratorType iter(m_working_trachea, m_working_trachea->GetLargestPossibleRegion());
-  iter.SetFirstDirection(0);
-  iter.SetSecondDirection(1);
-  iter.GoToBegin();
-  bool stop = false;
-  ImagePixelType leftLabel;
-  ImagePixelType rightLabel;
-  while (!stop) {
-    if (iter.Get() != m_BackgroundValueTrachea) {
-      //     DD(iter.GetIndex());
-      //       DD((int)iter.Get());
-      leftLabel = iter.Get();
-      stop = true;
-    }
-    ++iter;
-  }
-  if (leftLabel == 1) rightLabel = 2;
-  else rightLabel = 1;
-  DD((int)leftLabel);
-  DD((int)rightLabel);  
+ExtractStation_8() {
 
-  // End step
-  StopCurrentStep<ImageType>(m_working_trachea);
-  
-  //-----------------------------------------------------
-  /*  DD("TEST Skeleton");
-  typedef itk::BinaryThinningImageFilter<ImageType, ImageType> SkeletonFilterType;
-  typename SkeletonFilterType::Pointer skeletonFilter = SkeletonFilterType::New();
-  skeletonFilter->SetInput(m_working_trachea);
-  skeletonFilter->Update();
-  writeImage<ImageType>(skeletonFilter->GetOutput(), "skel.mhd");
-  writeImage<ImageType>(skeletonFilter->GetThinning(), "skel2.mhd");  
-  */
+  // Check if m_ListOfStations["8"] exist. If yes -> use it as initial
+  // support instead of m_Mediastinum
+  if (m_ListOfStations["8"]) {
+    DD("Station 8 support already exist -> use it");
+    m_Working_Support = m_ListOfStations["8"];
+  }
+  else m_Working_Support = m_Mediastinum;
 
-  //-----------------------------------------------------
-  StartNewStep("Left limits with bronchus (slice by slice)");  
-  // Select LeftLabel (set right label to 0)
-  ImagePointer temp = SetBackground<ImageType, ImageType>(m_working_trachea, m_working_trachea, rightLabel, 0);
-  writeImage<ImageType>(temp, "temp1.mhd");
-
-  typedef clitk::SliceBySliceRelativePositionFilter<ImageType> SliceRelPosFilterType;
-  typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
-  sliceRelPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
-  sliceRelPosFilter->VerboseStepOn();
-  sliceRelPosFilter->WriteStepOn();
-  sliceRelPosFilter->SetInput(m_working_image);
-  sliceRelPosFilter->SetInputObject(temp);
-  sliceRelPosFilter->SetDirection(2);
-  sliceRelPosFilter->SetFuzzyThreshold(0.5);
-  sliceRelPosFilter->SetOrientationType(SliceRelPosFilterType::RelPosFilterType::RightTo);
-  sliceRelPosFilter->Update();
-  m_working_image = sliceRelPosFilter->GetOutput();
-  writeImage<ImageType>(m_working_image, "afterslicebyslice.mhd");
-
-
-  //-----------------------------------------------------
-  StartNewStep("Right limits with bronchus (slice by slice)");
-  // Select LeftLabel (set right label to 0)
-  temp = SetBackground<ImageType, ImageType>(m_working_trachea, m_working_trachea, leftLabel, 0);
-  writeImage<ImageType>(temp, "temp2.mhd");
-
-  sliceRelPosFilter = SliceRelPosFilterType::New();
-  sliceRelPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
-  sliceRelPosFilter->VerboseStepOn();
-  sliceRelPosFilter->WriteStepOn();
-  sliceRelPosFilter->SetInput(m_working_image);
-  sliceRelPosFilter->SetInputObject(temp);
-  sliceRelPosFilter->SetDirection(2);
-  sliceRelPosFilter->SetFuzzyThreshold(0.5);
-  sliceRelPosFilter->SetOrientationType(SliceRelPosFilterType::RelPosFilterType::LeftTo);
-  sliceRelPosFilter->Update();
-  m_working_image = sliceRelPosFilter->GetOutput();
-  writeImage<ImageType>(m_working_image, "afterslicebyslice.mhd");
-
-
-  DD("end");
-  m_output = m_working_image;
-  StopCurrentStep<ImageType>(m_output);
-
-  // Set output image information (required)
-  ImagePointer outputImage = this->GetOutput(0);
-  outputImage->SetRegions(m_working_image->GetLargestPossibleRegion());
-  outputImage->SetOrigin(m_working_image->GetOrigin());
-  outputImage->SetRequestedRegion(m_working_image->GetLargestPossibleRegion());
-  DD("end2");
+  ExtractStation_8_SI_Limits();
+  ExtractStation_8_AP_Limits();
+  // ExtractStation_8_LR_Limits();
 }
 //--------------------------------------------------------------------
 
@@ -272,15 +172,15 @@ GenerateOutputInformation() {
 template <class TImageType>
 void 
 clitk::ExtractLymphStationsFilter<TImageType>::
-GenerateInputRequestedRegion() {
-  DD("GenerateInputRequestedRegion");
-  // Call default
-  Superclass::GenerateInputRequestedRegion();
-  // Following needed because output region can be greater than input (trachea)
-  ImagePointer mediastinum = dynamic_cast<TImageType*>(itk::ProcessObject::GetInput(0));
-  ImagePointer trachea = dynamic_cast<TImageType*>(itk::ProcessObject::GetInput(1));
-  mediastinum->SetRequestedRegion(mediastinum->GetLargestPossibleRegion());
-  trachea->SetRequestedRegion(trachea->GetLargestPossibleRegion());
+ExtractStation_7() {
+  if (m_ListOfStations["7"]) {
+    DD("Station 7 support already exist -> use it");
+    m_Working_Support = m_ListOfStations["7"];
+  }
+  else m_Working_Support = m_Mediastinum;
+  ExtractStation_7_SI_Limits();
+  ExtractStation_7_RL_Limits();
+  ExtractStation_7_Posterior_Limits();
 }
 //--------------------------------------------------------------------
 
@@ -289,12 +189,84 @@ GenerateInputRequestedRegion() {
 template <class TImageType>
 void 
 clitk::ExtractLymphStationsFilter<TImageType>::
-GenerateData() {
-  DD("GenerateData");
-  // Final Step -> graft output (if SetNthOutput => redo)
-  this->GraftOutput(m_output);
+ExtractStation_4RL() {
+  /*
+    WARNING ONLY 4R FIRST !!! (not same inf limits)
+  */    
+  ExtractStation_4RL_SI_Limits();
+  ExtractStation_4RL_LR_Limits();
+  ExtractStation_4RL_AP_Limits();
 }
 //--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template <class TImageType>
+void 
+clitk::ExtractLymphStationsFilter<TImageType>::
+FindExtremaPointsInBronchus(MaskImagePointer input, 
+                           int direction,
+                           double distance_max_from_center_point, 
+                           ListOfPointsType & LR, 
+                           ListOfPointsType & Ant, 
+                           ListOfPointsType & Post)
+{
+
+  // Other solution ==> with auto bounding box ! (but pb to prevent to
+  // be too distant from the center point
+
+  // Extract slices
+  std::vector<typename MaskSliceType::Pointer> slices;
+  clitk::ExtractSlices<MaskImageType>(input, 2, slices);
   
+  // Loop on slices
+  bool found;
+  for(uint i=0; i<slices.size(); i++) {
+    /*
+    // Keep main CCL
+    slices[i] = Labelize<MaskSliceType>(slices[i], 0, true, 10);
+    slices[i] = KeepLabels<MaskSliceType>(slices[i], 
+                                         GetBackgroundValue(), 
+                                         GetForegroundValue(), 1, 1, true);
+    */
+
+    // ------- Find rightmost or leftmost point  ------- 
+    MaskSliceType::PointType LRMost;
+    found = 
+      clitk::FindExtremaPointInAGivenDirection<MaskSliceType>(slices[i], 
+                                                              GetBackgroundValue(), 
+                                                              0, // axis XY
+                                                              (direction==0?false:true),  // right or left according to direction
+                                                              LRMost);
+    // ------- Find postmost point  ------- 
+    MaskSliceType::PointType postMost;
+    found = 
+      clitk::FindExtremaPointInAGivenDirection<MaskSliceType>(slices[i], 
+                                                              GetBackgroundValue(), 
+                                                              1, false, LRMost, 
+                                                              distance_max_from_center_point, 
+                                                              postMost);
+    // ------- Find antmost point  ------- 
+    MaskSliceType::PointType antMost;
+    found = 
+      clitk::FindExtremaPointInAGivenDirection<MaskSliceType>(slices[i], 
+                                                              GetBackgroundValue(), 
+                                                              1, true, LRMost, 
+                                                              distance_max_from_center_point, 
+                                                              antMost);
+    // Only add point if found
+    if (found)  {
+      // ------- Convert 2D to 3D points --------
+      MaskImageType::PointType p;
+      clitk::PointsUtils<MaskImageType>::Convert2DTo3D(LRMost, input, i, p);
+      LR.push_back(p); 
+      clitk::PointsUtils<MaskImageType>::Convert2DTo3D(antMost, input, i, p);
+      Ant.push_back(p);
+      clitk::PointsUtils<MaskImageType>::Convert2DTo3D(postMost, input, i, p);
+      Post.push_back(p);
+    }
+  }
+} 
+//--------------------------------------------------------------------
 
 #endif //#define CLITKBOOLEANOPERATORLABELIMAGEFILTER_TXX