]> Creatis software - clitk.git/blob - itk/clitkRelativePositionAnalyzerFilter.txx
Remove warnings
[clitk.git] / itk / clitkRelativePositionAnalyzerFilter.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://www.centreleonberard.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 //--------------------------------------------------------------------
20 template <class ImageType>
21 clitk::RelativePositionAnalyzerFilter<ImageType>::
22 RelativePositionAnalyzerFilter():
23   itk::ImageToImageFilter<ImageType, ImageType>()
24 {
25   this->SetNumberOfRequiredInputs(3); // Input : support, object, target
26   SetBackgroundValue(0);
27   SetForegroundValue(1);
28   SetNumberOfBins(100);
29   SetAreaLossTolerance(0.01);
30   SetSupportSize(0);
31   SetTargetSize(0);
32   SetSizeWithThreshold(0);
33   SetSizeWithReverseThreshold(0);
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 template <class ImageType>
40 void 
41 clitk::RelativePositionAnalyzerFilter<ImageType>::
42 SetInputSupport(const ImageType * image) 
43 {
44   // Process object is not const-correct so the const casting is required.
45   this->SetNthInput(0, const_cast<ImageType *>(image));
46 }
47 //--------------------------------------------------------------------
48   
49
50 //--------------------------------------------------------------------
51 template <class ImageType>
52 void 
53 clitk::RelativePositionAnalyzerFilter<ImageType>::
54 SetInputObject(const ImageType * image) 
55 {
56   // Process object is not const-correct so the const casting is required.
57   this->SetNthInput(1, const_cast<ImageType *>(image));
58 }
59 //--------------------------------------------------------------------
60   
61
62 //--------------------------------------------------------------------
63 template <class ImageType>
64 void 
65 clitk::RelativePositionAnalyzerFilter<ImageType>::
66 SetInputTarget(const ImageType * image) 
67 {
68   // Process object is not const-correct so the const casting is required.
69   this->SetNthInput(2, const_cast<ImageType *>(image));
70 }
71 //--------------------------------------------------------------------
72   
73
74 //--------------------------------------------------------------------
75 template <class ImageType>
76 void 
77 clitk::RelativePositionAnalyzerFilter<ImageType>::
78 PrintOptions() 
79 {
80   DD("TODO");
81 }
82 //--------------------------------------------------------------------
83
84
85 //--------------------------------------------------------------------
86 template <class ImageType>
87 void 
88 clitk::RelativePositionAnalyzerFilter<ImageType>::
89 GenerateOutputInformation() 
90
91   ImagePointer input = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
92   ImagePointer outputImage = this->GetOutput(0);
93   outputImage->SetRegions(outputImage->GetLargestPossibleRegion());
94 }
95 //--------------------------------------------------------------------
96
97
98 //--------------------------------------------------------------------
99 template <class ImageType>
100 void 
101 clitk::RelativePositionAnalyzerFilter<ImageType>::
102 GenerateData() 
103 {
104   ImagePointer temp = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
105   m_Object = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
106   m_Target = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(2));
107
108   // Remove object from support (keep initial image)
109   m_Support = clitk::Clone<ImageType>(temp);
110   clitk::AndNot<ImageType>(m_Support, m_Object, GetBackgroundValue());
111   
112   // Define filter to compute statics on mask image
113   typedef itk::LabelStatisticsImageFilter<ImageType, ImageType> StatFilterType;
114   typename StatFilterType::Pointer statFilter = StatFilterType::New();
115
116   // Compute the initial support size
117   statFilter->SetInput(m_Support);
118   statFilter->SetLabelInput(m_Support);
119   statFilter->Update();
120   SetSupportSize(statFilter->GetCount(GetForegroundValue()));
121   // DD(GetSupportSize());
122   
123   // Compute the initial target size
124   ImagePointer s = clitk::ResizeImageLike<ImageType>(m_Support, m_Target, GetBackgroundValue());
125   statFilter->SetInput(s);
126   statFilter->SetLabelInput(m_Target);
127   statFilter->Update();
128   SetTargetSize(statFilter->GetCount(GetForegroundValue()));
129   // DD(GetTargetSize());
130
131   //
132   int bins = GetNumberOfBins();
133   double tolerance = GetAreaLossTolerance();
134
135   // Compute Fuzzy map
136   double angle = GetDirection().angle1;
137   typename FloatImageType::Pointer map = ComputeFuzzyMap(m_Object, m_Target, m_Support, angle);
138   writeImage<FloatImageType>(map, "fuzzy_"+toString(clitk::rad2deg(angle))+".mha");
139
140   // Compute the optimal thresholds (direct and inverse)
141   double mThreshold=0.0;
142   double mReverseThreshold=1.0;
143   ComputeOptimalThresholds(map, m_Target, bins, tolerance, mThreshold, mReverseThreshold);
144
145   // Use the threshold to compute new support
146   int s1 = GetSupportSize();
147   if (mThreshold > 0.0) {
148     ImagePointer support1 = 
149       clitk::SliceBySliceRelativePosition<ImageType>(m_Support, m_Object, 2, 
150                                                      mThreshold,
151                                                      angle,false, // inverseFlag
152                                                      false,  // uniqueConnectedComponent
153                                                      -1, true, 
154                                                      false);//singleObjectCCL
155     // Compute the new support size
156     statFilter->SetInput(support1);
157     statFilter->SetLabelInput(support1);
158     statFilter->Update();
159     s1 = statFilter->GetCount(GetForegroundValue());
160   }
161   
162   int s2 = GetSupportSize();
163   if (mReverseThreshold < 1.0) {
164     ImagePointer support2 = 
165       clitk::SliceBySliceRelativePosition<ImageType>(m_Support, m_Object, 2, 
166                                                      mReverseThreshold, 
167                                                      angle,true,// inverseFlag
168                                                      false, // uniqueConnectedComponent
169                                                      -1, true, 
170                                                      false); //singleObjectCCL
171     // Compute the new support size
172     statFilter = StatFilterType::New();
173     statFilter->SetInput(support2);
174     statFilter->SetLabelInput(support2);
175     statFilter->Update();
176     s2 = statFilter->GetCount(GetForegroundValue());
177   }
178   
179   // Set results values
180   m_Info.threshold = mThreshold;
181   m_Info.sizeAfterThreshold = s1;
182   m_Info.sizeBeforeThreshold = GetSupportSize();
183   m_Info.sizeReference = GetTargetSize();
184   m_InfoReverse.threshold = mReverseThreshold;
185   m_InfoReverse.sizeAfterThreshold = s2;
186   m_InfoReverse.sizeBeforeThreshold = GetSupportSize();
187   m_InfoReverse.sizeReference = GetTargetSize();  
188 }
189 //--------------------------------------------------------------------
190
191
192 //--------------------------------------------------------------------
193 template <class ImageType>
194 typename clitk::RelativePositionAnalyzerFilter<ImageType>::FloatImageType::Pointer
195 clitk::RelativePositionAnalyzerFilter<ImageType>::
196 ComputeFuzzyMap(ImageType * object, ImageType * target, ImageType * support, double angle)
197 {
198   typedef clitk::SliceBySliceRelativePositionFilter<ImageType> SliceRelPosFilterType;
199   typedef typename SliceRelPosFilterType::FloatImageType FloatImageType;
200   typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
201   sliceRelPosFilter->VerboseStepFlagOff();
202   sliceRelPosFilter->WriteStepFlagOff();
203   sliceRelPosFilter->SetInput(support);
204   sliceRelPosFilter->SetInputObject(object);
205   sliceRelPosFilter->SetDirection(2);
206   sliceRelPosFilter->SetIntermediateSpacingFlag(false);
207   //sliceRelPosFilter->AddOrientationTypeString(orientation);
208   sliceRelPosFilter->AddAnglesInRad(angle, 0.0);
209   sliceRelPosFilter->FuzzyMapOnlyFlagOn(); // do not threshold, only compute the fuzzy map
210   // sliceRelPosFilter->PrintOptions();
211   sliceRelPosFilter->Update();
212   typename FloatImageType::Pointer map = sliceRelPosFilter->GetFuzzyMap();
213
214   // Resize map like object to allow SetBackground
215   map = clitk::ResizeImageLike<FloatImageType>(map, object, GetBackgroundValue());
216   
217   // Remove initial object from the fuzzy map
218   map = clitk::SetBackground<FloatImageType, ImageType>(map, object, GetForegroundValue(), 0.0, true);
219   
220   // Resize the fuzzy map like the target, put 2.0 when outside
221   map = clitk::ResizeImageLike<FloatImageType>(map, target, 2.0);  // Put 2.0 when out of initial map
222   
223   // end
224   return map;
225 }
226 //--------------------------------------------------------------------
227
228
229 //--------------------------------------------------------------------
230 template <class ImageType>
231 void
232 clitk::RelativePositionAnalyzerFilter<ImageType>::
233 ComputeOptimalThresholds(FloatImageType * map, ImageType * target, int bins, double tolerance, 
234                          double & threshold, double & reverseThreshold)
235 {
236   // Get the histogram of fuzzy values inside the target image
237   typedef itk::LabelStatisticsImageFilter<FloatImageType, ImageType> FloatStatFilterType;
238   typename FloatStatFilterType::Pointer f = FloatStatFilterType::New();
239   f->SetInput(map);
240   f->SetLabelInput(target);
241   f->UseHistogramsOn();
242   f->SetHistogramParameters(bins, 0.0, 1.1);
243   f->Update();
244   int count = f->GetCount(GetForegroundValue());
245   typename FloatStatFilterType::HistogramPointer h = f->GetHistogram(GetForegroundValue());
246
247   // Debug : dump histogram
248   static int i=0;
249   std::ofstream histogramFile(std::string("fuzzy_histo_"+toString(i)+".txt").c_str());
250   for(int j=0; j<bins; j++) {
251     histogramFile << h->GetMeasurement(j,0) 
252                   << "\t" << h->GetFrequency(j) 
253                   << "\t" << (double)h->GetFrequency(j)/(double)count << std::endl;
254   }
255   histogramFile.close();  
256   i++;
257
258   // Analyze the histogram (direct)
259   double sum = 0.0;
260   bool found = false;
261   threshold = 0.0;
262   for(int j=0; j<bins; j++) {
263     sum += ((double)h->GetFrequency(j)/(double)count);
264     if ((!found) && (sum > tolerance)) {
265       if (j==0) threshold = h->GetBinMin(0,j);
266       else threshold = h->GetBinMin(0,j-1); // the last before reaching the threshold
267       found = true;
268     }
269   }
270
271   // Analyze the histogram (reverse)
272   sum = 0.0;
273   found = false;
274   reverseThreshold = 1.0;
275   for(int j=bins-1; j>=0; j--) {
276     sum += ((double)h->GetFrequency(j)/(double)count);
277     if ((!found) && (sum > tolerance)) {
278       if (j==bins-1) reverseThreshold = h->GetBinMax(0,j);
279       else reverseThreshold = h->GetBinMax(0,j+1);
280       found = true;
281     }
282   }
283 }
284 //--------------------------------------------------------------------
285