]> Creatis software - clitk.git/blob - segmentation/clitkExtractLymphStationsFilter.txx
aec05d782df8553d4a0bdd861f7e5d40fdf8eedb
[clitk.git] / segmentation / clitkExtractLymphStationsFilter.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 CLITKEXTRACTLYMPHSTATIONSFILTER_TXX
20 #define CLITKEXTRACTLYMPHSTATIONSFILTER_TXX
21
22 // clitk
23 #include "clitkCommon.h"
24 #include "clitkExtractLymphStationsFilter.h"
25 #include "clitkAddRelativePositionConstraintToLabelImageFilter.h"
26 #include "clitkSegmentationUtils.h"
27 #include "clitkAutoCropFilter.h"
28 #include "clitkSegmentationUtils.h"
29 #include "clitkSliceBySliceRelativePositionFilter.h"
30
31 // itk
32 #include <itkStatisticsLabelMapFilter.h>
33 #include <itkLabelImageToStatisticsLabelMapFilter.h>
34 #include <itkRegionOfInterestImageFilter.h>
35 #include <itkBinaryThresholdImageFilter.h>
36 #include <itkImageSliceConstIteratorWithIndex.h>
37 #include <itkImageSliceIteratorWithIndex.h>
38 #include <itkBinaryThinningImageFilter.h>
39
40 // itk ENST
41 #include "RelativePositionPropImageFilter.h"
42
43 //--------------------------------------------------------------------
44 template <class TImageType>
45 clitk::ExtractLymphStationsFilter<TImageType>::
46 ExtractLymphStationsFilter():
47   clitk::FilterBase(),
48   clitk::FilterWithAnatomicalFeatureDatabaseManagement(),
49   itk::ImageToImageFilter<TImageType, MaskImageType>()
50 {
51   this->SetNumberOfRequiredInputs(1);
52   SetBackgroundValue(0);
53   SetForegroundValue(1);
54
55   // Default values
56   ExtractStation_8_SetDefaultValues();
57   ExtractStation_3P_SetDefaultValues();
58   ExtractStation_3A_SetDefaultValues();
59   ExtractStation_7_SetDefaultValues();
60 }
61 //--------------------------------------------------------------------
62
63
64 //--------------------------------------------------------------------
65 template <class TImageType>
66 void 
67 clitk::ExtractLymphStationsFilter<TImageType>::
68 GenerateOutputInformation() { 
69   // Get inputs
70   LoadAFDB();
71   m_Input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
72   m_Mediastinum = GetAFDB()->template GetImage <MaskImageType>("Mediastinum");
73
74   // Extract Station8
75   StartNewStep("Station 8");
76   StartSubStep(); 
77   ExtractStation_8();
78   StopSubStep();
79
80   // Extract Station3P
81   StartNewStep("Station 3P");
82   StartSubStep(); 
83   ExtractStation_3P();
84   StopSubStep();
85
86   // Extract Station3A
87   StartNewStep("Station 3A");
88   StartSubStep(); 
89   ExtractStation_3A();
90   StopSubStep();
91
92   // Extract Station7
93   StartNewStep("Station 7");
94   StartSubStep();
95   ExtractStation_7();
96   StopSubStep();
97
98   if (0) { // temporary suppress
99     // Extract Station4RL
100     StartNewStep("Station 4RL");
101     StartSubStep();
102     //ExtractStation_4RL();
103     StopSubStep();
104   }
105
106
107   //
108   //  typedef clitk::BooleanOperatorLabelImageFilter<MaskImageType> BFilter;
109   //BFilter::Pointer merge = BFilter::New();  
110   // writeImage<MaskImageType>(m_Output, "ouput.mhd");
111   //writeImage<MaskImageType>(m_Working_Support, "ws.mhd");
112   /*merge->SetInput1(m_Station7);
113     merge->SetInput2(m_Station4RL); // support
114     merge->SetOperationType(BFilter::AndNot); CHANGE OPERATOR
115     merge->SetForegroundValue(4);
116     merge->Update();
117     m_Output = merge->GetOutput();
118   */
119 }
120 //--------------------------------------------------------------------
121
122
123 //--------------------------------------------------------------------
124 template <class TImageType>
125 void 
126 clitk::ExtractLymphStationsFilter<TImageType>::
127 GenerateInputRequestedRegion() {
128   //DD("GenerateInputRequestedRegion (nothing?)");
129 }
130 //--------------------------------------------------------------------
131
132
133 //--------------------------------------------------------------------
134 template <class TImageType>
135 void 
136 clitk::ExtractLymphStationsFilter<TImageType>::
137 GenerateData() {
138   // Final Step -> graft output (if SetNthOutput => redo)
139   this->GraftOutput(m_ListOfStations["8"]);
140 }
141 //--------------------------------------------------------------------
142   
143
144 //--------------------------------------------------------------------
145 template <class TImageType>
146 bool 
147 clitk::ExtractLymphStationsFilter<TImageType>::
148 CheckForStation(std::string station) 
149 {
150   // Compute Station name
151   std::string s = "Station"+station;
152   
153
154   // Check if station already exist in DB
155   bool found = false;
156   if (GetAFDB()->TagExist(s)) {
157     m_ListOfStations[station] = GetAFDB()->template GetImage<MaskImageType>(s);
158     found = true;
159   }
160
161   // Define the starting support 
162   if (found && GetComputeStation(station)) {
163     std::cout << "Station " << station << " already exists, but re-computation forced." << std::endl;
164   }
165   if (!found || GetComputeStation(station)) {
166     m_Working_Support = m_Mediastinum = GetAFDB()->template GetImage<MaskImageType>("Mediastinum", true);
167     return true;
168   }
169   else {
170     std::cout << "Station " << station << " found. I used it" << std::endl;
171     return false;
172   }
173 }
174 //--------------------------------------------------------------------
175
176
177 //--------------------------------------------------------------------
178 template <class TImageType>
179 bool
180 clitk::ExtractLymphStationsFilter<TImageType>::
181 GetComputeStation(std::string station) 
182 {
183   return (m_ComputeStationMap.find(station) != m_ComputeStationMap.end());
184 }
185 //--------------------------------------------------------------------
186
187
188 //--------------------------------------------------------------------
189 template <class TImageType>
190 void
191 clitk::ExtractLymphStationsFilter<TImageType>::
192 AddComputeStation(std::string station) 
193 {
194   m_ComputeStationMap[station] = true;
195 }
196 //--------------------------------------------------------------------
197
198
199 //--------------------------------------------------------------------
200 template <class TImageType>
201 void 
202 clitk::ExtractLymphStationsFilter<TImageType>::
203 ExtractStation_8() 
204 {
205   if (CheckForStation("8")) {
206     ExtractStation_8_SI_Limits();
207     ExtractStation_8_Post_Limits();
208     ExtractStation_8_Ant_Sup_Limits();
209     ExtractStation_8_Ant_Inf_Limits();
210     ExtractStation_8_LR_1_Limits();
211     ExtractStation_8_LR_2_Limits();
212     ExtractStation_8_LR_Limits();
213     ExtractStation_8_Ant_Injected_Limits();
214     ExtractStation_8_Single_CCL_Limits();
215     ExtractStation_8_Remove_Structures();
216     // Store image filenames into AFDB 
217     writeImage<MaskImageType>(m_ListOfStations["8"], "seg/Station8.mhd");
218     GetAFDB()->SetImageFilename("Station8", "seg/Station8.mhd");  
219     WriteAFDB();
220   }
221 }
222 //--------------------------------------------------------------------
223
224
225 //--------------------------------------------------------------------
226 template <class TImageType>
227 void 
228 clitk::ExtractLymphStationsFilter<TImageType>::
229 ExtractStation_3P()
230 {
231   if (CheckForStation("3P")) {
232     ExtractStation_3P_SI_Limits();
233     ExtractStation_3P_Ant_Limits();
234     ExtractStation_3P_Post_Limits();
235     ExtractStation_3P_LR_sup_Limits();
236     //    ExtractStation_3P_LR_sup_Limits_2();
237     ExtractStation_3P_LR_inf_Limits();
238     ExtractStation_8_Single_CCL_Limits(); // YES 8 !
239     ExtractStation_3P_Remove_Structures(); // after CCL
240     // Store image filenames into AFDB 
241     writeImage<MaskImageType>(m_ListOfStations["3P"], "seg/Station3P.mhd");
242     GetAFDB()->SetImageFilename("Station3P", "seg/Station3P.mhd"); 
243     WriteAFDB(); 
244   }
245 }
246 //--------------------------------------------------------------------
247
248
249 //--------------------------------------------------------------------
250 template <class TImageType>
251 void 
252 clitk::ExtractLymphStationsFilter<TImageType>::
253 ExtractStation_7() {
254   if (CheckForStation("7")) {
255   ExtractStation_7_SI_Limits();
256   ExtractStation_7_RL_Limits();
257   ExtractStation_7_Posterior_Limits();
258   //  ExtractStation_8_Single_CCL_Limits(); // Yes the same than 8
259   ExtractStation_7_Remove_Structures();
260   // Store image filenames into AFDB 
261   writeImage<MaskImageType>(m_ListOfStations["7"], "seg/Station7.mhd");
262   GetAFDB()->SetImageFilename("Station7", "seg/Station7.mhd");  
263   WriteAFDB();
264   }
265 }
266 //--------------------------------------------------------------------
267
268
269 //--------------------------------------------------------------------
270 template <class TImageType>
271 void 
272 clitk::ExtractLymphStationsFilter<TImageType>::
273 ExtractStation_3A()
274 {
275   if (CheckForStation("3A")) {
276     ExtractStation_3A_SI_Limits();
277     ExtractStation_3A_Ant_Limits();
278     // Store image filenames into AFDB 
279     writeImage<MaskImageType>(m_ListOfStations["3A"], "seg/Station3A.mhd");
280     GetAFDB()->SetImageFilename("Station3A", "seg/Station3A.mhd"); 
281     WriteAFDB(); 
282   }
283 }
284 //--------------------------------------------------------------------
285
286
287 //--------------------------------------------------------------------
288 template <class TImageType>
289 void 
290 clitk::ExtractLymphStationsFilter<TImageType>::
291 ExtractStation_4RL() {
292   DD("TODO");
293   exit(0);
294   /*
295     WARNING ONLY 4R FIRST !!! (not same inf limits)
296   */    
297   ExtractStation_4RL_SI_Limits();
298   ExtractStation_4RL_LR_Limits();
299   ExtractStation_4RL_AP_Limits();
300 }
301 //--------------------------------------------------------------------
302
303
304 //--------------------------------------------------------------------
305 template <class ImageType>
306 void 
307 clitk::ExtractLymphStationsFilter<ImageType>::
308 Remove_Structures(std::string s)
309 {
310   try {
311     StartNewStep("[Station7] Remove "+s);  
312     MaskImagePointer Structure = GetAFDB()->template GetImage<MaskImageType>(s);
313     clitk::AndNot<MaskImageType>(m_Working_Support, Structure, GetBackgroundValue());
314   }
315   catch(clitk::ExceptionObject e) {
316     std::cout << s << " not found, skip." << std::endl;
317   }
318 }
319 //--------------------------------------------------------------------
320
321
322
323 #endif //#define CLITKBOOLEANOPERATORLABELIMAGEFILTER_TXX