]> Creatis software - clitk.git/blob - itk/clitkSegmentationFunctions.txx
reconstruct with dilatation (jef)
[clitk.git] / itk / clitkSegmentationFunctions.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ======================================================================-====*/
18
19 // clitk
20 #include "clitkSetBackgroundImageFilter.h"
21
22 // itk
23 #include <itkConnectedComponentImageFilter.h>
24 #include <itkRelabelComponentImageFilter.h>
25 #include <itkBinaryThresholdImageFilter.h>
26
27 //--------------------------------------------------------------------
28 template<class TImageType, class TMaskImageType>
29 typename TImageType::Pointer
30 clitk::SetBackground(typename TImageType::ConstPointer input, 
31                      typename TMaskImageType::ConstPointer mask, 
32                      typename TMaskImageType::PixelType maskBG,
33                      typename TImageType::PixelType outValue) {
34   typedef clitk::SetBackgroundImageFilter<TImageType, TMaskImageType, TImageType> SetBackgroundImageFilterType;
35   typename SetBackgroundImageFilterType::Pointer setBackgroundFilter = SetBackgroundImageFilterType::New();
36   setBackgroundFilter->SetInput(input);
37   setBackgroundFilter->SetInput2(mask);
38   setBackgroundFilter->SetMaskValue(maskBG);
39   setBackgroundFilter->SetOutsideValue(outValue);
40   setBackgroundFilter->Update();
41   return setBackgroundFilter->GetOutput();
42 }
43 //--------------------------------------------------------------------
44
45
46 //--------------------------------------------------------------------
47 template<class TImageType>
48 typename TImageType::Pointer
49 clitk::Labelize(typename TImageType::Pointer input, 
50                 typename TImageType::PixelType BG, 
51                 bool isFullyConnected, 
52                 int minimalComponentSize) {
53
54   // Connected Component label 
55   typedef itk::ConnectedComponentImageFilter<TImageType, TImageType> ConnectFilterType;
56   typename ConnectFilterType::Pointer connectFilter = ConnectFilterType::New();
57   connectFilter->SetInput(input);
58   connectFilter->SetBackgroundValue(BG);
59   connectFilter->SetFullyConnected(isFullyConnected);
60   
61   // Sort by size and remove too small area.
62   typedef itk::RelabelComponentImageFilter<TImageType, TImageType> RelabelFilterType;
63   typename RelabelFilterType::Pointer relabelFilter = RelabelFilterType::New();
64   relabelFilter->InPlaceOn();
65   relabelFilter->SetInput(connectFilter->GetOutput());
66   relabelFilter->SetMinimumObjectSize(minimalComponentSize);
67   relabelFilter->Update();
68
69   // Return result
70   return relabelFilter->GetOutput();
71 }
72 //--------------------------------------------------------------------
73
74
75 //--------------------------------------------------------------------
76 template<class TImageType>
77 typename TImageType::Pointer
78 clitk::RemoveLabels(typename TImageType::Pointer input, 
79                     typename TImageType::PixelType BG,
80                     std::vector<typename TImageType::PixelType> & labelsToRemove) {
81   typename TImageType::Pointer working_image = input;
82   for (unsigned int i=0; i <labelsToRemove.size(); i++) {
83     typedef clitk::SetBackgroundImageFilter<TImageType, TImageType> SetBackgroundImageFilterType;
84     typename SetBackgroundImageFilterType::Pointer setBackgroundFilter = SetBackgroundImageFilterType::New();
85     setBackgroundFilter->SetInput(input);
86     setBackgroundFilter->SetInput2(input);
87     setBackgroundFilter->SetMaskValue(labelsToRemove[i]);
88     setBackgroundFilter->SetOutsideValue(BG);
89     setBackgroundFilter->Update();
90     working_image = setBackgroundFilter->GetOutput();
91   }
92   return working_image;
93 }
94 //--------------------------------------------------------------------
95
96
97 //--------------------------------------------------------------------
98 template<class TImageType>
99 typename TImageType::Pointer
100 clitk::KeepLabels(typename TImageType::Pointer input, 
101                   typename TImageType::PixelType BG, 
102                   typename TImageType::PixelType FG, 
103                   typename TImageType::PixelType firstKeep, 
104                   typename TImageType::PixelType lastKeep, 
105                   bool useLastKeep) {
106   typedef itk::BinaryThresholdImageFilter<TImageType, TImageType> BinarizeFilterType; 
107   typename BinarizeFilterType::Pointer binarizeFilter = BinarizeFilterType::New();
108   binarizeFilter->SetInput(input);
109   binarizeFilter->SetLowerThreshold(firstKeep);
110   if (useLastKeep) binarizeFilter->SetUpperThreshold(lastKeep);
111   binarizeFilter->SetInsideValue(FG);
112   binarizeFilter->SetOutsideValue(BG);
113   binarizeFilter->Update();
114   return binarizeFilter->GetOutput();
115 }
116 //--------------------------------------------------------------------
117
118
119 //--------------------------------------------------------------------
120 template<class TImageType>
121 typename TImageType::Pointer
122 clitk::LabelizeAndSelectLabels(typename TImageType::Pointer input,
123                                typename TImageType::PixelType BG, 
124                                typename TImageType::PixelType FG, 
125                                bool isFullyConnected,
126                                int minimalComponentSize,
127                                LabelizeParameters<typename TImageType::PixelType> * param)
128 {
129   typename TImageType::Pointer working_image;
130   working_image = Labelize<TImageType>(input, BG, isFullyConnected, minimalComponentSize);
131   working_image = RemoveLabels<TImageType>(working_image, BG, param->GetLabelsToRemove());
132   working_image = KeepLabels<TImageType>(working_image, 
133                                          BG, FG, 
134                                          param->GetFirstKeep(), 
135                                          param->GetLastKeep(), 
136                                          param->GetUseLastKeep());
137   return working_image;
138 }
139 //--------------------------------------------------------------------