]> Creatis software - clitk.git/blob - segmentation/clitkExtractLymphStationsFilter.txx
Station8 almost ok.
[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
59   // Station 7
60   SetFuzzyThreshold(0.5);
61   SetStation7Filename("station7.mhd");
62 }
63 //--------------------------------------------------------------------
64
65
66 //--------------------------------------------------------------------
67 template <class TImageType>
68 void 
69 clitk::ExtractLymphStationsFilter<TImageType>::
70 GenerateOutputInformation() { 
71   // Get inputs
72   LoadAFDB();
73   m_Input = dynamic_cast<const ImageType*>(itk::ProcessObject::GetInput(0));
74   m_Mediastinum = GetAFDB()->template GetImage <MaskImageType>("Mediastinum");
75
76   // Extract Station8
77   StartNewStep("Station 8");
78   StartSubStep(); 
79   ExtractStation_8();
80   StopSubStep();
81
82   DD(GetCurrentStepNumber());
83
84   // Extract Station3P
85   StartNewStep("Station 3P");
86   StartSubStep(); 
87   ExtractStation_3P();
88   StopSubStep();
89
90   if (0) { // temporary suppress
91     // Extract Station7
92     StartNewStep("Station 7");
93     StartSubStep();
94     ExtractStation_7();
95     StopSubStep();
96
97     // Extract Station4RL
98     StartNewStep("Station 4RL");
99     StartSubStep();
100     //ExtractStation_4RL();
101     StopSubStep();
102   }
103
104
105   //
106   //  typedef clitk::BooleanOperatorLabelImageFilter<MaskImageType> BFilter;
107   //BFilter::Pointer merge = BFilter::New();  
108   // writeImage<MaskImageType>(m_Output, "ouput.mhd");
109   //writeImage<MaskImageType>(m_Working_Support, "ws.mhd");
110   /*merge->SetInput1(m_Station7);
111     merge->SetInput2(m_Station4RL); // support
112     merge->SetOperationType(BFilter::AndNot); CHANGE OPERATOR
113     merge->SetForegroundValue(4);
114     merge->Update();
115     m_Output = merge->GetOutput();
116   */
117 }
118 //--------------------------------------------------------------------
119
120
121 //--------------------------------------------------------------------
122 template <class TImageType>
123 void 
124 clitk::ExtractLymphStationsFilter<TImageType>::
125 GenerateInputRequestedRegion() {
126   DD("GenerateInputRequestedRegion (nothing?)");
127 }
128 //--------------------------------------------------------------------
129
130
131 //--------------------------------------------------------------------
132 template <class TImageType>
133 void 
134 clitk::ExtractLymphStationsFilter<TImageType>::
135 GenerateData() {
136   DD("GenerateData, graft output");
137
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("8")) {
163     std::cout << "Station " << station << " already exists, but re-computation forced." << std::endl;
164   }
165   if (!found || GetComputeStation("8")) {
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_Limits();
209     ExtractStation_8_LR_Limits();
210     // Store image filenames into AFDB 
211     writeImage<MaskImageType>(m_ListOfStations["8"], "seg/Station8.mhd");
212     GetAFDB()->SetImageFilename("Station8", "seg/Station8.mhd");  
213     WriteAFDB();
214   }
215 }
216 //--------------------------------------------------------------------
217
218
219 //--------------------------------------------------------------------
220 template <class TImageType>
221 void 
222 clitk::ExtractLymphStationsFilter<TImageType>::
223 ExtractStation_3P()
224 {
225   if (CheckForStation("3P")) {
226     ExtractStation_3P_SI_Limits();
227     // Store image filenames into AFDB 
228     writeImage<MaskImageType>(m_ListOfStations["3P"], "seg/Station3P.mhd");
229     GetAFDB()->SetImageFilename("Station3P", "seg/Station3P.mhd");  
230   }
231 }
232 //--------------------------------------------------------------------
233
234
235 //--------------------------------------------------------------------
236 template <class TImageType>
237 void 
238 clitk::ExtractLymphStationsFilter<TImageType>::
239 ExtractStation_7() {
240   if (m_ListOfStations["7"]) {
241     DD("Station 7 support already exist -> use it");
242     m_Working_Support = m_ListOfStations["7"];
243   }
244   else m_Working_Support = m_Mediastinum;
245   ExtractStation_7_SI_Limits();
246   ExtractStation_7_RL_Limits();
247   ExtractStation_7_Posterior_Limits();
248 }
249 //--------------------------------------------------------------------
250
251
252 //--------------------------------------------------------------------
253 template <class TImageType>
254 void 
255 clitk::ExtractLymphStationsFilter<TImageType>::
256 ExtractStation_4RL() {
257   /*
258     WARNING ONLY 4R FIRST !!! (not same inf limits)
259   */    
260   ExtractStation_4RL_SI_Limits();
261   ExtractStation_4RL_LR_Limits();
262   ExtractStation_4RL_AP_Limits();
263 }
264 //--------------------------------------------------------------------
265
266
267 //--------------------------------------------------------------------
268
269
270 //--------------------------------------------------------------------
271 template <class TImageType>
272 void 
273 clitk::ExtractLymphStationsFilter<TImageType>::
274 FindExtremaPointsInBronchus(MaskImagePointer input, 
275                             int direction,
276                             double distance_max_from_center_point, 
277                             ListOfPointsType & LR, 
278                             ListOfPointsType & Ant, 
279                             ListOfPointsType & Post)
280 {
281
282   // Other solution ==> with auto bounding box ! (but pb to prevent to
283   // be too distant from the center point
284
285   // Extract slices
286   std::vector<typename MaskSliceType::Pointer> slices;
287   clitk::ExtractSlices<MaskImageType>(input, 2, slices);
288   
289   // Loop on slices
290   bool found;
291   for(uint i=0; i<slices.size(); i++) {
292     /*
293     // Keep main CCL
294     slices[i] = Labelize<MaskSliceType>(slices[i], 0, true, 10);
295     slices[i] = KeepLabels<MaskSliceType>(slices[i], 
296                                           GetBackgroundValue(), 
297                                           GetForegroundValue(), 1, 1, true);
298     */
299
300     // ------- Find rightmost or leftmost point  ------- 
301     MaskSliceType::PointType LRMost;
302     found = 
303       clitk::FindExtremaPointInAGivenDirection<MaskSliceType>(slices[i], 
304                                                               GetBackgroundValue(), 
305                                                               0, // axis XY
306                                                               (direction==0?false:true),  // right or left according to direction
307                                                               LRMost);
308     // ------- Find postmost point  ------- 
309     MaskSliceType::PointType postMost;
310     found = 
311       clitk::FindExtremaPointInAGivenDirection<MaskSliceType>(slices[i], 
312                                                               GetBackgroundValue(), 
313                                                               1, false, LRMost, 
314                                                               distance_max_from_center_point, 
315                                                               postMost);
316     // ------- Find antmost point  ------- 
317     MaskSliceType::PointType antMost;
318     found = 
319       clitk::FindExtremaPointInAGivenDirection<MaskSliceType>(slices[i], 
320                                                               GetBackgroundValue(), 
321                                                               1, true, LRMost, 
322                                                               distance_max_from_center_point, 
323                                                               antMost);
324     // Only add point if found
325     if (found)  {
326       // ------- Convert 2D to 3D points --------
327       MaskImageType::PointType p;
328       clitk::PointsUtils<MaskImageType>::Convert2DTo3D(LRMost, input, i, p);
329       LR.push_back(p); 
330       clitk::PointsUtils<MaskImageType>::Convert2DTo3D(antMost, input, i, p);
331       Ant.push_back(p);
332       clitk::PointsUtils<MaskImageType>::Convert2DTo3D(postMost, input, i, p);
333       Post.push_back(p);
334     }
335   }
336
337 //--------------------------------------------------------------------
338
339 #endif //#define CLITKBOOLEANOPERATORLABELIMAGEFILTER_TXX