]> Creatis software - clitk.git/blob - segmentation/clitkExtractLymphStation_7.txx
Do not change visilibity of actors when changing renderer drawing action
[clitk.git] / segmentation / clitkExtractLymphStation_7.txx
1
2 //--------------------------------------------------------------------
3 template <class TImageType>
4 void 
5 clitk::ExtractLymphStationsFilter<TImageType>::
6 ExtractStation_7_SetDefaultValues()
7 {
8   SetFuzzyThreshold("7", "Bronchi", 0.1);
9   SetFuzzyThreshold("7", "LeftSuperiorPulmonaryVein", 0.3);
10   SetFuzzyThreshold("7", "RightSuperiorPulmonaryVein", 0.2);
11   SetFuzzyThreshold("7", "RightPulmonaryArtery", 0.3);
12   SetFuzzyThreshold("7", "LeftPulmonaryArtery", 0.5);
13   SetFuzzyThreshold("7", "SVC", 0.2);
14 }
15 //--------------------------------------------------------------------
16
17
18 //--------------------------------------------------------------------
19 template <class TImageType>
20 void 
21 clitk::ExtractLymphStationsFilter<TImageType>::
22 ExtractStation_7_SI_Limits() 
23 {
24   StartNewStep("[Station7] Inf/Sup mediastinum limits with carina/LLLBronchus");
25   // Get Inputs
26   MaskImagePointer Trachea = GetAFDB()->template GetImage <MaskImageType>("Trachea");  
27   
28   // We suppoe that CarinaZ was already computed (S8)
29   double m_CarinaZ = GetAFDB()->GetDouble("CarinaZ");
30   
31   //  double m_OriginOfRightMiddleLobeBronchusZ = GetAFDB()->GetPoint3D("OriginOfRightMiddleLobeBronchus", 2);
32   // DD(m_OriginOfRightMiddleLobeBronchusZ);
33   MaskImagePointer UpperBorderOfLLLBronchus = GetAFDB()->template GetImage<MaskImageType>("UpperBorderOfLLLBronchus");
34
35   // Search most inf point (WHY ? IS IT THE RIGHT STRUCTURE ??)
36   MaskImagePointType ps = UpperBorderOfLLLBronchus->GetOrigin(); // initialise to avoid warning 
37   clitk::FindExtremaPointInAGivenDirection<MaskImageType>(UpperBorderOfLLLBronchus, GetBackgroundValue(), 2, true, ps);
38   double m_UpperBorderOfLLLBronchusZ = ps[2];
39
40   /*
41   std::vector<MaskImagePointType> centroids;
42   clitk::ComputeCentroids<MaskImageType>(UpperBorderOfLLLBronchus, GetBackgroundValue(), centroids);
43   double m_UpperBorderOfLLLBronchusZ = centroids[1][2];
44   DD(m_UpperBorderOfLLLBronchusZ)
45   */
46
47   /* Crop support */
48   m_Working_Support = 
49     clitk::CropImageAlongOneAxis<MaskImageType>(m_Working_Support, 2, 
50                                                 m_UpperBorderOfLLLBronchusZ, 
51                                                 m_CarinaZ, true,
52                                                 GetBackgroundValue());
53   /* Crop trachea */
54   m_Working_Trachea = 
55     clitk::CropImageAlongOneAxis<MaskImageType>(Trachea, 2, 
56                                                 m_UpperBorderOfLLLBronchusZ, 
57                                                 m_CarinaZ, true,
58                                                 GetBackgroundValue());
59
60   StopCurrentStep<MaskImageType>(m_Working_Support);
61   m_ListOfStations["7"] = m_Working_Support;
62 }
63 //--------------------------------------------------------------------
64
65
66 //--------------------------------------------------------------------
67 template <class TImageType>
68 void 
69 clitk::ExtractLymphStationsFilter<TImageType>::
70 ExtractStation_7_RL_Limits() 
71 {
72   // ----------------------------------------------------------------
73   StartNewStep("[Station7] Limits with bronchus : RightTo the left bronchus");  
74
75   // First consider bronchus and keep the main CCL by slice
76   m_RightBronchus = GetAFDB()->template GetImage <MaskImageType>("RightBronchus");
77   m_LeftBronchus = GetAFDB()->template GetImage <MaskImageType>("LeftBronchus");
78
79   // Extract slices, Label, compute centroid, keep most central connected component
80   std::vector<MaskSlicePointer> slices_leftbronchus;
81   std::vector<MaskSlicePointer> slices_rightbronchus;
82   clitk::ExtractSlices<MaskImageType>(m_LeftBronchus, 2, slices_leftbronchus);
83   clitk::ExtractSlices<MaskImageType>(m_RightBronchus, 2, slices_rightbronchus);
84   
85   // Loop on slices
86   for(uint i=0; i<slices_leftbronchus.size(); i++) {
87     slices_leftbronchus[i] = Labelize<MaskSliceType>(slices_leftbronchus[i], 0, false, 10);
88     std::vector<typename MaskSliceType::PointType> c;
89     clitk::ComputeCentroids<MaskSliceType>(slices_leftbronchus[i], GetBackgroundValue(), c);
90     if (c.size() > 1) {
91       double most_at_left = c[1][0];
92       int most_at_left_index=1;
93       for(uint j=1; j<c.size(); j++) {
94         if (c[j][0] < most_at_left) {
95           most_at_left = c[j][0];
96           most_at_left_index = j;
97         }
98       }
99       // Put all other CCL to Background
100       slices_leftbronchus[i] = 
101         clitk::Binarize<MaskSliceType>(slices_leftbronchus[i], most_at_left_index, 
102                                        most_at_left_index, GetBackgroundValue(), GetForegroundValue());
103     } // end c.size
104   }
105   
106   for(uint i=0; i<slices_rightbronchus.size(); i++) {
107     slices_rightbronchus[i] = Labelize<MaskSliceType>(slices_rightbronchus[i], 0, false, 10);
108     std::vector<typename MaskSliceType::PointType> c;
109     clitk::ComputeCentroids<MaskSliceType>(slices_rightbronchus[i], GetBackgroundValue(), c);
110     if (c.size() > 1) {
111       double most_at_right = c[1][0];
112       int most_at_right_index=1;
113       for(uint j=1; j<c.size(); j++) {
114         if (c[j][0] > most_at_right) {
115           most_at_right = c[j][0];
116           most_at_right_index = j;
117         }
118       }
119       // Put all other CCL to Background
120       slices_rightbronchus[i] = 
121         clitk::Binarize<MaskSliceType>(slices_rightbronchus[i], most_at_right_index, 
122                                        most_at_right_index, GetBackgroundValue(), GetForegroundValue());
123     } // end c.size
124   }
125   
126   // Joint slices
127   m_LeftBronchus = clitk::JoinSlices<MaskImageType>(slices_leftbronchus, m_LeftBronchus, 2);
128   m_RightBronchus = clitk::JoinSlices<MaskImageType>(slices_rightbronchus, m_RightBronchus, 2);
129
130   writeImage<MaskImageType>(m_LeftBronchus, "step-left.mhd");
131   writeImage<MaskImageType>(m_RightBronchus, "step-right.mhd");
132
133   m_Working_Support = 
134     clitk::SliceBySliceRelativePosition<MaskImageType>(m_Working_Support, m_LeftBronchus, 2, 
135                                                        GetFuzzyThreshold("7", "Bronchi"), "RightTo", 
136                                                        false, 3, false);
137   StopCurrentStep<MaskImageType>(m_Working_Support);
138
139
140   // ----------------------------------------------------------------
141   StartNewStep("[Station7] Limits with bronchus : LeftTo the right bronchus");
142   m_Working_Support = 
143     clitk::SliceBySliceRelativePosition<MaskImageType>(m_Working_Support, m_RightBronchus, 2, 
144                                                        GetFuzzyThreshold("7", "Bronchi"), "LeftTo", 
145                                                        false, 3, false); 
146   StopCurrentStep<MaskImageType>(m_Working_Support);
147
148
149   // ----------------------------------------------------------------
150   StartNewStep("[Station7] Limits with LeftSuperiorPulmonaryVein");
151   try {
152     MaskImagePointer LeftSuperiorPulmonaryVein = GetAFDB()->template GetImage<MaskImageType>("LeftSuperiorPulmonaryVein");
153     typedef SliceBySliceRelativePositionFilter<MaskImageType> SliceRelPosFilterType;
154     typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
155     sliceRelPosFilter->SetInput(m_Working_Support);
156     sliceRelPosFilter->SetInputObject(LeftSuperiorPulmonaryVein);
157     sliceRelPosFilter->SetDirection(2);
158     sliceRelPosFilter->SetFuzzyThreshold(GetFuzzyThreshold("7", "LeftSuperiorPulmonaryVein"));
159     sliceRelPosFilter->AddOrientationTypeString("NotLeftTo");
160     sliceRelPosFilter->AddOrientationTypeString("NotAntTo");
161     sliceRelPosFilter->SetIntermediateSpacingFlag(true);
162     sliceRelPosFilter->SetIntermediateSpacing(3);
163     sliceRelPosFilter->SetUniqueConnectedComponentBySlice(false);
164     sliceRelPosFilter->SetAutoCropFlag(false); 
165     sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
166     sliceRelPosFilter->Update();
167     m_Working_Support = sliceRelPosFilter->GetOutput();
168     StopCurrentStep<MaskImageType>(m_Working_Support);
169   }
170   catch (clitk::ExceptionObject e) {
171     std::cout << "Not LeftSuperiorPulmonaryVein, skip" << std::endl;
172   }
173
174   // ----------------------------------------------------------------
175   StartNewStep("[Station7] Limits with RightSuperiorPulmonaryVein");
176   try {
177     MaskImagePointer RightSuperiorPulmonaryVein = GetAFDB()->template GetImage<MaskImageType>("RightSuperiorPulmonaryVein");
178     typedef SliceBySliceRelativePositionFilter<MaskImageType> SliceRelPosFilterType;
179     typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
180     sliceRelPosFilter->SetInput(m_Working_Support);
181     sliceRelPosFilter->SetInputObject(RightSuperiorPulmonaryVein);
182     sliceRelPosFilter->SetDirection(2);
183     sliceRelPosFilter->SetFuzzyThreshold(GetFuzzyThreshold("7", "RightSuperiorPulmonaryVein"));
184     sliceRelPosFilter->AddOrientationTypeString("NotRightTo");
185     sliceRelPosFilter->AddOrientationTypeString("NotAntTo");
186     sliceRelPosFilter->AddOrientationTypeString("NotPostTo");
187     sliceRelPosFilter->SetIntermediateSpacingFlag(true);
188     sliceRelPosFilter->SetIntermediateSpacing(3);
189     sliceRelPosFilter->SetUniqueConnectedComponentBySlice(false);
190     sliceRelPosFilter->SetAutoCropFlag(false); 
191     sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
192     sliceRelPosFilter->Update();
193     m_Working_Support = sliceRelPosFilter->GetOutput();
194     StopCurrentStep<MaskImageType>(m_Working_Support);
195   }
196   catch (clitk::ExceptionObject e) {
197     std::cout << "Not RightSuperiorPulmonaryVein, skip" << std::endl;
198   }
199
200   // ----------------------------------------------------------------
201   StartNewStep("[Station7] Limits with RightPulmonaryArtery");
202   MaskImagePointer RightPulmonaryArtery = GetAFDB()->template GetImage<MaskImageType>("RightPulmonaryArtery");
203   typedef SliceBySliceRelativePositionFilter<MaskImageType> SliceRelPosFilterType;
204   typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
205   sliceRelPosFilter->SetInput(m_Working_Support);
206   sliceRelPosFilter->SetInputObject(RightPulmonaryArtery);
207   sliceRelPosFilter->SetDirection(2);
208   sliceRelPosFilter->SetFuzzyThreshold(GetFuzzyThreshold("7", "RightPulmonaryArtery"));
209   sliceRelPosFilter->AddOrientationTypeString("NotAntTo");
210   sliceRelPosFilter->SetIntermediateSpacingFlag(true);
211   sliceRelPosFilter->SetIntermediateSpacing(3);
212   sliceRelPosFilter->SetUniqueConnectedComponentBySlice(false);
213   sliceRelPosFilter->SetAutoCropFlag(false); 
214   sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
215   sliceRelPosFilter->Update();
216   m_Working_Support = sliceRelPosFilter->GetOutput();
217   StopCurrentStep<MaskImageType>(m_Working_Support);
218
219   // ----------------------------------------------------------------
220   StartNewStep("[Station7] Limits with LeftPulmonaryArtery");
221   MaskImagePointer LeftPulmonaryArtery = GetAFDB()->template GetImage<MaskImageType>("LeftPulmonaryArtery");
222   sliceRelPosFilter = SliceRelPosFilterType::New();
223   sliceRelPosFilter->SetInput(m_Working_Support);
224   sliceRelPosFilter->SetInputObject(LeftPulmonaryArtery);
225   sliceRelPosFilter->SetDirection(2);
226   sliceRelPosFilter->SetFuzzyThreshold(GetFuzzyThreshold("7", "LeftPulmonaryArtery"));
227   sliceRelPosFilter->AddOrientationTypeString("NotAntTo");
228   sliceRelPosFilter->SetIntermediateSpacingFlag(true);
229   sliceRelPosFilter->SetIntermediateSpacing(3);
230   sliceRelPosFilter->SetUniqueConnectedComponentBySlice(false);
231   sliceRelPosFilter->SetAutoCropFlag(false); 
232   sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
233   sliceRelPosFilter->Update();
234   m_Working_Support = sliceRelPosFilter->GetOutput();
235   StopCurrentStep<MaskImageType>(m_Working_Support);
236
237   StartNewStep("[Station7] Limits with SVC");
238   MaskImagePointer SVC = GetAFDB()->template GetImage<MaskImageType>("SVC");
239   sliceRelPosFilter = SliceRelPosFilterType::New();
240   sliceRelPosFilter->SetInput(m_Working_Support);
241   sliceRelPosFilter->SetInputObject(SVC);
242   sliceRelPosFilter->SetDirection(2);
243   sliceRelPosFilter->SetFuzzyThreshold(GetFuzzyThreshold("7", "SVC"));
244   sliceRelPosFilter->AddOrientationTypeString("NotRightTo");
245   sliceRelPosFilter->AddOrientationTypeString("NotAntTo");
246   sliceRelPosFilter->SetIntermediateSpacingFlag(true);
247   sliceRelPosFilter->SetIntermediateSpacing(3);
248   sliceRelPosFilter->SetUniqueConnectedComponentBySlice(false);
249   sliceRelPosFilter->SetAutoCropFlag(true); 
250   sliceRelPosFilter->IgnoreEmptySliceObjectFlagOn();
251   sliceRelPosFilter->Update();
252   m_Working_Support = sliceRelPosFilter->GetOutput();
253   StopCurrentStep<MaskImageType>(m_Working_Support);
254   
255   // End
256   m_ListOfStations["7"] = m_Working_Support;
257 }
258 //--------------------------------------------------------------------
259
260
261 //--------------------------------------------------------------------
262 template <class TImageType>
263 void 
264 clitk::ExtractLymphStationsFilter<TImageType>::
265 ExtractStation_7_Posterior_Limits() 
266 {
267   StartNewStep("[Station7] Posterior limits -> must be AntTo post wall of the bronchi (OLD CLASSIF)");  
268
269   // Search for points that are the most left/post/ant and most
270   // right/post/ant of the left and right bronchus
271
272   // extract, loop slices, label/keep, find extrema x 3
273   /*  FindExtremaPointsInBronchus(m_LeftBronchus, 0, 15, m_RightMostInLeftBronchus, 
274                               m_AntMostInLeftBronchus, m_PostMostInLeftBronchus);
275   FindExtremaPointsInBronchus(m_RightBronchus, 1, 15, m_LeftMostInRightBronchus, 
276                               m_AntMostInRightBronchus, m_PostMostInRightBronchus);
277   */
278   
279   // First cut bronchus to the correct sup/inf support 
280   MaskImagePointer RightBronchus = clitk::ResizeImageLike<MaskImageType>(m_RightBronchus, m_Working_Support, GetBackgroundValue());
281   MaskImagePointer LeftBronchus = clitk::ResizeImageLike<MaskImageType>(m_LeftBronchus, m_Working_Support, GetBackgroundValue());
282
283   // Find extrema points
284   FindExtremaPointsInBronchus(RightBronchus, 0, 10, m_LeftMostInRightBronchus, 
285                               m_AntMostInRightBronchus, m_PostMostInRightBronchus);
286
287   FindExtremaPointsInBronchus(LeftBronchus, 1, 10, m_RightMostInLeftBronchus, 
288                               m_AntMostInLeftBronchus, m_PostMostInLeftBronchus);
289
290
291
292   // DEBUG
293   std::ofstream osrl; openFileForWriting(osrl, "osrl.txt"); osrl << "LANDMARKS1" << std::endl;
294   std::ofstream osal; openFileForWriting(osal, "osal.txt"); osal << "LANDMARKS1" << std::endl;
295   std::ofstream ospl; openFileForWriting(ospl, "ospl.txt"); ospl << "LANDMARKS1" << std::endl;
296   std::ofstream osrr; openFileForWriting(osrr, "osrr.txt"); osrr << "LANDMARKS1" << std::endl;
297   std::ofstream osar; openFileForWriting(osar, "osar.txt"); osar << "LANDMARKS1" << std::endl;
298   std::ofstream ospr; openFileForWriting(ospr, "ospr.txt"); ospr << "LANDMARKS1" << std::endl;
299
300   for(uint i=0; i<m_RightMostInLeftBronchus.size(); i++) {
301     osrl << i << " " << m_RightMostInLeftBronchus[i][0] << " " << m_RightMostInLeftBronchus[i][1] 
302          << " " << m_RightMostInLeftBronchus[i][2] << " 0 0 " << std::endl;
303     osal << i << " " << m_AntMostInLeftBronchus[i][0] << " " << m_AntMostInLeftBronchus[i][1] 
304          << " " << m_AntMostInLeftBronchus[i][2] << " 0 0 " << std::endl;
305     ospl << i << " " << m_PostMostInLeftBronchus[i][0] << " " << m_PostMostInLeftBronchus[i][1] 
306          << " " << m_PostMostInLeftBronchus[i][2] << " 0 0 " << std::endl;
307   }
308
309   for(uint i=0; i<m_LeftMostInRightBronchus.size(); i++) {
310     osrr << i << " " << m_LeftMostInRightBronchus[i][0] << " " << m_LeftMostInRightBronchus[i][1] 
311          << " " << m_LeftMostInRightBronchus[i][2] << " 0 0 " << std::endl;
312     osar << i << " " << m_AntMostInRightBronchus[i][0] << " " << m_AntMostInRightBronchus[i][1] 
313          << " " << m_AntMostInRightBronchus[i][2] << " 0 0 " << std::endl;
314     ospr << i << " " << m_PostMostInRightBronchus[i][0] << " " << m_PostMostInRightBronchus[i][1] 
315          << " " << m_PostMostInRightBronchus[i][2] << " 0 0 " << std::endl;
316   }
317   osrl.close();
318   osal.close();
319   ospl.close();
320   osrr.close();
321   osar.close();
322   ospr.close();
323
324   clitk::SliceBySliceSetBackgroundFromLineSeparation<MaskImageType>(m_Working_Support, 
325                                                                     m_PostMostInRightBronchus,
326                                                                     m_PostMostInLeftBronchus,
327                                                                     GetBackgroundValue(), 1, -10);
328   // If needed -> can do the same with AntMost.
329
330   // End
331   StopCurrentStep<MaskImageType>(m_Working_Support);
332   m_ListOfStations["7"] = m_Working_Support;
333 }
334 //--------------------------------------------------------------------
335
336 //--------------------------------------------------------------------
337 template <class ImageType>
338 void 
339 clitk::ExtractLymphStationsFilter<ImageType>::
340 ExtractStation_7_Remove_Structures()
341 {
342
343   //--------------------------------------------------------------------
344   StartNewStep("[Station7] remove some structures");
345
346   Remove_Structures("7", "AzygousVein");
347   Remove_Structures("7", "Aorta");
348   Remove_Structures("7", "Esophagus");
349   Remove_Structures("7", "RightPulmonaryArtery");
350   Remove_Structures("7", "LeftPulmonaryArtery");
351   Remove_Structures("7", "LeftSuperiorPulmonaryVein");
352   Remove_Structures("7", "PulmonaryTrunk");
353   Remove_Structures("7", "VertebralBody");
354
355   // END
356   StopCurrentStep<MaskImageType>(m_Working_Support);
357   m_ListOfStations["7"] = m_Working_Support;
358   return;
359 }
360 //--------------------------------------------------------------------
361
362
363