00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef HxNgbKuwahara_h
00010 #define HxNgbKuwahara_h
00011
00012 #include "HxClassName.h"
00013 #include "HxSizes.h"
00014 #include "HxCategories.h"
00015 #include "HxTagList.h"
00016
00017 #include <algorithm>
00018
00021 template <class ArgType, class ResType>
00022 class HxNgbKuwahara
00023 {
00024 public:
00025
00027 typedef HxTagLoop IteratorCategory;
00028
00030 typedef HxTag1Phase PhaseCategory;
00031
00032
00038
00039 HxNgbKuwahara(HxTagList& tags);
00040 ~HxNgbKuwahara();
00041
00042 HxSizes size();
00043 void init(int, int, ArgType);
00044
00045 void next(int x, int y, ArgType value);
00046 ResType result() const;
00047
00049 static HxString className();
00050
00051 private:
00052 int _pctIdx;
00053
00054 int _windowW;
00055 int _windowH;
00056
00057 ArgType* _values;
00058 int _i;
00059 };
00060
00061 template <class ArgType, class ResType>
00062 ostream&
00063 operator<<(ostream& os, const HxNgbKuwahara<ArgType, ResType>& pct)
00064 {
00065 return pct.put(os);
00066 }
00067
00068 template <class ArgType, class ResType>
00069 inline HxString
00070 HxNgbKuwahara<ArgType, ResType>::className()
00071 {
00072 static HxString s("kuwahara");
00073 return s;
00074 }
00075
00076 template <class ArgType, class ResType>
00077 inline void
00078 HxNgbKuwahara<ArgType, ResType>::init(int, int, ArgType)
00079 {
00080 _i = 0;
00081 }
00082
00083 template <class ArgType, class ResType>
00084 inline void
00085 HxNgbKuwahara<ArgType, ResType>::next(int, int, ArgType value)
00086 {
00087 _values[_i++] = value;
00088 }
00089
00090 template <class ArgType, class ResType>
00091 inline HxSizes
00092 HxNgbKuwahara<ArgType, ResType>::size()
00093 {
00094 return HxSizes(_windowW, _windowH, 1);
00095 }
00096
00097 template <class ArgType, class ResType>
00098 inline ResType
00099 HxNgbKuwahara<ArgType, ResType>::result() const
00100 {
00101 ArgType meanA, meanB, meanC,meanD, minMean;
00102 ArgType varA, varB, varC, varD, minVar;
00103
00104
00105 int i,j;
00106 const HxValue zero(HxVec3Double(0,0,0));
00107 ArgType sumA=zero,sumB=zero,sumC=zero,sumD=zero;
00108 int regW, regH;
00109 regW = _windowW/2;
00110 regH = _windowH/2;
00111 int dimReg = (regW+1)*(regH+1);
00112 HxValue dimRegV(HxVec3Double(dimReg,dimReg,dimReg));
00113
00114 for(i=0;i<=regW;i++)
00115 for(j=0;j<=regH;j++)
00116 sumA += _values[j*_windowW+i];
00117 for(i=regW;i<_windowW;i++)
00118 for(j=0;j<=regH;j++)
00119 sumB += _values[j*_windowW+i];
00120 for(i=0;i<=regW;i++)
00121 for(j=regH;j<_windowH;j++)
00122 sumC += _values[j*_windowW+i];
00123 for(i=regW;i<_windowW;i++)
00124 for(j=regH;j<_windowH;j++)
00125 sumD += _values[j*_windowW+i];
00126 meanA = sumA/dimRegV;
00127 meanB = sumB/dimRegV;
00128 meanC = sumC/dimRegV;
00129 meanD = sumD/dimRegV;
00130
00131
00132 sumA=zero; sumB=zero; sumC=zero; sumD=zero;
00133 for(i=0;i<=regW;i++)
00134 for(j=0;j<=regH;j++)
00135 sumA += (_values[j*_windowW+i]-meanA)*(_values[j*_windowW+i]-meanA);
00136 for(i=regW;i<_windowW;i++)
00137 for(j=0;j<=regH;j++)
00138 sumB += (_values[j*_windowW+i]-meanB)*(_values[j*_windowW+i]-meanB);
00139 for(i=0;i<=regW;i++)
00140 for(j=regH;j<_windowH;j++)
00141 sumC += (_values[j*_windowW+i]-meanC)*(_values[j*_windowW+i]-meanC);
00142 for(i=regW;i<_windowW;i++)
00143 for(j=regH;j<_windowH;j++)
00144 sumD += (_values[j*_windowW+i]-meanD)*(_values[j*_windowW+i]-meanD);
00145 varA = sumA/dimRegV;
00146 varB = sumB/dimRegV;
00147 varC = sumC/dimRegV;
00148 varD = sumD/dimRegV;
00149
00150 minVar = ArgType::LARGE_VAL;
00151 if(varA<minVar) { minVar=varA; minMean = meanA;}
00152 if(varB<minVar) { minVar=varB; minMean = meanB;}
00153 if(varC<minVar) { minVar=varC; minMean = meanC;}
00154 if(varD<minVar) { minVar=varD; minMean = meanD;}
00155
00156
00157 return minMean;
00158 }
00159
00160 #ifdef INC_TEMPLATE_SRC
00161 #include "HxNgbKuwahara.c"
00162 #endif
00163
00164
00165 #endif