00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __itkRankHistogram_h
00020 #define __itkRankHistogram_h
00021 #include "itkNumericTraits.h"
00022
00023 namespace itk {
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <sstream>
00034
00035 template <class TInputPixel>
00036 class RankHistogram
00037 {
00038 public:
00039 RankHistogram()
00040 {
00041 m_Rank = 0.5;
00042 }
00043 virtual ~RankHistogram(){}
00044
00045 virtual RankHistogram * Clone() const
00046 {
00047 return 0;
00048 }
00049
00050 virtual void AddPixel(const TInputPixel &itkNotUsed(p)){}
00051
00052 virtual void RemovePixel(const TInputPixel &itkNotUsed(p)){}
00053
00054 void AddBoundary(){}
00055
00056 void RemoveBoundary(){}
00057
00058 virtual TInputPixel GetValue( const TInputPixel & ){return 0;}
00059
00060 void SetRank(float rank)
00061 {
00062 m_Rank = rank;
00063 }
00064
00065 protected:
00066 float m_Rank;
00067 };
00068
00069 template <class TInputPixel, class TCompare>
00070 class RankHistogramMap : public RankHistogram<TInputPixel>
00071 {
00072 public:
00073
00074 typedef RankHistogram<TInputPixel> Superclass;
00075
00076 public:
00077 RankHistogramMap()
00078 {
00079 m_Below = m_Entries = 0;
00080
00081 m_Initialized = false;
00082 if( m_Compare( NumericTraits< TInputPixel >::max(),
00083 NumericTraits< TInputPixel >::NonpositiveMin() ) )
00084 {
00085 m_InitVal = NumericTraits< TInputPixel >::NonpositiveMin();
00086 }
00087 else
00088 {
00089 m_InitVal = NumericTraits< TInputPixel >::max();
00090 }
00091 m_RankValue = m_InitVal;
00092 m_RankIt = m_Map.begin();
00093 }
00094 ~RankHistogramMap()
00095 {
00096 }
00097
00098 void AddPixel(const TInputPixel &p)
00099 {
00100 m_Map[ p ]++;
00101 ++m_Entries;
00102 if (!m_Initialized)
00103 {
00104 m_Initialized = true;
00105 m_RankIt = m_Map.begin();
00106 m_RankValue = p;
00107 }
00108 if (m_Compare(p, m_RankValue) || p == m_RankValue)
00109 {
00110 ++m_Below;
00111 }
00112
00113 }
00114
00115 void RemovePixel(const TInputPixel &p)
00116 {
00117 m_Map[ p ]--;
00118 if (m_Compare(p, m_RankValue) || p == m_RankValue)
00119 {
00120 --m_Below;
00121 }
00122 --m_Entries;
00123 }
00124
00125 void Initialize()
00126 {
00127 m_RankIt = m_Map.begin();
00128 }
00129
00130 TInputPixel GetValue( const TInputPixel & )
00131 {
00132 unsigned long target = (int)(this->m_Rank * (m_Entries-1)) + 1;
00133 unsigned long total = m_Below;
00134 unsigned long ThisBin;
00135 bool eraseFlag = false;
00136
00137
00138 assert(m_Initialized);
00139
00140 if (total < target)
00141 {
00142 typename MapType::iterator searchIt = m_RankIt;
00143 typename MapType::iterator eraseIt;
00144
00145 while (searchIt != m_Map.end())
00146 {
00147
00148
00149
00150 ++searchIt;
00151 ThisBin = searchIt->second;
00152 total += ThisBin;
00153 if (eraseFlag)
00154 {
00155 m_Map.erase(eraseIt);
00156 eraseFlag = false;
00157 }
00158 if (ThisBin <= 0)
00159 {
00160 eraseFlag = true;
00161 eraseIt = searchIt;
00162 }
00163 if (total >= target)
00164 break;
00165 }
00166 m_RankValue = searchIt->first;
00167 m_RankIt = searchIt;
00168 }
00169 else
00170 {
00171 typename MapType::iterator searchIt = m_RankIt;
00172 typename MapType::iterator eraseIt;
00173
00174 while(searchIt != m_Map.begin())
00175 {
00176 ThisBin = searchIt->second;
00177 unsigned int tbelow = total - ThisBin;
00178 if (tbelow < target)
00179 break;
00180 if (eraseFlag)
00181 {
00182 m_Map.erase(eraseIt);
00183 eraseFlag = false;
00184 }
00185 if (ThisBin <= 0)
00186 {
00187 eraseIt = searchIt;
00188 eraseFlag = true;
00189 }
00190 total = tbelow;
00191
00192
00193 --searchIt;
00194 }
00195 m_RankValue = searchIt->first;
00196 m_RankIt = searchIt;
00197 }
00198
00199 m_Below = total;
00200
00201 return(m_RankValue);
00202
00203 }
00204
00205 Superclass * Clone() const
00206 {
00207 RankHistogramMap *result = new RankHistogramMap();
00208 result->m_Map = this->m_Map;
00209 result->m_Rank = this->m_Rank;
00210 result->m_Below = this->m_Below;
00211 result->m_Entries = this->m_Entries;
00212 result->m_InitVal = this->m_InitVal;
00213 result->m_RankValue = this->m_RankValue;
00214 result->m_Initialized = this->m_Initialized;
00215 if (result->m_Initialized)
00216 result->m_RankIt = result->m_Map.find(this->m_RankValue);
00217 return(result);
00218 }
00219
00220 private:
00221 typedef typename std::map< TInputPixel, unsigned long, TCompare > MapType;
00222
00223 MapType m_Map;
00224 unsigned long m_Below;
00225 unsigned long m_Entries;
00226 TInputPixel m_RankValue;
00227 TInputPixel m_InitVal;
00228 TCompare m_Compare;
00229 bool m_Initialized;
00230
00231 typename MapType::iterator m_RankIt;
00232 };
00233
00234 template <class TInputPixel, class TCompare>
00235 class RankHistogramVec : public RankHistogram<TInputPixel>
00236 {
00237 public:
00238 typedef RankHistogram<TInputPixel> Superclass;
00239
00240 public:
00241 RankHistogramVec()
00242 {
00243 m_Size = static_cast<unsigned int>( NumericTraits< TInputPixel >::max() -
00244 NumericTraits< TInputPixel >::NonpositiveMin() + 1 );
00245 m_Vec.resize(m_Size, 0 );
00246 if( m_Compare( NumericTraits< TInputPixel >::max(),
00247 NumericTraits< TInputPixel >::NonpositiveMin() ) )
00248 {
00249 m_InitVal = NumericTraits< TInputPixel >::NonpositiveMin();
00250 }
00251 else
00252 {
00253 m_InitVal = NumericTraits< TInputPixel >::max();
00254 }
00255 m_Entries = m_Below = 0;
00256 m_RankValue = m_InitVal - NumericTraits< TInputPixel >::NonpositiveMin();
00257 }
00258
00259 RankHistogramVec(bool NoInit)
00260 {
00261 m_Size = static_cast<unsigned int>( NumericTraits< TInputPixel >::max() -
00262 NumericTraits< TInputPixel >::NonpositiveMin() + 1 );
00263 if (!NoInit) m_Vec.resize(m_Size, 0 );
00264 if( m_Compare( NumericTraits< TInputPixel >::max(),
00265 NumericTraits< TInputPixel >::NonpositiveMin() ) )
00266 {
00267 m_InitVal = NumericTraits< TInputPixel >::NonpositiveMin();
00268 }
00269 else
00270 {
00271 m_InitVal = NumericTraits< TInputPixel >::max();
00272 }
00273 m_Entries = m_Below = 0;
00274 m_RankValue = m_InitVal - NumericTraits< TInputPixel >::NonpositiveMin();
00275 }
00276
00277
00278 ~RankHistogramVec()
00279 {
00280 }
00281
00282 TInputPixel GetValue( const TInputPixel & )
00283 {
00284 unsigned long target = (int)(this->m_Rank * (m_Entries-1)) + 1;
00285 unsigned long total = m_Below;
00286 long unsigned int pos = (long unsigned int)(m_RankValue - NumericTraits< TInputPixel >::NonpositiveMin());
00287
00288 if (total < target)
00289 {
00290 while (pos < m_Size)
00291 {
00292 ++pos;
00293 total += m_Vec[pos];
00294 if (total >= target)
00295 break;
00296 }
00297 }
00298 else
00299 {
00300 while(pos > 0)
00301 {
00302 unsigned int tbelow = total - m_Vec[pos];
00303 if (tbelow < target)
00304 break;
00305 total = tbelow;
00306 --pos;
00307 }
00308 }
00309
00310 m_RankValue = (TInputPixel)(pos + NumericTraits< TInputPixel >::NonpositiveMin());
00311 m_Below = total;
00312 return(m_RankValue);
00313 }
00314
00315 void AddPixel(const TInputPixel &p)
00316 {
00317 long unsigned int idx = (long unsigned int)(p - NumericTraits< TInputPixel >::NonpositiveMin());
00318 m_Vec[ idx ]++;
00319 if (m_Compare(p, m_RankValue) || p == m_RankValue)
00320 {
00321 ++m_Below;
00322 }
00323 ++m_Entries;
00324 }
00325
00326 void RemovePixel(const TInputPixel &p)
00327 {
00328 assert(p - NumericTraits< TInputPixel >::NonpositiveMin() >= 0);
00329 assert(p - NumericTraits< TInputPixel >::NonpositiveMin() < (int)m_Vec.size());
00330 assert(m_Entries >= 1);
00331 m_Vec[ (long unsigned int)(p - NumericTraits< TInputPixel >::NonpositiveMin()) ]--;
00332 --m_Entries;
00333
00334 if (m_Compare(p, m_RankValue) || p == m_RankValue)
00335 {
00336 --m_Below;
00337 }
00338 }
00339
00340 Superclass * Clone() const
00341 {
00342 RankHistogramVec *result = new RankHistogramVec(true);
00343 result->m_Vec = this->m_Vec;
00344 result->m_Size = this->m_Size;
00345
00346 result->m_InitVal = this->m_InitVal;
00347 result->m_Entries = this->m_Entries;
00348 result->m_Below = this->m_Below;
00349 result->m_Rank = this->m_Rank;
00350 result->m_RankValue = this->m_RankValue;
00351 return(result);
00352 }
00353
00354 private:
00355 typedef typename std::vector<unsigned long> VecType;
00356
00357 VecType m_Vec;
00358 unsigned int m_Size;
00359 TCompare m_Compare;
00360
00361
00362 TInputPixel m_RankValue;
00363 TInputPixel m_InitVal;
00364 int m_Below;
00365 int m_Entries;
00366 };
00367
00368 }
00369 #endif
00370