17 #ifndef __itkVoronoiSegmentationRGBImageFilter_txx
18 #define __itkVoronoiSegmentationRGBImageFilter_txx
29 template <
class TInputImage,
class TOutputImage>
30 VoronoiSegmentationRGBImageFilter <TInputImage,TOutputImage>::
31 VoronoiSegmentationRGBImageFilter(){
37 m_MeanTolerance[i] = 10;
38 m_STDTolerance[i] = 10;
39 m_MeanPercentError[i] = 0.10;
40 m_STDPercentError[i] = 1.5;
49 m_MaxValueOfRGB = 256;
53 template <
class TInputImage,
class TOutputImage>
55 ~VoronoiSegmentationRGBImageFilter()
59 template <
class TInputImage,
class TOutputImage>
62 SetMeanPercentError(
double x[6])
64 for(
unsigned int i=0;i<6;i++)
66 m_MeanPercentError[i] = x[i];
67 m_MeanTolerance[i] = vcl_fabs(x[i]*m_Mean[i]);
71 template <
class TInputImage,
class TOutputImage>
74 SetSTDPercentError(
double x[6])
76 for(
unsigned int i=0;i<6;i++)
78 m_STDPercentError[i] = x[i];
79 m_STDTolerance[i] = x[i]*m_STD[i];
85 template <
class TInputImage,
class TOutputImage>
90 this->Superclass::SetInput(inputNumber,input);
95 template <
class TInputImage,
class TOutputImage>
101 this->Superclass::SetInput(input);
103 this->SetSize(this->GetInput()->GetLargestPossibleRegion().GetSize());
107 region.SetSize(this->GetSize());
108 region.SetIndex(index);
110 m_WorkingImage=RGBHCVImage::New();
111 m_WorkingImage->SetLargestPossibleRegion( region );
112 m_WorkingImage->SetBufferedRegion( region );
113 m_WorkingImage->SetRequestedRegion( region );
114 m_WorkingImage->Allocate();
127 double X0 = m_MaxValueOfRGB*0.982;
128 double Y0 = m_MaxValueOfRGB;
129 double Z0 = m_MaxValueOfRGB*1.183;
134 wpixel[0] = ipixel[0];
135 wpixel[1] = ipixel[1];
136 wpixel[2] = ipixel[2];
138 X = 0.607*ipixel[0] + 0.174*ipixel[1] + 0.201*ipixel[2];
139 Y = 0.299*ipixel[0] + 0.587*ipixel[1] + 0.114*ipixel[2];
140 Z = 0.066*ipixel[1] + 1.117*ipixel[2];
141 X = vcl_pow((X/X0),0.3333);
142 Y = vcl_pow((Y/Y0),0.3333);
143 Z = vcl_pow((Z/Z0),0.3333);
150 wpixel[3] = vcl_atan2(b,a);
156 wpixel[4] = vcl_sqrt(a*a+b*b);
164 template <
class TInputImage,
class TOutputImage>
169 int num=Plist.size();
172 double addp[6]={0,0,0,0,0,0};
173 double addpp[6]={0,0,0,0,0,0};
176 getp = m_WorkingImage->GetPixel(Plist[i]);
179 addp[j]=addp[j]+getp[j];
180 addpp[j]=addpp[j]+getp[j]*getp[j];
184 double savemean[6],saveSTD[6];
189 savemean[i] = addp[i]/num;
190 saveSTD[i] = vcl_sqrt((addpp[i] - (addp[i]*addp[i])/(num) )/(num-1));
206 while (ok && (j < 3))
208 savem = savemean[m_TestMean[j]] - m_Mean[m_TestMean[j]];
209 savev = saveSTD[m_TestSTD[j]] - m_STD[m_TestSTD[j]];
210 if( (savem < -m_MeanTolerance[m_TestMean[j]]) ||
211 (savem > m_MeanTolerance[m_TestMean[j]]) )
215 if( (savev < -m_STDTolerance[m_TestSTD[j]]) ||
216 (savev > m_STDTolerance[m_TestSTD[j]]) )
229 template <
class TInputImage,
class TOutputImage>
235 RegionType region = this->GetInput()->GetRequestedRegion();
239 unsigned int minx=0,miny=0,maxx=0,maxy=0;
241 for(
unsigned int i=0;i<this->GetSize()[1];i++)
243 for(
unsigned int j=0;j<this->GetSize()[0];j++)
245 if( (status==0)&&(ait.
Get()) )
253 else if( (status==1)&&(ait.
Get()) )
266 float objaddp[6] = {0.0,0.0,0.0,0.0,0.0,0.0};
267 float objaddpp[6] = {0.0,0.0,0.0,0.0,0.0,0.0};
268 float bkgaddp[6] = {0.0,0.0,0.0,0.0,0.0,0.0};
269 float bkgaddpp[6] = {0.0,0.0,0.0,0.0,0.0,0.0};
275 for(
unsigned int i=0;i<miny;i++)
277 for(
unsigned int j=0;j<this->GetSize()[0];j++)
283 for(
unsigned int i=miny;i<=maxy;i++)
285 for(
unsigned int j=0;j<minx;j++)
290 for(
unsigned int j=minx;j<=maxx;j++)
298 objaddp[k] += currp[k];
299 objaddpp[k] += currp[k]*currp[k];
307 bkgaddp[k] += currp[k];
308 bkgaddpp[k] += currp[k]*currp[k];
313 for(
unsigned int j=maxx+1;j<this->GetSize()[0];j++)
324 for(
unsigned int i=0;i<6;i++)
326 m_Mean[i] = objaddp[i]/objnum;
327 m_STD[i] = vcl_sqrt((objaddpp[i] - (objaddp[i]*objaddp[i])/objnum)/(objnum-1));
328 m_STDTolerance[i] = m_STD[i]*m_STDPercentError[i];
329 b_Mean[i] = bkgaddp[i]/bkgnum;
330 b_STD[i] = vcl_sqrt((bkgaddpp[i] - (bkgaddp[i]*bkgaddp[i])/bkgnum)/(bkgnum-1));
331 diffMean[i] = (b_Mean[i]-m_Mean[i])/m_Mean[i];
332 if(diffMean[i] < 0) diffMean[i] = -diffMean[i];
333 diffSTD[i] = (b_STD[i]-m_STD[i])/m_STD[i];
334 if(diffSTD[i] < 0) diffSTD[i] = -diffSTD[i];
335 if(this->GetUseBackgroundInAPrior())
337 m_MeanTolerance[i] = diffMean[i]*m_Mean[i]*this->GetMeanDeviation();
341 m_MeanTolerance[i] = vcl_fabs(m_Mean[i]*m_MeanPercentError[i]);
348 for(
unsigned int i=0;i<6;i++)
350 m_MeanTolerance[i] = 0;
351 m_STDTolerance[i] = 0;
356 unsigned char tmp[6]={0,1,2,3,4,5};
357 for(
unsigned j=0;j<3;j++)
360 for(
unsigned int i=1;i<6-j;i++)
362 if(diffMean[tmp[i]]>diffMean[tmp[k]])
367 m_TestMean[j]=tmp[k];
370 unsigned char tmp1[6]={0,1,2,3,4,5};
371 for(
unsigned int j=0;j<3;j++)
374 for(
unsigned int i=1;i<6-j;i++)
376 if(diffSTD[tmp1[i]]>diffSTD[tmp1[k]])
381 m_TestSTD[j]=tmp1[k];
385 template <
class TInputImage,
class TOutputImage>
388 ::PrintSelf(std::ostream& os,
Indent indent)
const
390 Superclass::PrintSelf(os, indent);
391 os << indent <<
"MaxValueOfRGB: " << m_MaxValueOfRGB << std::endl;
392 os << indent <<
"Mean: " << m_Mean << std::endl;