]> Creatis software - clitk.git/blob - segmentation/clitkExtractLungFilter.h
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / segmentation / clitkExtractLungFilter.h
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://www.centreleonberard.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 #ifndef CLITKEXTRACTLUNGSFILTER_H
20 #define CLITKEXTRACTLUNGSFILTER_H
21
22 // clitk 
23 #include "clitkFilterBase.h"
24 #include "clitkDecomposeAndReconstructImageFilter.h"
25 #include "clitkExplosionControlledThresholdConnectedImageFilter.h"
26 #include "clitkSegmentationUtils.h"
27 #include "clitkFilterWithAnatomicalFeatureDatabaseManagement.h"
28
29 // itk
30 #include "itkStatisticsImageFilter.h"
31
32 namespace clitk {
33   
34   //--------------------------------------------------------------------
35   /*
36     Try to extract the Lung part of a thorax CT. Inspired by
37     Rikxoort2009, Section IIA, MedPhys.
38
39     - First, all air besides lungs and thrachea is removed, by
40     removing the second largest label of the firstLabelImage and
41     setting the remainder to 0HU . This modified input is optimally
42     thresholded (Otsu1979).
43
44     - Trachea and bronchi are grown from seeds in the top of the image
45     by explosion controlled region growing, slightly dilated and
46     removed from the second label image.
47
48     - Left and right lung are separated (if necessary) by erosion and
49     reconstructed by conditional dilation. 
50
51     - TRACHEA is available at the end
52
53     TODO ********** Remaining holes can be       filled afterwards (clitkFillMask).
54
55   */
56   //--------------------------------------------------------------------
57   
58   //--------------------------------------------------------------------
59   template <class TImageType>
60   class ITK_EXPORT ExtractLungFilter: 
61     public virtual clitk::FilterBase, 
62     public clitk::FilterWithAnatomicalFeatureDatabaseManagement,
63     public itk::ImageToImageFilter<TImageType, itk::Image<uchar, TImageType::ImageDimension> > 
64   {
65     
66   public:
67     /** Standard class typedefs. */
68     typedef itk::Image<uchar, TImageType::ImageDimension> MaskImageType;
69     typedef itk::ImageToImageFilter<TImageType, MaskImageType> Superclass;
70     typedef ExtractLungFilter              Self;
71     typedef itk::SmartPointer<Self>        Pointer;
72     typedef itk::SmartPointer<const Self>  ConstPointer;
73     
74     /** Method for creation through the object factory. */
75     itkNewMacro(Self);  
76     
77     /** Run-time type information (and related methods). */
78     itkTypeMacro(ExtractLungFilter, ImageToImageFilter);
79     FILTERBASE_INIT;
80
81     /** Some convenient typedefs */
82     typedef TImageType                       ImageType;
83     typedef typename ImageType::ConstPointer InputImageConstPointer;
84     typedef typename ImageType::Pointer      InputImagePointer;
85     typedef typename ImageType::RegionType   InputImageRegionType; 
86     typedef typename ImageType::PixelType    InputImagePixelType; 
87     typedef typename ImageType::SizeType     InputImageSizeType; 
88     typedef typename ImageType::IndexType    InputImageIndexType; 
89     typedef typename ImageType::PointType    InputImagePointType; 
90         
91     typedef typename MaskImageType::ConstPointer MaskImageConstPointer;
92     typedef typename MaskImageType::Pointer      MaskImagePointer;
93     typedef typename MaskImageType::RegionType   MaskImageRegionType; 
94     typedef typename MaskImageType::PixelType    MaskImagePixelType; 
95     typedef typename MaskImageType::SizeType     MaskImageSizeType; 
96     typedef typename MaskImageType::IndexType    MaskImageIndexType; 
97     typedef typename MaskImageType::PointType    MaskImagePointType; 
98
99     itkStaticConstMacro(ImageDimension, unsigned int, ImageType::ImageDimension);
100     //    typedef int InternalPixelType;
101     typedef uchar InternalPixelType;
102     typedef itk::Image<InternalPixelType, ImageType::ImageDimension> InternalImageType;
103     typedef typename InternalImageType::Pointer                      InternalImagePointer;
104     typedef typename InternalImageType::IndexType                    InternalIndexType;
105     typedef LabelizeParameters<InternalPixelType>                    LabelParamType;
106     
107     /** Connect inputs */
108     void SetInput(const ImageType * image);
109     itkSetMacro(PatientMaskBackgroundValue, MaskImagePixelType);
110     itkGetConstMacro(PatientMaskBackgroundValue, MaskImagePixelType);
111
112     // Output filename  (for AFBD)
113     itkSetMacro(OutputLungFilename, std::string);
114     itkGetMacro(OutputLungFilename, std::string);
115
116     itkSetMacro(OutputTracheaFilename, std::string);
117     itkGetMacro(OutputTracheaFilename, std::string);
118
119     // Get output (only availabe after update !)
120     typename MaskImageType::Pointer GetTracheaImage() { return trachea; }
121
122     // Background / Foreground
123     itkGetConstMacro(BackgroundValue, MaskImagePixelType);
124     itkGetConstMacro(ForegroundValue, MaskImagePixelType);
125
126     // For common segmentation processes
127     itkSetMacro(MinimalComponentSize, int);
128     itkGetConstMacro(MinimalComponentSize, int);
129
130     // Step 1 options RemoveAir
131     itkSetMacro(UpperThreshold, InputImagePixelType);
132     itkGetConstMacro(UpperThreshold, InputImagePixelType);
133
134     itkSetMacro(NumberOfSlicesToSkipBeforeSearchingSeed, int);
135     itkGetConstMacro(NumberOfSlicesToSkipBeforeSearchingSeed, int);
136     
137     itkSetMacro(LowerThreshold, InputImagePixelType);
138     itkGetConstMacro(LowerThreshold, InputImagePixelType);
139     itkSetMacro(UseLowerThreshold, bool);
140     itkGetConstMacro(UseLowerThreshold, bool);
141     itkBooleanMacro(UseLowerThreshold);
142
143     void SetLabelizeParameters1(LabelParamType * a) { m_LabelizeParameters1 = a; }
144     itkGetConstMacro(LabelizeParameters1, LabelParamType*);
145     
146     itkSetMacro(TracheaSeedAlgorithm, int);
147     itkGetConstMacro(TracheaSeedAlgorithm, int);
148
149     // Step 2 options FindTrachea
150     itkSetMacro(UpperThresholdForTrachea, InputImagePixelType);
151     itkGetConstMacro(UpperThresholdForTrachea, InputImagePixelType);
152
153     itkSetMacro(MultiplierForTrachea, double);
154     itkGetConstMacro(MultiplierForTrachea, double);
155
156     itkSetMacro(ThresholdStepSizeForTrachea, InputImagePixelType);
157     itkGetConstMacro(ThresholdStepSizeForTrachea, InputImagePixelType);
158
159     // options FindTrachea2
160     itkSetMacro(NumSlices, int);
161     itkGetConstMacro(NumSlices, int);
162     itkSetMacro(MaxElongation, double);
163     itkGetConstMacro(MaxElongation, double);
164     itkSetMacro(SeedPreProcessingThreshold, int);
165     itkGetConstMacro(SeedPreProcessingThreshold, int);
166
167     void AddSeed(InternalIndexType s);
168     std::vector<InternalIndexType> & GetSeeds() { return  m_Seeds; }
169
170     itkSetMacro(TracheaVolumeMustBeCheckedFlag, bool);
171     itkGetConstMacro(TracheaVolumeMustBeCheckedFlag, bool);
172     itkBooleanMacro(TracheaVolumeMustBeCheckedFlag);    
173
174     itkSetMacro(VerboseRegionGrowingFlag, bool);
175     itkGetConstMacro(VerboseRegionGrowingFlag, bool);
176     itkBooleanMacro(VerboseRegionGrowingFlag);    
177
178     itkSetMacro(RemoveSmallLabelBeforeSeparationFlag, bool);
179     itkGetConstMacro(RemoveSmallLabelBeforeSeparationFlag, bool);
180     itkBooleanMacro(RemoveSmallLabelBeforeSeparationFlag);    
181
182     // Step 3 options ExtractLung
183     itkSetMacro(NumberOfHistogramBins, int);
184     itkGetConstMacro(NumberOfHistogramBins, int);
185
186     void SetLabelizeParameters2(LabelParamType* a) { m_LabelizeParameters2 = a; }
187     itkGetConstMacro(LabelizeParameters2, LabelParamType*);
188
189     // Step 4 options RemoveTrachea
190     itkSetMacro(RadiusForTrachea, int);
191     itkGetConstMacro(RadiusForTrachea, int);
192     
193     void SetLabelizeParameters3(LabelParamType * a) { m_LabelizeParameters3 = a; }
194     itkGetConstMacro(LabelizeParameters3, LabelParamType*);
195
196     // Step 5 final openclose
197     itkSetMacro(OpenCloseFlag, bool);
198     itkGetConstMacro(OpenCloseFlag, bool);
199     itkBooleanMacro(OpenCloseFlag);
200
201     itkSetMacro(OpenCloseRadius, int);
202     itkGetConstMacro(OpenCloseRadius, int);
203     
204     // Step 6 fill holes
205     itkSetMacro(FillHolesFlag, bool);
206     itkGetConstMacro(FillHolesFlag, bool);
207     itkBooleanMacro(FillHolesFlag);
208
209     // Separate lungs
210     itkSetMacro(SeparateLungsFlag, bool);
211     itkGetConstMacro(SeparateLungsFlag, bool);
212     itkBooleanMacro(SeparateLungsFlag);
213
214     // Step Auto Crop
215     itkSetMacro(AutoCrop, bool);
216     itkGetConstMacro(AutoCrop, bool);
217     itkBooleanMacro(AutoCrop);
218     
219   protected:
220     ExtractLungFilter();
221     virtual ~ExtractLungFilter() {}
222
223     // Main members
224     InputImageConstPointer input;
225     MaskImagePointer patient;
226     InputImagePointer working_input;
227     std::string m_OutputLungFilename;
228     std::string m_OutputTracheaFilename;
229     MaskImagePointer working_mask;  
230     MaskImagePointer trachea;
231     unsigned int m_MaxSeedNumber;
232
233     // Global options
234     itkSetMacro(BackgroundValue, MaskImagePixelType);
235     itkSetMacro(ForegroundValue, MaskImagePixelType);
236     MaskImagePixelType m_PatientMaskBackgroundValue;
237     MaskImagePixelType m_BackgroundValue;
238     MaskImagePixelType m_ForegroundValue;
239     int m_MinimalComponentSize;
240     bool m_AutoCrop;
241     bool m_RemoveSmallLabelBeforeSeparationFlag;
242
243     // Step 1
244     InputImagePixelType m_UpperThreshold;
245     InputImagePixelType m_LowerThreshold;
246     bool m_UseLowerThreshold;
247     LabelParamType* m_LabelizeParameters1;
248
249     // Step 2
250     int m_TracheaSeedAlgorithm;
251     InputImagePixelType m_UpperThresholdForTrachea;
252     InputImagePixelType m_ThresholdStepSizeForTrachea;
253     double m_MultiplierForTrachea;
254     std::vector<InternalIndexType> m_Seeds;
255     int m_NumberOfSlicesToSkipBeforeSearchingSeed;
256     bool m_TracheaVolumeMustBeCheckedFlag;
257     bool m_VerboseRegionGrowingFlag;
258     int m_NumSlices;
259     double m_MaxElongation;
260     int m_SeedPreProcessingThreshold;
261
262     // Step 3
263     int m_NumberOfHistogramBins;
264     LabelParamType* m_LabelizeParameters2;
265
266     // Step 4
267     int m_RadiusForTrachea;
268     LabelParamType* m_LabelizeParameters3;
269
270     // Step 5
271     bool m_OpenCloseFlag;    
272     int m_OpenCloseRadius;
273
274     // Step 6
275     bool m_FillHolesFlag;    
276     InputImageSizeType m_FillHolesDirections;
277
278     bool m_SeparateLungsFlag;
279     
280     // Main functions
281     virtual void GenerateOutputInformation();
282     virtual void GenerateInputRequestedRegion();
283     virtual void GenerateData();
284     
285     // Functions for trachea extraction
286     bool SearchForTracheaSeed(int skip);
287     bool SearchForTracheaSeed2(int numberOfSlices);
288     void SearchForTrachea();
289     void TracheaRegionGrowing();
290     double ComputeTracheaVolume();
291
292   private:
293     ExtractLungFilter(const Self&); //purposely not implemented
294     void operator=(const Self&); //purposely not implemented
295     
296   }; // end class
297   //--------------------------------------------------------------------
298
299 } // end namespace clitk
300 //--------------------------------------------------------------------
301
302 #ifndef ITK_MANUAL_INSTANTIATION
303 #include "clitkExtractLungFilter.txx"
304 #endif
305
306 #endif