Orfeo Toolbox  3.16
itkVoronoiSegmentationImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkVoronoiSegmentationImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2009-01-27 19:30:17 $
7  Version: $Revision: 1.39 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkVoronoiSegmentationImageFilter_txx
18 #define __itkVoronoiSegmentationImageFilter_txx
20 
23 
24 
25 namespace itk
26 {
27 
28 /* constructor: seting the default value of the parameters */
29 template <class TInputImage, class TOutputImage, class TBinaryPriorImage>
30 VoronoiSegmentationImageFilter <TInputImage,TOutputImage,TBinaryPriorImage>
32 {
33  m_MeanPercentError = 0.10;
34  m_STDPercentError = 1.5;
35  m_Mean = m_STD = m_MeanTolerance = m_STDTolerance = 0.0;
36 }
37 
38 /* destructor */
39 template <class TInputImage, class TOutputImage, class TBinaryPriorImage>
41 ::~VoronoiSegmentationImageFilter()
42 {
43 }
44 
45 template <class TInputImage, class TOutputImage, class TBinaryPriorImage>
46 void
48 ::SetMeanPercentError(double x)
49 {
50  m_MeanPercentError = x;
51  m_MeanTolerance = x*m_Mean;
52 }
53 
54 
55 template <class TInputImage, class TOutputImage, class TBinaryPriorImage>
56 void
58 ::SetSTDPercentError(double x)
59 {
60  m_STDPercentError = x;
61  m_STDTolerance = x*m_STD;
62 }
63 
64 template <class TInputImage, class TOutputImage, class TBinaryPriorImage>
65 bool
67 ::TestHomogeneity(IndexList &Plist)
68 {
69  int num = static_cast<int>( Plist.size() );
70  int i;
71  double getp;
72  double addp=0;
73  double addpp=0;
74  for(i=0;i<num;i++)
75  {
76  getp = (double)(this->GetInput()->GetPixel(Plist[i]));
77  addp=addp+getp;
78  addpp=addpp+getp*getp;
79  }
80 
81  double savemean,saveSTD;
82  if(num > 1)
83  {
84  savemean = addp/num;
85  saveSTD = vcl_sqrt((addpp - (addp*addp)/(num) )/(num-1));
86  }
87  else
88  {
89  savemean = 0;
90  saveSTD = -1;
91  }
92 
93  // // jvm - Mahalanobis distance
94  // if (savevar > 0 && vcl_fabs(savemean - m_Mean) / m_Var < 2.5)
95  // return true;
96  // else
97  // return false;
98 
99  savemean -= m_Mean;
100  saveSTD -= m_STD;
101  if( (savemean>-m_MeanTolerance) && (savemean<m_MeanTolerance)
102  && (saveSTD<m_STDTolerance) )
103  {
104  return 1;
105  }
106  else
107  {
108  return 0;
109  }
110 }
111 
112 template <class TInputImage, class TOutputImage , class TBinaryPriorImage>
113 void
115 ::TakeAPrior(const BinaryObjectImage* aprior)
116 {
117  RegionType region = this->GetInput()->GetRequestedRegion();
119  itk::ImageRegionConstIteratorWithIndex <InputImageType> iit(this->GetInput(), region);
120 
121  this->m_Size = this->GetInput()->GetRequestedRegion().GetSize();
122 
123  int num=0;
124  float addp=0;
125  float addpp=0;
126  float currp;
127 
128  unsigned int i,j;
129  unsigned int minx = 0,miny = 0,maxx = 0,maxy = 0;
130  bool status=0;
131  for(i=0;i<this->m_Size[1];i++)
132  {
133  for(j=0;j<this->m_Size[0];j++)
134  {
135  if( (status==0)&&(ait.Get()) )
136  {
137  miny=i;
138  minx=j;
139  maxy=i;
140  maxx=j;
141  status=1;
142  }
143  else if( (status==1)&&(ait.Get()) )
144  {
145  maxy=i;
146  if(minx>j) minx=j;
147  if(maxx<j) maxx=j;
148  }
149  ++ait;
150  }
151  }
152 
153  float addb=0;
154  float addbb=0;
155  int numb=0;
156 
157  ait.GoToBegin();
158  iit.GoToBegin();
159  for(i=0;i<miny;i++)
160  {
161  for(j=0;j<this->m_Size[0];j++)
162  {
163  ++ait;
164  ++iit;
165  }
166  }
167  for(i=miny;i<=maxy;i++)
168  {
169  for(j=0;j<minx;j++)
170  {
171  ++ait;
172  ++iit;
173  }
174  for(j=minx;j<=maxx;j++)
175  {
176  if(ait.Get())
177  {
178  num++;
179  currp = (float)(iit.Get());
180  addp += currp;
181  addpp += currp*currp;
182  }
183  else
184  {
185  numb++;
186  currp = (float)(iit.Get());
187  addb += currp;
188  addbb += currp*currp;
189  }
190  ++ait;++iit;
191  }
192  for(j=maxx+1;j<this->m_Size[0];j++)
193  {
194  ++ait;
195  ++iit;
196  }
197  }
198 
199  m_Mean = addp/num;
200  m_STD = vcl_sqrt((addpp - (addp*addp)/num)/(num-1));
201  float b_Mean = addb/numb;
202 
203  if(this->GetUseBackgroundInAPrior())
204  {
205  m_MeanTolerance = vcl_fabs(m_Mean-b_Mean)*this->GetMeanDeviation();
206  }
207  else
208  {
209  m_MeanTolerance = m_Mean*m_MeanPercentError;
210  }
211  m_STDTolerance = m_STD*m_STDPercentError;
212 }
213 
214 template <class TInputImage, class TOutputImage, class TBinaryPriorImage>
215 void
217 ::PrintSelf(std::ostream& os, Indent indent) const
218 {
219  Superclass::PrintSelf(os, indent);
220 
221  os << indent << "Mean = " << m_Mean << std::endl;
222  os << indent << "MeanTolerance = " << m_MeanTolerance << std::endl;
223  os << indent << "MeanPercentError = " << m_MeanPercentError << std::endl;
224  os << indent << "STD = " << m_STD << std::endl;
225  os << indent << "STDTolerance = " << m_STDTolerance << std::endl;
226  os << indent << "STDPercentError = " << m_STDPercentError << std::endl;
227 }
228 
229 } //end namespace
230 
231 #endif

Generated at Sun Jun 16 2013 00:14:34 for Orfeo Toolbox with doxygen 1.8.3.1