]> Creatis software - clitk.git/blob - segmentation/clitkExtractMediastinumFilter.txx
some segmentation tools (most from jef)
[clitk.git] / segmentation / clitkExtractMediastinumFilter.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 #ifndef CLITKEXTRACTMEDIASTINUMFILTER_TXX
20 #define CLITKEXTRACTMEDIASTINUMFILTER_TXX
21
22 // clitk
23 #include "clitkCommon.h"
24 #include "clitkExtractMediastinumFilter.h"
25 #include "clitkAddRelativePositionConstraintToLabelImageFilter.h"
26 #include "clitkSegmentationFunctions.h"
27
28 // itk
29 #include <deque>
30 #include "itkStatisticsLabelMapFilter.h"
31 #include "itkLabelImageToStatisticsLabelMapFilter.h"
32 #include "itkRegionOfInterestImageFilter.h"
33 #include "itkBinaryThresholdImageFilter.h"
34
35 // itk ENST
36 #include "RelativePositionPropImageFilter.h"
37
38 //--------------------------------------------------------------------
39 template <class TImageType>
40 clitk::ExtractMediastinumFilter<TImageType>::
41 ExtractMediastinumFilter():
42   clitk::FilterBase(),
43   itk::ImageToImageFilter<TImageType, TImageType>()
44 {
45   this->SetNumberOfRequiredInputs(3);
46
47   SetBackgroundValuePatient(0);
48   SetBackgroundValueLung(0);
49   SetBackgroundValueBones(0);
50   SetForegroundValueLeftLung(1);
51   SetForegroundValueRightLung(2);
52   SetBackgroundValue(0);
53   SetForegroundValue(1);
54
55   SetIntermediateSpacing(6);
56   SetFuzzyThreshold1(0.6);
57   SetFuzzyThreshold2(0.7);
58 }
59 //--------------------------------------------------------------------
60
61
62 //--------------------------------------------------------------------
63 template <class TImageType>
64 void 
65 clitk::ExtractMediastinumFilter<TImageType>::
66 SetInputPatientLabelImage(const TImageType * image, ImagePixelType bg) {
67   this->SetNthInput(0, const_cast<TImageType *>(image));
68   m_BackgroundValuePatient = bg;
69 }
70 //--------------------------------------------------------------------
71
72
73 //--------------------------------------------------------------------
74 template <class TImageType>
75 void 
76 clitk::ExtractMediastinumFilter<TImageType>::
77 SetInputLungLabelImage(const TImageType * image, ImagePixelType bg, 
78                        ImagePixelType fgLeft, ImagePixelType fgRight) {
79   this->SetNthInput(1, const_cast<TImageType *>(image));
80   m_BackgroundValueLung = bg;
81   m_ForegroundValueLeftLung = fgLeft;
82   m_ForegroundValueRightLung = fgRight;
83 }
84 //--------------------------------------------------------------------
85
86
87 //--------------------------------------------------------------------
88 template <class TImageType>
89 void 
90 clitk::ExtractMediastinumFilter<TImageType>::
91 SetInputBonesLabelImage(const TImageType * image, ImagePixelType bg) {
92   this->SetNthInput(2, const_cast<TImageType *>(image));
93   m_BackgroundValueBones = bg;
94 }
95 //--------------------------------------------------------------------
96
97
98 //--------------------------------------------------------------------
99 template <class TImageType>
100 template<class ArgsInfoType>
101 void 
102 clitk::ExtractMediastinumFilter<TImageType>::
103 SetArgsInfo(ArgsInfoType mArgsInfo)
104 {
105   SetVerboseOption_GGO(mArgsInfo);
106   SetVerboseStep_GGO(mArgsInfo);
107   SetWriteStep_GGO(mArgsInfo);
108   SetVerboseWarningOff_GGO(mArgsInfo);
109
110   SetBackgroundValuePatient_GGO(mArgsInfo);
111   SetBackgroundValueLung_GGO(mArgsInfo);
112   SetBackgroundValueBones_GGO(mArgsInfo);
113
114   SetForegroundValueLeftLung_GGO(mArgsInfo);
115   SetForegroundValueRightLung_GGO(mArgsInfo);
116
117   SetIntermediateSpacing_GGO(mArgsInfo);
118   SetFuzzyThreshold1_GGO(mArgsInfo);
119   SetFuzzyThreshold2_GGO(mArgsInfo);
120 }
121 //--------------------------------------------------------------------
122
123
124 //--------------------------------------------------------------------
125 template <class TImageType>
126 void 
127 clitk::ExtractMediastinumFilter<TImageType>::
128 GenerateOutputInformation() { 
129   ImagePointer input = dynamic_cast<TImageType*>(itk::ProcessObject::GetInput(0));
130   ImagePointer outputImage = this->GetOutput(0);
131   outputImage->SetRegions(outputImage->GetLargestPossibleRegion());
132 }
133 //--------------------------------------------------------------------
134
135
136 //--------------------------------------------------------------------
137 template <class TImageType>
138 void 
139 clitk::ExtractMediastinumFilter<TImageType>::
140 GenerateInputRequestedRegion() {
141   // Call default
142   Superclass::GenerateInputRequestedRegion();  
143   // Get input pointers
144   ImagePointer patient = dynamic_cast<TImageType*>(itk::ProcessObject::GetInput(0));
145   ImagePointer lung    = dynamic_cast<TImageType*>(itk::ProcessObject::GetInput(1));
146   ImagePointer bones   = dynamic_cast<TImageType*>(itk::ProcessObject::GetInput(2));
147     
148   patient->SetRequestedRegion(patient->GetLargestPossibleRegion());
149   lung->SetRequestedRegion(lung->GetLargestPossibleRegion());
150   bones->SetRequestedRegion(bones->GetLargestPossibleRegion());
151 }
152 //--------------------------------------------------------------------
153
154
155 //--------------------------------------------------------------------
156 template <class TImageType>
157 void 
158 clitk::ExtractMediastinumFilter<TImageType>::
159 GenerateData() {
160   // Get input pointers
161   ImageConstPointer patient = dynamic_cast<const TImageType*>(itk::ProcessObject::GetInput(0));
162   ImageConstPointer lung   = dynamic_cast<const TImageType*>(itk::ProcessObject::GetInput(1));
163   ImageConstPointer bones   = dynamic_cast<const TImageType*>(itk::ProcessObject::GetInput(2));
164     
165   // Get output pointer
166   ImagePointer output;
167
168   // Step 1: patient minus lungs, minus bones
169   StartNewStep("Patient contours minus lungs and minus bones");
170   typedef clitk::BooleanOperatorLabelImageFilter<ImageType> BoolFilterType;
171   typename BoolFilterType::Pointer boolFilter = BoolFilterType::New(); 
172   boolFilter->InPlaceOn();
173   boolFilter->SetInput1(patient);
174   boolFilter->SetInput2(lung);    
175   boolFilter->SetOperationType(BoolFilterType::AndNot);
176   boolFilter->Update();    
177   boolFilter->SetInput1(boolFilter->GetOutput());
178   boolFilter->SetInput2(bones);
179   boolFilter->SetOperationType(BoolFilterType::AndNot);
180   boolFilter->Update(); 
181   output = boolFilter->GetOutput();
182
183   output = clitk::AutoCrop<ImageType>(output, GetBackgroundValue()); 
184   ////autoCropFilter->GetOutput();  typedef clitk::AutoCropFilter<ImageType> CropFilterType;
185   //typename CropFilterType::Pointer cropFilter = CropFilterType::New();
186   //cropFilter->SetInput(output);
187   //cropFilter->Update();   
188   //output = cropFilter->GetOutput();
189
190   this->template StopCurrentStep<TImageType>(output);
191
192   // Step 2: LR limits from lung (need separate lung ?)
193   StartNewStep("Left/Right limits with lungs");
194   typedef clitk::AddRelativePositionConstraintToLabelImageFilter<TImageType> RelPosFilterType;
195   typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New();
196   relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
197   relPosFilter->VerboseStepOff();
198   relPosFilter->WriteStepOff();
199   relPosFilter->SetInput(output); 
200   relPosFilter->SetInputObject(lung); 
201   relPosFilter->SetOrientationType(RelPosFilterType::LeftTo);
202   relPosFilter->SetIntermediateSpacing(GetIntermediateSpacing());
203   relPosFilter->SetFuzzyThreshold(GetFuzzyThreshold1());
204   relPosFilter->Update();
205   output = relPosFilter->GetOutput();
206
207   relPosFilter->SetInput(output); 
208   relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
209   relPosFilter->VerboseStepOff();
210   relPosFilter->WriteStepOff();
211   relPosFilter->SetInputObject(lung); 
212   relPosFilter->SetOrientationType(RelPosFilterType::RightTo);
213   relPosFilter->SetIntermediateSpacing(GetIntermediateSpacing());
214   relPosFilter->SetFuzzyThreshold(GetFuzzyThreshold1());
215   relPosFilter->Update();   
216   output = relPosFilter->GetOutput();
217   this->template StopCurrentStep<TImageType>(output);
218
219   // Step 3: AP limits from bones
220   StartNewStep("Ant/Post limits with bones");
221   relPosFilter->SetCurrentStepNumber(0);
222   relPosFilter->ResetPipeline();// = RelPosFilterType::New();
223   relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
224   relPosFilter->VerboseStepOff();
225   relPosFilter->WriteStepOff();
226   relPosFilter->SetInput(output); 
227   relPosFilter->SetInputObject(bones); 
228   relPosFilter->SetOrientationType(RelPosFilterType::AntTo);
229   relPosFilter->SetIntermediateSpacing(GetIntermediateSpacing());
230   relPosFilter->SetFuzzyThreshold(GetFuzzyThreshold2());
231   relPosFilter->Update();
232
233   relPosFilter->SetInput(relPosFilter->GetOutput()); 
234   relPosFilter->SetCurrentStepBaseId(this->GetCurrentStepId());
235   relPosFilter->VerboseStepOff();
236   relPosFilter->WriteStepOff();
237   relPosFilter->SetInputObject(bones); 
238   relPosFilter->SetOrientationType(RelPosFilterType::PostTo);
239   relPosFilter->SetIntermediateSpacing(GetIntermediateSpacing());
240   relPosFilter->SetFuzzyThreshold(GetFuzzyThreshold2());
241   relPosFilter->Update();   
242   output = relPosFilter->GetOutput();
243   this->template StopCurrentStep<TImageType>(output);
244
245   // Get CCL
246   output = clitk::Labelize<TImageType>(output, GetBackgroundValue(), true, 100);
247   // output = RemoveLabels<TImageType>(output, BG, param->GetLabelsToRemove());
248   output = clitk::KeepLabels<TImageType>(output, GetBackgroundValue(), 
249                                   GetForegroundValue(), 1, 1, 0);
250
251   output = clitk::AutoCrop<ImageType>(output, GetBackgroundValue()); 
252   //  cropFilter = CropFilterType::New();
253   //cropFilter->SetInput(output);
254   //cropFilter->Update();   
255   //output = cropFilter->GetOutput();
256
257   // Final Step -> set output
258   this->SetNthOutput(0, output);
259 }
260 //--------------------------------------------------------------------
261   
262
263 #endif //#define CLITKBOOLEANOPERATORLABELIMAGEFILTER_TXX