]> Creatis software - clitk.git/commitdiff
some utilities for segmentation
authordsarrut <dsarrut>
Wed, 30 Jun 2010 05:59:17 +0000 (05:59 +0000)
committerdsarrut <dsarrut>
Wed, 30 Jun 2010 05:59:17 +0000 (05:59 +0000)
itk/clitkSegmentationFunctions.h [new file with mode: 0644]
itk/clitkSegmentationFunctions.txx [new file with mode: 0644]
itk/clitkSegmentationUtils.h [new file with mode: 0644]
itk/clitkSegmentationUtils.txx [new file with mode: 0644]

diff --git a/itk/clitkSegmentationFunctions.h b/itk/clitkSegmentationFunctions.h
new file mode 100644 (file)
index 0000000..e39c3e5
--- /dev/null
@@ -0,0 +1,111 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to: 
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+  ======================================================================-====*/
+
+#ifndef CLITKSEGMENTATIONFUNCTIONS_H
+#define CLITKSEGMENTATIONFUNCTIONS_H
+
+#include "clitkAutoCropFilter.h"
+
+//--------------------------------------------------------------------
+namespace clitk {
+
+  //--------------------------------------------------------------------
+  template<class TInternalImageType, class TMaskInternalImageType>
+  typename TInternalImageType::Pointer
+  SetBackground(typename TInternalImageType::ConstPointer input,
+                typename TMaskInternalImageType::ConstPointer mask, 
+                typename TMaskInternalImageType::PixelType maskBG, 
+                typename TInternalImageType::PixelType outValue);
+  //--------------------------------------------------------------------
+
+    
+  //--------------------------------------------------------------------
+  template<class TInternalImageType, class TMaskInternalImageType>
+  typename TInternalImageType::Pointer
+  SetBackground(typename TInternalImageType::Pointer input, 
+                typename TMaskInternalImageType::Pointer mask, 
+                typename TMaskInternalImageType::PixelType maskBG, 
+                typename TInternalImageType::PixelType outValue) {
+    return SetBackground<TInternalImageType, TMaskInternalImageType>
+      (static_cast<typename TInternalImageType::ConstPointer>(input),  
+       static_cast<typename TMaskInternalImageType::ConstPointer>(mask), 
+       maskBG, outValue);
+  }
+  //--------------------------------------------------------------------
+
+
+  //-------------------------------------------------------------------- 
+  template<class TImageType>
+  typename TImageType::Pointer
+  Labelize(typename TImageType::Pointer input, 
+           typename TImageType::PixelType BG, 
+           bool isFullyConnected, 
+           int minimalComponentSize);
+  //--------------------------------------------------------------------
+
+
+  //--------------------------------------------------------------------
+  template<class TImageType>
+  typename TImageType::Pointer
+  RemoveLabels(typename TImageType::Pointer input, 
+               typename TImageType::PixelType BG, 
+               std::vector<typename TImageType::PixelType> & labelsToRemove);
+  //--------------------------------------------------------------------
+
+
+  //--------------------------------------------------------------------
+  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();
+  }
+
+  //--------------------------------------------------------------------
+  //--------------------------------------------------------------------
+  template<class TImageType>
+  typename TImageType::Pointer
+  KeepLabels(typename TImageType::Pointer input,
+             typename TImageType::PixelType BG, 
+             typename TImageType::PixelType FG,  
+             typename TImageType::PixelType firstKeep, 
+             typename TImageType::PixelType lastKeep, 
+             bool useLastKeep);
+  //--------------------------------------------------------------------
+
+
+  //--------------------------------------------------------------------
+  template<class TImageType>
+  typename TImageType::Pointer
+  LabelizeAndSelectLabels(typename TImageType::Pointer input,
+                          typename TImageType::PixelType BG, 
+                          typename TImageType::PixelType FG, 
+                          bool isFullyConnected,
+                          int minimalComponentSize,
+                          LabelizeParameters<typename TImageType::PixelType> * param);
+}
+//--------------------------------------------------------------------
+
+#include "clitkSegmentationFunctions.txx"
+//--------------------------------------------------------------------
+                      
+#endif
diff --git a/itk/clitkSegmentationFunctions.txx b/itk/clitkSegmentationFunctions.txx
new file mode 100644 (file)
index 0000000..6da6b7b
--- /dev/null
@@ -0,0 +1,139 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to: 
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+  ======================================================================-====*/
+
+// clitk
+#include "clitkSetBackgroundImageFilter.h"
+
+// itk
+#include <itkConnectedComponentImageFilter.h>
+#include <itkRelabelComponentImageFilter.h>
+#include <itkBinaryThresholdImageFilter.h>
+
+//--------------------------------------------------------------------
+template<class TImageType, class TMaskImageType>
+typename TImageType::Pointer
+clitk::SetBackground(typename TImageType::ConstPointer input, 
+                     typename TMaskImageType::ConstPointer mask, 
+                     typename TMaskImageType::PixelType maskBG,
+                     typename TImageType::PixelType outValue) {
+  typedef clitk::SetBackgroundImageFilter<TImageType, TMaskImageType, TImageType> SetBackgroundImageFilterType;
+  typename SetBackgroundImageFilterType::Pointer setBackgroundFilter = SetBackgroundImageFilterType::New();
+  setBackgroundFilter->SetInput(input);
+  setBackgroundFilter->SetInput2(mask);
+  setBackgroundFilter->SetMaskValue(maskBG);
+  setBackgroundFilter->SetOutsideValue(outValue);
+  setBackgroundFilter->Update();
+  return setBackgroundFilter->GetOutput();
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class TImageType>
+typename TImageType::Pointer
+clitk::Labelize(typename TImageType::Pointer input, 
+                typename TImageType::PixelType BG, 
+                bool isFullyConnected, 
+                int minimalComponentSize) {
+
+  // Connected Component label 
+  typedef itk::ConnectedComponentImageFilter<TImageType, TImageType> ConnectFilterType;
+  typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
+  connectFilter->SetInput(input);
+  connectFilter->SetBackgroundValue(BG);
+  connectFilter->SetFullyConnected(isFullyConnected);
+  
+  // Sort by size and remove too small area.
+  typedef itk::RelabelComponentImageFilter<TImageType, TImageType> RelabelFilterType;
+  typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
+  relabelFilter->InPlaceOn();
+  relabelFilter->SetInput(connectFilter->GetOutput());
+  relabelFilter->SetMinimumObjectSize(minimalComponentSize);
+  relabelFilter->Update();
+
+  // Return result
+  return relabelFilter->GetOutput();
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class TImageType>
+typename TImageType::Pointer
+clitk::RemoveLabels(typename TImageType::Pointer input, 
+                    typename TImageType::PixelType BG,
+                    std::vector<typename TImageType::PixelType> & labelsToRemove) {
+  typename TImageType::Pointer working_image = input;
+  for (unsigned int i=0; i <labelsToRemove.size(); i++) {
+    typedef clitk::SetBackgroundImageFilter<TImageType, TImageType> SetBackgroundImageFilterType;
+    typename SetBackgroundImageFilterType::Pointer setBackgroundFilter = SetBackgroundImageFilterType::New();
+    setBackgroundFilter->SetInput(input);
+    setBackgroundFilter->SetInput2(input);
+    setBackgroundFilter->SetMaskValue(labelsToRemove[i]);
+    setBackgroundFilter->SetOutsideValue(BG);
+    setBackgroundFilter->Update();
+    working_image = setBackgroundFilter->GetOutput();
+  }
+  return working_image;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class TImageType>
+typename TImageType::Pointer
+clitk::KeepLabels(typename TImageType::Pointer input, 
+                  typename TImageType::PixelType BG, 
+                  typename TImageType::PixelType FG, 
+                  typename TImageType::PixelType firstKeep, 
+                  typename TImageType::PixelType lastKeep, 
+                  bool useLastKeep) {
+  typedef itk::BinaryThresholdImageFilter<TImageType, TImageType> BinarizeFilterType; 
+  typename BinarizeFilterType::Pointer binarizeFilter = BinarizeFilterType::New();
+  binarizeFilter->SetInput(input);
+  binarizeFilter->SetLowerThreshold(firstKeep);
+  if (useLastKeep) binarizeFilter->SetUpperThreshold(lastKeep);
+  binarizeFilter->SetInsideValue(FG);
+  binarizeFilter->SetOutsideValue(BG);
+  binarizeFilter->Update();
+  return binarizeFilter->GetOutput();
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class TImageType>
+typename TImageType::Pointer
+clitk::LabelizeAndSelectLabels(typename TImageType::Pointer input,
+                               typename TImageType::PixelType BG, 
+                               typename TImageType::PixelType FG, 
+                               bool isFullyConnected,
+                               int minimalComponentSize,
+                               LabelizeParameters<typename TImageType::PixelType> * param)
+{
+  typename TImageType::Pointer working_image;
+  working_image = Labelize<TImageType>(input, BG, isFullyConnected, minimalComponentSize);
+  working_image = RemoveLabels<TImageType>(working_image, BG, param->GetLabelsToRemove());
+  working_image = KeepLabels<TImageType>(working_image, 
+                                         BG, FG, 
+                                         param->GetFirstKeep(), 
+                                         param->GetLastKeep(), 
+                                         param->GetUseLastKeep());
+  return working_image;
+}
+//--------------------------------------------------------------------
diff --git a/itk/clitkSegmentationUtils.h b/itk/clitkSegmentationUtils.h
new file mode 100644 (file)
index 0000000..3b7a1e5
--- /dev/null
@@ -0,0 +1,51 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to: 
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+  ======================================================================-====*/
+
+#ifndef CLITKSEGMENTATIONUTILS_H
+#define CLITKSEGMENTATIONUTILS_H
+
+#include "clitkCommon.h"
+#include <itkBoundingBox.h>
+
+namespace clitk {
+
+  //--------------------------------------------------------------------
+  template<class ImageType>
+  void ComputeBBFromImageRegion(typename ImageType::Pointer image, 
+                                typename ImageType::RegionType region,
+                                typename itk::BoundingBox<unsigned long, 
+                                ImageType::ImageDimension>::Pointer bb);
+  
+  //--------------------------------------------------------------------
+  template<int Dimension>
+  void ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
+                             typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
+                             typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2);
+
+  //--------------------------------------------------------------------
+  template<class ImageType>
+  void ComputeRegionFromBB(typename ImageType::Pointer image, 
+                           const typename itk::BoundingBox<unsigned long, 
+                           ImageType::ImageDimension>::Pointer bb, 
+                           typename ImageType::RegionType & region);
+
+}
+
+#include "clitkSegmentationUtils.txx"
+
+#endif
diff --git a/itk/clitkSegmentationUtils.txx b/itk/clitkSegmentationUtils.txx
new file mode 100644 (file)
index 0000000..716e287
--- /dev/null
@@ -0,0 +1,100 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to: 
+  - University of LYON              http://www.universite-lyon.fr/
+  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
+
+  This software is distributed WITHOUT ANY WARRANTY; without even
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - BSD        See included LICENSE.txt file
+  - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+======================================================================-====*/
+
+//--------------------------------------------------------------------
+template<class ImageType>
+void clitk::ComputeBBFromImageRegion(typename ImageType::Pointer image, 
+                              typename ImageType::RegionType region,
+                              typename itk::BoundingBox<unsigned long, 
+                                     ImageType::ImageDimension>::Pointer bb) {
+  typedef typename ImageType::IndexType IndexType;
+  IndexType firstIndex;
+  IndexType lastIndex;
+  for(unsigned int i=0; i<image->GetImageDimension(); i++) {
+    firstIndex[i] = region.GetIndex()[i];
+    lastIndex[i] = region.GetSize()[i];
+  }
+
+  typedef itk::BoundingBox<unsigned long, 
+    ImageType::ImageDimension> BBType;
+  typedef typename BBType::PointType PointType;
+  PointType lastPoint;
+  PointType firstPoint;
+  image->TransformIndexToPhysicalPoint(firstIndex, firstPoint);
+  image->TransformIndexToPhysicalPoint(lastIndex, lastPoint);
+
+  bb->SetMaximum(lastPoint);
+  bb->SetMinimum(firstPoint);
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<int Dimension>
+void clitk::ComputeBBIntersection(typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbo, 
+                           typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi1, 
+                           typename itk::BoundingBox<unsigned long, Dimension>::Pointer bbi2) {
+
+  typedef itk::BoundingBox<unsigned long, Dimension> BBType;
+  typedef typename BBType::PointType PointType;
+  PointType lastPoint;
+  PointType firstPoint;
+
+  for(unsigned int i=0; i<Dimension; i++) {
+    firstPoint[i] = std::max(bbi1->GetMinimum()[i], 
+                             bbi2->GetMinimum()[i]);
+    lastPoint[i] = std::min(bbi1->GetMaximum()[i], 
+                            bbi2->GetMaximum()[i]);
+  }
+
+  bbo->SetMaximum(lastPoint);
+  bbo->SetMinimum(firstPoint);
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class ImageType>
+  void clitk::ComputeRegionFromBB(typename ImageType::Pointer image, 
+                           const typename itk::BoundingBox<unsigned long, 
+                           ImageType::ImageDimension>::Pointer bb, 
+                           typename ImageType::RegionType & region) {
+    // Types
+    typedef typename ImageType::IndexType  IndexType;
+    typedef typename ImageType::PointType  PointType;
+    typedef typename ImageType::RegionType RegionType;
+    typedef typename ImageType::SizeType   SizeType;
+
+    // Region starting point
+    IndexType regionStart;
+    PointType start = bb->GetMinimum();
+    image->TransformPhysicalPointToIndex(start, regionStart);
+    
+    // Region size
+    SizeType regionSize;
+    PointType maxs = bb->GetMaximum();
+    PointType mins = bb->GetMinimum();
+    for(unsigned int i=0; i<ImageType::ImageDimension; i++) {
+      regionSize[i] = floor((maxs[i] - mins[i])/image->GetSpacing()[i]);
+    }
+   
+    // Create region
+    region.SetIndex(regionStart);
+    region.SetSize(regionSize);
+  }
+  //--------------------------------------------------------------------