]> Creatis software - clitk.git/blob - segmentation/clitkExtractLymphStation_3P.txx
Merge branch 'master' of /home/dsarrut/clitk3.server
[clitk.git] / segmentation / clitkExtractLymphStation_3P.txx
1
2 //--------------------------------------------------------------------
3 template <class ImageType>
4 void 
5 clitk::ExtractLymphStationsFilter<ImageType>::
6 ExtractStation_3P_SetDefaultValues()
7 {
8 }
9 //--------------------------------------------------------------------
10
11
12 //--------------------------------------------------------------------
13 template <class TImageType>
14 void 
15 clitk::ExtractLymphStationsFilter<TImageType>::
16 ExtractStation_3P()
17 {
18   if (!CheckForStation("3P")) return;
19
20   StartNewStep("Station 3P");
21   StartSubStep();  
22
23   // Get the current support 
24   StartNewStep("[Station 3P] Get the current 3P suppport");
25   m_Working_Support = m_ListOfSupports["S3P"];
26   m_ListOfStations["3P"] = m_Working_Support;
27   StopCurrentStep<MaskImageType>(m_Working_Support);
28
29   /* TODO
30      LR_sup -> AzygousVein, Aorta
31
32 keep computed object, RelPos then
33      
34
35    */
36
37   
38   ExtractStation_3P_LR_inf_Limits();  
39   ExtractStation_8_Single_CCL_Limits(); // YES 8 !
40   ExtractStation_3P_Remove_Structures(); // after CCL
41
42   // Old stuff
43   // LR limits superiorly => not here for the moment because not
44   //  clear in the def
45   // ExtractStation_3P_LR_sup_Limits_2(); //TODO
46   // ExtractStation_3P_LR_sup_Limits();   // old version to change
47   
48   m_ListOfStations["3P"] = this->ApplyRelativePositionList("Station_3P", m_ListOfStations["3P"]); 
49
50   // Store image filenames into AFDB 
51   writeImage<MaskImageType>(m_ListOfStations["3P"], "seg/Station3P.mhd");
52   GetAFDB()->SetImageFilename("Station3P", "seg/Station3P.mhd"); 
53   WriteAFDB();   
54   StopSubStep();
55 }
56 //--------------------------------------------------------------------
57
58
59 //--------------------------------------------------------------------
60 template <class ImageType>
61 void 
62 clitk::ExtractLymphStationsFilter<ImageType>::
63 ExtractStation_3P_Remove_Structures() 
64 {
65   StartNewStep("[Station 3P] Remove some structures.");
66
67   Remove_Structures("3P", "Aorta");
68   Remove_Structures("3P", "VertebralBody");
69   Remove_Structures("3P", "SubclavianArteryLeft");
70   Remove_Structures("3P", "SubclavianArteryRight");
71   Remove_Structures("3P", "Esophagus");
72   Remove_Structures("3P", "AzygousVein");
73   Remove_Structures("3P", "Thyroid");
74   Remove_Structures("3P", "VertebralArtery");
75
76   StopCurrentStep<MaskImageType>(m_Working_Support);
77   m_ListOfStations["3P"] = m_Working_Support;
78 }
79 //--------------------------------------------------------------------
80
81
82 //--------------------------------------------------------------------
83 template <class ImageType>
84 void 
85 clitk::ExtractLymphStationsFilter<ImageType>::
86 ExtractStation_3P_LR_sup_Limits() 
87 {
88   /*
89     "On the right side, the limit is defined by the air–soft-tissue
90     interface. On the left side, it is defined by the air–tissue
91     interface superiorly (Fig. 2A–C) and the aorta inferiorly
92     (Figs. 2D–I and 3A–C)."
93
94     sup : 
95     Resizelike support : Trachea, SubclavianArtery
96     Trachea, slice by slice, get centroid trachea
97     SubclavianArtery, slice by slice, CCL
98     prendre la 1ère à L et R, not at Left
99     
100   */
101   StartNewStep("[Station 3P] Left/Right limits (superior part) ");
102
103   // Load structures
104   MaskImagePointer Trachea = GetAFDB()->template GetImage<MaskImageType>("Trachea");
105   MaskImagePointer SubclavianArtery = GetAFDB()->template GetImage<MaskImageType>("SubclavianArtery");
106   
107   // Crop like current support
108   Trachea = clitk::ResizeImageLike<MaskImageType>(Trachea, m_Working_Support, GetBackgroundValue());
109   SubclavianArtery = clitk::ResizeImageLike<MaskImageType>(SubclavianArtery, m_Working_Support, GetBackgroundValue());
110   
111   writeImage<MaskImageType>(Trachea, "tr.mhd");
112   writeImage<MaskImageType>(SubclavianArtery, "sca.mhd");
113   
114   // Get list of slices
115   std::vector<MaskSlicePointer> slices_support;
116   std::vector<MaskSlicePointer> slices_trachea;
117   std::vector<MaskSlicePointer> slices_subclavianartery;
118   clitk::ExtractSlices<MaskImageType>(m_Working_Support, 2, slices_support);
119   clitk::ExtractSlices<MaskImageType>(Trachea, 2, slices_trachea);
120   clitk::ExtractSlices<MaskImageType>(SubclavianArtery, 2, slices_subclavianartery);
121
122   // Loop on slices
123   std::vector<MaskImagePointType> points;
124   MaskImagePointType p;
125   for(uint i=0; i<slices_support.size(); i++) {
126     // Get Trachea centroid
127     std::vector<typename MaskSliceType::PointType> centroids;
128     typename MaskSliceType::PointType c;
129     ComputeCentroids<MaskSliceType>(slices_trachea[i], GetBackgroundValue(), centroids);
130     c = centroids[1];
131
132     // [debug] Store point
133     clitk::PointsUtils<MaskImageType>::Convert2DTo3D(centroids[1], Trachea, i, p);
134     points.push_back(p);
135     
136     // Get Right and Left CCL in SubclavianArtery
137     slices_subclavianartery[i] = Labelize<MaskSliceType>(slices_subclavianartery[i], 0, true, 10);
138     ComputeCentroids<MaskSliceType>(slices_subclavianartery[i], GetBackgroundValue(), centroids);
139
140     if (centroids.size() > 1) {
141       // Determine the one at Right/Left -> first after Trachea
142       // centroid 
143       typename MaskSliceType::PointType right;
144       typename MaskSliceType::PointType left;
145       int label_right=-1;
146       int label_left=-1;
147       right[0] = c[0]-100;
148       left[0] = c[0]+100;
149       for(uint j=1; j<centroids.size(); j++) {
150         if (centroids[j][0] < c[0]) { // At Right of Trachea centroid
151           if (centroids[j][0] >= right[0]) {
152             right = centroids[j];
153             label_right = j;
154           }
155         }
156         if (centroids[j][0] > c[0]) { // At Left of Trachea centroid
157           if (centroids[j][0] <= left[0]) {
158             left = centroids[j];
159             label_left = j;
160           }
161         }
162       }
163
164       if (label_right != -1) {
165     
166         // Debug points
167         clitk::PointsUtils<MaskImageType>::Convert2DTo3D(centroids[label_right], SubclavianArtery, i, p);
168         points.push_back(p);
169
170         // Set Background and ForegroundValue according to label_right
171         MaskSlicePointer object = 
172           clitk::Binarize<MaskSliceType>(slices_subclavianartery[i], label_right, label_right, 
173                                          GetBackgroundValue(), GetForegroundValue());
174       
175         // Relative Position : not at Right
176         typedef clitk::AddRelativePositionConstraintToLabelImageFilter<MaskSliceType> RelPosFilterType;
177         typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New();
178         relPosFilter->VerboseStepFlagOff();
179         relPosFilter->WriteStepFlagOff();
180         relPosFilter->SetBackgroundValue(GetBackgroundValue());
181         relPosFilter->SetInput(slices_support[i]); 
182         relPosFilter->SetInputObject(object); 
183         relPosFilter->AddOrientationTypeString("RightTo");
184         relPosFilter->SetInverseOrientationFlag(true);
185         //      relPosFilter->SetIntermediateSpacing(3);
186         relPosFilter->SetIntermediateSpacingFlag(false);
187         relPosFilter->SetFuzzyThreshold(0.7);
188         relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
189         relPosFilter->Update();
190         slices_support[i] = relPosFilter->GetOutput();
191
192         // Relative Position : not Anterior
193         relPosFilter = RelPosFilterType::New();
194         relPosFilter->VerboseStepFlagOff();
195         relPosFilter->WriteStepFlagOff();
196         relPosFilter->SetBackgroundValue(GetBackgroundValue());
197         relPosFilter->SetInput(slices_support[i]); 
198         relPosFilter->SetInputObject(object); 
199         relPosFilter->AddOrientationTypeString("AntTo");
200         relPosFilter->SetInverseOrientationFlag(true);
201         //      relPosFilter->SetIntermediateSpacing(3);
202         relPosFilter->SetIntermediateSpacingFlag(false);
203         relPosFilter->SetFuzzyThreshold(0.7);
204         relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
205         relPosFilter->Update();
206         slices_support[i] = relPosFilter->GetOutput();
207
208       } // End RelativePosition for Right
209
210
211       if (label_left != -1) {
212     
213         // Debug points
214         clitk::PointsUtils<MaskImageType>::Convert2DTo3D(centroids[label_left], SubclavianArtery, i, p);
215         points.push_back(p);
216
217         // Set Background and ForegroundValue according to label_right
218         MaskSlicePointer object = 
219           clitk::Binarize<MaskSliceType>(slices_subclavianartery[i], label_left, label_left, 
220                                          GetBackgroundValue(), GetForegroundValue());
221       
222         // Relative Position : not at Right
223         typedef clitk::AddRelativePositionConstraintToLabelImageFilter<MaskSliceType> RelPosFilterType;
224         typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New();
225         relPosFilter->VerboseStepFlagOff();
226         relPosFilter->WriteStepFlagOff();
227         relPosFilter->SetBackgroundValue(GetBackgroundValue());
228         relPosFilter->SetInput(slices_support[i]); 
229         relPosFilter->SetInputObject(object); 
230         relPosFilter->AddOrientationTypeString("L");
231         relPosFilter->SetInverseOrientationFlag(true);
232         //      relPosFilter->SetIntermediateSpacing(3);
233         relPosFilter->SetIntermediateSpacingFlag(false);
234         relPosFilter->SetFuzzyThreshold(0.7);
235         relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
236         relPosFilter->Update();
237         slices_support[i] = relPosFilter->GetOutput();
238
239         // Relative Position : not Anterior
240         relPosFilter = RelPosFilterType::New();
241         relPosFilter->VerboseStepFlagOff();
242         relPosFilter->WriteStepFlagOff();
243         relPosFilter->SetBackgroundValue(GetBackgroundValue());
244         relPosFilter->SetInput(slices_support[i]); 
245         relPosFilter->SetInputObject(object); 
246         relPosFilter->AddOrientationTypeString("A");
247         relPosFilter->SetInverseOrientationFlag(true);
248         //      relPosFilter->SetIntermediateSpacing(3);
249         relPosFilter->SetIntermediateSpacingFlag(false);
250         relPosFilter->SetFuzzyThreshold(0.7);
251         relPosFilter->AutoCropFlagOff(); // important ! because we join the slices after this loop
252         relPosFilter->Update();
253         slices_support[i] = relPosFilter->GetOutput();
254
255       }
256
257     
258     } // if centroids.size > 1
259   } // End loop slices
260
261   // Joint slices
262   m_Working_Support = clitk::JoinSlices<MaskImageType>(slices_support, m_Working_Support, 2);
263
264   // Save list points
265   clitk::WriteListOfLandmarks<MaskImageType>(points, "subcl-lr.txt");
266
267
268   StopCurrentStep<MaskImageType>(m_Working_Support);
269   m_ListOfStations["3P"] = m_Working_Support;
270 }
271 //--------------------------------------------------------------------
272
273 //--------------------------------------------------------------------
274 template <class ImageType>
275 void 
276 clitk::ExtractLymphStationsFilter<ImageType>::
277 ExtractStation_3P_LR_sup_Limits_2() 
278 {
279   /*
280     "On the right side, the limit is defined by the air–soft-tissue
281     interface. On the left side, it is defined by the air–tissue
282     interface superiorly (Fig. 2A–C) and the aorta inferiorly
283     (Figs. 2D–I and 3A–C)."
284
285     sup : 
286     Resizelike support : Trachea, SubclavianArtery
287     Trachea, slice by slice, get centroid trachea
288     SubclavianArtery, slice by slice, CCL
289     prendre la 1ère à L et R, not at Left
290     
291   */
292   //  StartNewStep("[Station 3P] Left/Right limits (superior part) ");
293   
294
295 }
296 //--------------------------------------------------------------------
297
298 //--------------------------------------------------------------------
299 template <class ImageType>
300 void 
301 clitk::ExtractLymphStationsFilter<ImageType>::
302 ExtractStation_3P_LR_inf_Limits() 
303 {
304   /*
305     "On the right side, the limit is defined by the air–soft-tissue
306     interface. On the left side, it is defined by the air–tissue
307     interface superiorly (Fig. 2A–C) and the aorta inferiorly
308     (Figs. 2D–I and 3A–C)."
309
310     inf : not Right to Azygousvein    
311   */
312   StartNewStep("[Station 3P] Left/Right limits (inferior part) with Azygousvein and Aorta");
313
314   // Load structures
315   MaskImagePointer AzygousVein = GetAFDB()->template GetImage<MaskImageType>("AzygousVein");
316   MaskImagePointer Aorta = GetAFDB()->template GetImage<MaskImageType>("Aorta");
317
318   DD("ici");
319   writeImage<MaskImageType>(m_Working_Support, "ws.mhd");
320
321   typedef clitk::AddRelativePositionConstraintToLabelImageFilter<MaskImageType> RelPosFilterType;
322   typename RelPosFilterType::Pointer relPosFilter = RelPosFilterType::New();
323   relPosFilter->VerboseStepFlagOff();
324   relPosFilter->WriteStepFlagOff();
325   relPosFilter->SetBackgroundValue(GetBackgroundValue());
326   relPosFilter->SetInput(m_Working_Support); 
327   relPosFilter->SetInputObject(AzygousVein); 
328   relPosFilter->AddOrientationTypeString("R");
329   relPosFilter->SetInverseOrientationFlag(true);
330   relPosFilter->SetIntermediateSpacing(3);
331   relPosFilter->SetIntermediateSpacingFlag(false);
332   relPosFilter->SetFuzzyThreshold(0.7);
333   relPosFilter->AutoCropFlagOn();
334   relPosFilter->Update();
335   m_Working_Support = relPosFilter->GetOutput();
336
337   DD("la");
338   writeImage<MaskImageType>(m_Working_Support, "before-L-aorta.mhd");
339   relPosFilter->SetInput(m_Working_Support); 
340   relPosFilter->SetInputObject(Aorta); 
341   relPosFilter->AddOrientationTypeString("L");
342   relPosFilter->SetInverseOrientationFlag(true);
343   relPosFilter->SetIntermediateSpacing(3);
344   relPosFilter->SetIntermediateSpacingFlag(false);
345   relPosFilter->SetFuzzyThreshold(0.7);
346   relPosFilter->AutoCropFlagOn();
347   relPosFilter->Update();
348   m_Working_Support = relPosFilter->GetOutput();
349   writeImage<MaskImageType>(m_Working_Support, "after-L-aorta.mhd");
350
351   StopCurrentStep<MaskImageType>(m_Working_Support);
352   m_ListOfStations["3P"] = m_Working_Support;
353 }
354 //--------------------------------------------------------------------