]> Creatis software - clitk.git/blob - segmentation/clitkExtractLymphStationsFilter.txx
Merge branch 'master' of /home/romulo/creatis/clitk3-git-shared/clitk3
[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_2RL_SetDefaultValues();
59   ExtractStation_3A_SetDefaultValues();
60   ExtractStation_7_SetDefaultValues();
61 }
62 //--------------------------------------------------------------------
63
64
65 //--------------------------------------------------------------------
66 template <class TImageType>
67 void 
68 clitk::ExtractLymphStationsFilter<TImageType>::
69 GenerateOutputInformation() { 
70   // Get inputs
71   LoadAFDB();
72   m_Input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
73   m_Mediastinum = GetAFDB()->template GetImage <MaskImageType>("Mediastinum");
74
75   // Extract Station8
76   StartNewStep("Station 8");
77   StartSubStep(); 
78   ExtractStation_8();
79   StopSubStep();
80
81   // Extract Station3P
82   StartNewStep("Station 3P");
83   StartSubStep(); 
84   ExtractStation_3P();
85   StopSubStep();
86
87   // Extract Station2RL
88   StartNewStep("Station 2RL");
89   StartSubStep(); 
90   ExtractStation_2RL();
91   StopSubStep();
92
93   // Extract Station3A
94   StartNewStep("Station 3A");
95   StartSubStep(); 
96   ExtractStation_3A();
97   StopSubStep();
98
99   // Extract Station7
100   StartNewStep("Station 7");
101   StartSubStep();
102   ExtractStation_7();
103   StopSubStep();
104
105   if (0) { // temporary suppress
106     // Extract Station4RL
107     StartNewStep("Station 4RL");
108     StartSubStep();
109     //ExtractStation_4RL();
110     StopSubStep();
111   }
112 }
113 //--------------------------------------------------------------------
114
115
116 //--------------------------------------------------------------------
117 template <class TImageType>
118 void 
119 clitk::ExtractLymphStationsFilter<TImageType>::
120 GenerateInputRequestedRegion() {
121   //DD("GenerateInputRequestedRegion (nothing?)");
122 }
123 //--------------------------------------------------------------------
124
125
126 //--------------------------------------------------------------------
127 template <class TImageType>
128 void 
129 clitk::ExtractLymphStationsFilter<TImageType>::
130 GenerateData() {
131   // Final Step -> graft output (if SetNthOutput => redo)
132   this->GraftOutput(m_ListOfStations["8"]);
133 }
134 //--------------------------------------------------------------------
135   
136
137 //--------------------------------------------------------------------
138 template <class TImageType>
139 bool 
140 clitk::ExtractLymphStationsFilter<TImageType>::
141 CheckForStation(std::string station) 
142 {
143   // Compute Station name
144   std::string s = "Station"+station;
145   
146
147   // Check if station already exist in DB
148   bool found = false;
149   if (GetAFDB()->TagExist(s)) {
150     m_ListOfStations[station] = GetAFDB()->template GetImage<MaskImageType>(s);
151     found = true;
152   }
153
154   // Define the starting support 
155   if (found && GetComputeStation(station)) {
156     std::cout << "Station " << station << " already exists, but re-computation forced." << std::endl;
157   }
158   if (!found || GetComputeStation(station)) {
159     m_Working_Support = m_Mediastinum = GetAFDB()->template GetImage<MaskImageType>("Mediastinum", true);
160     return true;
161   }
162   else {
163     std::cout << "Station " << station << " found. I used it" << std::endl;
164     return false;
165   }
166 }
167 //--------------------------------------------------------------------
168
169
170 //--------------------------------------------------------------------
171 template <class TImageType>
172 bool
173 clitk::ExtractLymphStationsFilter<TImageType>::
174 GetComputeStation(std::string station) 
175 {
176   return (m_ComputeStationMap.find(station) != m_ComputeStationMap.end());
177 }
178 //--------------------------------------------------------------------
179
180
181 //--------------------------------------------------------------------
182 template <class TImageType>
183 void
184 clitk::ExtractLymphStationsFilter<TImageType>::
185 AddComputeStation(std::string station) 
186 {
187   m_ComputeStationMap[station] = true;
188 }
189 //--------------------------------------------------------------------
190
191
192 //--------------------------------------------------------------------
193 template <class TImageType>
194 void 
195 clitk::ExtractLymphStationsFilter<TImageType>::
196 ExtractStation_8() 
197 {
198   if (CheckForStation("8")) {
199     ExtractStation_8_SI_Limits();
200     ExtractStation_8_Post_Limits();
201     ExtractStation_8_Ant_Sup_Limits();
202     ExtractStation_8_Ant_Inf_Limits();
203     ExtractStation_8_LR_1_Limits();
204     ExtractStation_8_LR_2_Limits();
205     ExtractStation_8_LR_Limits();
206     ExtractStation_8_Ant_Injected_Limits();
207     ExtractStation_8_Single_CCL_Limits();
208     ExtractStation_8_Remove_Structures();
209     // Store image filenames into AFDB 
210     writeImage<MaskImageType>(m_ListOfStations["8"], "seg/Station8.mhd");
211     GetAFDB()->SetImageFilename("Station8", "seg/Station8.mhd");  
212     WriteAFDB();
213   }
214 }
215 //--------------------------------------------------------------------
216
217
218 //--------------------------------------------------------------------
219 template <class TImageType>
220 void 
221 clitk::ExtractLymphStationsFilter<TImageType>::
222 ExtractStation_3P()
223 {
224   if (CheckForStation("3P")) {
225     ExtractStation_3P_SI_Limits();
226     ExtractStation_3P_Ant_Limits();
227     ExtractStation_3P_Post_Limits();
228     ExtractStation_3P_LR_sup_Limits();
229     //    ExtractStation_3P_LR_sup_Limits_2();
230     ExtractStation_3P_LR_inf_Limits();
231     ExtractStation_8_Single_CCL_Limits(); // YES 8 !
232     ExtractStation_3P_Remove_Structures(); // after CCL
233     // Store image filenames into AFDB 
234     writeImage<MaskImageType>(m_ListOfStations["3P"], "seg/Station3P.mhd");
235     GetAFDB()->SetImageFilename("Station3P", "seg/Station3P.mhd"); 
236     WriteAFDB(); 
237   }
238 }
239 //--------------------------------------------------------------------
240
241
242 //--------------------------------------------------------------------
243 template <class TImageType>
244 void 
245 clitk::ExtractLymphStationsFilter<TImageType>::
246 ExtractStation_7() {
247   if (CheckForStation("7")) {
248   ExtractStation_7_SI_Limits();
249   ExtractStation_7_RL_Limits();
250   ExtractStation_7_Posterior_Limits();
251   //  ExtractStation_8_Single_CCL_Limits(); // Yes the same than 8
252   ExtractStation_7_Remove_Structures();
253   // Store image filenames into AFDB 
254   writeImage<MaskImageType>(m_ListOfStations["7"], "seg/Station7.mhd");
255   GetAFDB()->SetImageFilename("Station7", "seg/Station7.mhd");  
256   WriteAFDB();
257   }
258 }
259 //--------------------------------------------------------------------
260
261
262 //--------------------------------------------------------------------
263 template <class TImageType>
264 void 
265 clitk::ExtractLymphStationsFilter<TImageType>::
266 ExtractStation_3A()
267 {
268   if (CheckForStation("3A")) {
269     ExtractStation_3A_SI_Limits();
270     ExtractStation_3A_Ant_Limits();
271     ExtractStation_3A_Post_Limits();
272     // Store image filenames into AFDB 
273     writeImage<MaskImageType>(m_ListOfStations["3A"], "seg/Station3A.mhd");
274     GetAFDB()->SetImageFilename("Station3A", "seg/Station3A.mhd"); 
275     WriteAFDB(); 
276   }
277 }
278 //--------------------------------------------------------------------
279
280
281 //--------------------------------------------------------------------
282 template <class TImageType>
283 void 
284 clitk::ExtractLymphStationsFilter<TImageType>::
285 ExtractStation_2RL()
286 {
287   if (CheckForStation("2RL")) {
288     ExtractStation_2RL_SI_Limits();
289     ExtractStation_2RL_Post_Limits();
290     ExtractStation_2RL_Ant_Limits2();
291     ExtractStation_2RL_Ant_Limits(); 
292     ExtractStation_2RL_LR_Limits(); 
293     ExtractStation_2RL_Remove_Structures(); 
294     ExtractStation_2RL_SeparateRL(); 
295     
296     // Store image filenames into AFDB 
297     writeImage<MaskImageType>(m_ListOfStations["2R"], "seg/Station2R.mhd");
298     writeImage<MaskImageType>(m_ListOfStations["2L"], "seg/Station2L.mhd");
299     GetAFDB()->SetImageFilename("Station2R", "seg/Station2R.mhd"); 
300     GetAFDB()->SetImageFilename("Station2L", "seg/Station2L.mhd"); 
301     WriteAFDB(); 
302   }
303 }
304 //--------------------------------------------------------------------
305
306
307 //--------------------------------------------------------------------
308 template <class TImageType>
309 void 
310 clitk::ExtractLymphStationsFilter<TImageType>::
311 ExtractStation_4RL() {
312   DD("TODO");
313   exit(0);
314   /*
315     WARNING ONLY 4R FIRST !!! (not same inf limits)
316   */    
317   ExtractStation_4RL_SI_Limits();
318   ExtractStation_4RL_LR_Limits();
319   ExtractStation_4RL_AP_Limits();
320 }
321 //--------------------------------------------------------------------
322
323
324 //--------------------------------------------------------------------
325 template <class ImageType>
326 void 
327 clitk::ExtractLymphStationsFilter<ImageType>::
328 Remove_Structures(std::string station, std::string s)
329 {
330   try {
331     StartNewStep("[Station"+station+"] Remove "+s);  
332     MaskImagePointer Structure = GetAFDB()->template GetImage<MaskImageType>(s);
333     clitk::AndNot<MaskImageType>(m_Working_Support, Structure, GetBackgroundValue());
334   }
335   catch(clitk::ExceptionObject e) {
336     std::cout << s << " not found, skip." << std::endl;
337   }
338 }
339 //--------------------------------------------------------------------
340
341
342 //--------------------------------------------------------------------
343 template <class TImageType>
344 void 
345 clitk::ExtractLymphStationsFilter<TImageType>::
346 SetFuzzyThreshold(std::string station, std::string tag, double value)
347 {
348   m_FuzzyThreshold[station][tag] = value;
349 }
350 //--------------------------------------------------------------------
351
352
353 //--------------------------------------------------------------------
354 template <class TImageType>
355 double 
356 clitk::ExtractLymphStationsFilter<TImageType>::
357 GetFuzzyThreshold(std::string station, std::string tag)
358 {
359   if (m_FuzzyThreshold.find(station) == m_FuzzyThreshold.end()) {
360     clitkExceptionMacro("Could not find options for station "+station+" in the list (while searching for tag "+tag+").");
361     return 0.0;
362   }
363
364   if (m_FuzzyThreshold[station].find(tag) == m_FuzzyThreshold[station].end()) {
365     clitkExceptionMacro("Could not find options "+tag+" in the list of FuzzyThreshold for station "+station+".");
366     return 0.0;
367   }
368   
369   return m_FuzzyThreshold[station][tag]; 
370 }
371 //--------------------------------------------------------------------
372
373
374
375
376 #endif //#define CLITKBOOLEANOPERATORLABELIMAGEFILTER_TXX