]> Creatis software - clitk.git/blob - itk/clitkRelativePositionAnalyzerFilter.txx
8313766347c19f930c5415224855674eae12cebb
[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   static const unsigned int dim = ImageType::ImageDimension;
105   
106   ImagePointer temp = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(0));
107   m_Object = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(1));
108   m_Target = dynamic_cast<ImageType*>(itk::ProcessObject::GetInput(2));
109
110   // Remove object from support (keep initial image)
111   m_Support = clitk::Clone<ImageType>(temp);
112   clitk::AndNot<ImageType>(m_Support, m_Object, GetBackgroundValue());
113   
114   // Define filter to compute statics on mask image
115   typedef itk::LabelStatisticsImageFilter<ImageType, ImageType> StatFilterType;
116   typename StatFilterType::Pointer statFilter = StatFilterType::New();
117
118   // Compute the initial support size
119   statFilter->SetInput(m_Support);
120   statFilter->SetLabelInput(m_Support);
121   statFilter->Update();
122   SetSupportSize(statFilter->GetCount(GetForegroundValue()));
123   // DD(GetSupportSize());
124   
125   // Compute the initial target size
126   ImagePointer s = clitk::ResizeImageLike<ImageType>(m_Support, m_Target, GetBackgroundValue());
127   statFilter->SetInput(s);
128   statFilter->SetLabelInput(m_Target);
129   statFilter->Update();
130   SetTargetSize(statFilter->GetCount(GetForegroundValue()));
131   // DD(GetTargetSize());
132
133   //
134   int bins = GetNumberOfBins();
135   double tolerance = GetAreaLossTolerance();
136
137   // Compute Fuzzy map
138   double angle = GetDirection().angle1;
139   typename FloatImageType::Pointer map = ComputeFuzzyMap(m_Object, m_Target, m_Support, angle);
140   writeImage<FloatImageType>(map, "fuzzy_"+toString(clitk::rad2deg(angle))+".mha");
141
142   // Compute the optimal thresholds (direct and inverse)
143   double mThreshold=0.0;
144   double mReverseThreshold=1.0;
145   ComputeOptimalThresholds(map, m_Target, bins, tolerance, mThreshold, mReverseThreshold);
146
147   // Use the threshold to compute new support
148   int s1 = GetSupportSize();
149   if (mThreshold > 0.0) {
150     ImagePointer support1 = 
151       clitk::SliceBySliceRelativePosition<ImageType>(m_Support, m_Object, 2, 
152                                                      mThreshold,
153                                                      angle,false, // inverseFlag
154                                                      false,  // uniqueConnectedComponent
155                                                      -1, true, 
156                                                      false);//singleObjectCCL
157     // Compute the new support size
158     statFilter->SetInput(support1);
159     statFilter->SetLabelInput(support1);
160     statFilter->Update();
161     s1 = statFilter->GetCount(GetForegroundValue());
162   }
163   
164   int s2 = GetSupportSize();
165   if (mReverseThreshold < 1.0) {
166     ImagePointer support2 = 
167       clitk::SliceBySliceRelativePosition<ImageType>(m_Support, m_Object, 2, 
168                                                      mReverseThreshold, 
169                                                      angle,true,// inverseFlag
170                                                      false, // uniqueConnectedComponent
171                                                      -1, true, 
172                                                      false); //singleObjectCCL
173     // Compute the new support size
174     statFilter = StatFilterType::New();
175     statFilter->SetInput(support2);
176     statFilter->SetLabelInput(support2);
177     statFilter->Update();
178     s2 = statFilter->GetCount(GetForegroundValue());
179   }
180   
181   // Set results values
182   m_Info.threshold = mThreshold;
183   m_Info.sizeAfterThreshold = s1;
184   m_Info.sizeBeforeThreshold = GetSupportSize();
185   m_Info.sizeReference = GetTargetSize();
186   m_InfoReverse.threshold = mReverseThreshold;
187   m_InfoReverse.sizeAfterThreshold = s2;
188   m_InfoReverse.sizeBeforeThreshold = GetSupportSize();
189   m_InfoReverse.sizeReference = GetTargetSize();  
190 }
191 //--------------------------------------------------------------------
192
193
194 //--------------------------------------------------------------------
195 template <class ImageType>
196 typename clitk::RelativePositionAnalyzerFilter<ImageType>::FloatImageType::Pointer
197 clitk::RelativePositionAnalyzerFilter<ImageType>::
198 ComputeFuzzyMap(ImageType * object, ImageType * target, ImageType * support, double angle)
199 {
200   typedef clitk::SliceBySliceRelativePositionFilter<ImageType> SliceRelPosFilterType;
201   typedef typename SliceRelPosFilterType::FloatImageType FloatImageType;
202   typename SliceRelPosFilterType::Pointer sliceRelPosFilter = SliceRelPosFilterType::New();
203   sliceRelPosFilter->VerboseStepFlagOff();
204   sliceRelPosFilter->WriteStepFlagOff();
205   sliceRelPosFilter->SetInput(support);
206   sliceRelPosFilter->SetInputObject(object);
207   sliceRelPosFilter->SetDirection(2);
208   sliceRelPosFilter->SetIntermediateSpacingFlag(false);
209   //sliceRelPosFilter->AddOrientationTypeString(orientation);
210   sliceRelPosFilter->AddAnglesInRad(angle, 0.0);
211   sliceRelPosFilter->FuzzyMapOnlyFlagOn(); // do not threshold, only compute the fuzzy map
212   // sliceRelPosFilter->PrintOptions();
213   sliceRelPosFilter->Update();
214   typename FloatImageType::Pointer map = sliceRelPosFilter->GetFuzzyMap();
215
216   // Resize map like object to allow SetBackground
217   map = clitk::ResizeImageLike<FloatImageType>(map, object, GetBackgroundValue());
218   
219   // Remove initial object from the fuzzy map
220   map = clitk::SetBackground<FloatImageType, ImageType>(map, object, GetForegroundValue(), 0.0, true);
221   
222   // Resize the fuzzy map like the target, put 2.0 when outside
223   map = clitk::ResizeImageLike<FloatImageType>(map, target, 2.0);  // Put 2.0 when out of initial map
224   
225   // end
226   return map;
227 }
228 //--------------------------------------------------------------------
229
230
231 //--------------------------------------------------------------------
232 template <class ImageType>
233 void
234 clitk::RelativePositionAnalyzerFilter<ImageType>::
235 ComputeOptimalThresholds(FloatImageType * map, ImageType * target, int bins, double tolerance, 
236                          double & threshold, double & reverseThreshold)
237 {
238   // Get the histogram of fuzzy values inside the target image
239   typedef itk::LabelStatisticsImageFilter<FloatImageType, ImageType> FloatStatFilterType;
240   typename FloatStatFilterType::Pointer f = FloatStatFilterType::New();
241   f->SetInput(map);
242   f->SetLabelInput(target);
243   f->UseHistogramsOn();
244   f->SetHistogramParameters(bins, 0.0, 1.1);
245   f->Update();
246   int count = f->GetCount(GetForegroundValue());
247   typename FloatStatFilterType::HistogramPointer h = f->GetHistogram(GetForegroundValue());
248
249   // Debug : dump histogram
250   static int i=0;
251   std::ofstream histogramFile(std::string("fuzzy_histo_"+toString(i)+".txt").c_str());
252   for(int j=0; j<bins; j++) {
253     histogramFile << h->GetMeasurement(j,0) 
254                   << "\t" << h->GetFrequency(j) 
255                   << "\t" << (double)h->GetFrequency(j)/(double)count << std::endl;
256   }
257   histogramFile.close();  
258   i++;
259
260   // Analyze the histogram (direct)
261   double sum = 0.0;
262   bool found = false;
263   threshold = 0.0;
264   for(int j=0; j<bins; j++) {
265     sum += ((double)h->GetFrequency(j)/(double)count);
266     if ((!found) && (sum > tolerance)) {
267       if (j==0) threshold = h->GetBinMin(0,j);
268       else threshold = h->GetBinMin(0,j-1); // the last before reaching the threshold
269       found = true;
270     }
271   }
272
273   // Analyze the histogram (reverse)
274   sum = 0.0;
275   found = false;
276   reverseThreshold = 1.0;
277   for(int j=bins-1; j>=0; j--) {
278     sum += ((double)h->GetFrequency(j)/(double)count);
279     if ((!found) && (sum > tolerance)) {
280       if (j==bins-1) reverseThreshold = h->GetBinMax(0,j);
281       else reverseThreshold = h->GetBinMax(0,j+1);
282       found = true;
283     }
284   }
285 }
286 //--------------------------------------------------------------------
287