Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLUtils/src/ICLUtils/FastMedianList.h ** 00010 ** Module : ICLUtils ** 00011 ** Authors: Christof Elbrechter ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <ICLUtils/CompatMacros.h> 00034 #include <string.h> 00035 #include <cmath> 00036 00037 namespace icl{ 00038 namespace utils{ 00040 00085 class FastMedianList{ 00086 public: 00088 00089 inline FastMedianList(int size=0,int t2=-1, int t3=-1, int t4=-1): 00090 m_iSize(size),m_iT2(t2),m_iT3(t3),m_iT4(t4), m_piTable(size ? new int[size]: 0){ 00091 clear(); 00092 } 00093 00095 inline FastMedianList(const FastMedianList &l): 00096 m_iSize(l.m_iSize),m_iT2(l.m_iT2),m_iT3(l.m_iT3),m_iT4(l.m_iT4){ 00097 m_piTable = m_iSize ? new int[m_iSize] : 0; 00098 clear(); 00099 } 00100 00102 inline FastMedianList &operator=(const FastMedianList &l){ 00103 m_iSize = l.m_iSize; 00104 m_iT2 = l.m_iT2; 00105 m_iT3 = l.m_iT3; 00106 m_iT4 = l.m_iT4; 00107 m_piTable = m_iSize ? new int[m_iSize] : 0; 00108 clear(); 00109 return *this; 00110 } 00111 00113 inline ~FastMedianList(){ 00114 if(m_piTable) delete [] m_piTable; 00115 } 00116 00118 inline void init(int size, int t2=-1, int t3=-1, int t4=-1){ 00119 m_iSize = size; 00120 m_iT2 = t2; 00121 m_iT3 = t3; 00122 m_iT4 = t4; 00123 if(m_piTable)delete [] m_piTable; 00124 m_piTable = new int[size]; 00125 } 00126 00128 inline int add(int index){ 00129 m_piTable[index]++; 00130 m_iCount++; 00131 return m_iCount; 00132 } 00133 00135 inline void add(int index, int val){ 00136 m_piTable[index]++; 00137 m_iCount++; 00138 if(val > m_iT2)return; 00139 m_piTable[index]++; 00140 m_iCount++; 00141 if(val > m_iT3)return; 00142 m_piTable[index]++; 00143 m_iCount++; 00144 if(val > m_iT4)return; 00145 m_piTable[index]++; 00146 m_iCount++; 00147 } 00148 00150 inline int median(){ 00151 if(m_iCount == 0)return -1; 00152 int mass=0; 00153 int count2 = m_iCount/2; 00154 for(int i=0;i<m_iSize;i++){ 00155 mass+=m_piTable[i]; 00156 if(mass > count2){ 00157 return i; 00158 } 00159 } 00160 return 0; 00161 } 00162 00164 inline void clear(){ 00165 memset(m_piTable,0,m_iSize*sizeof(int)); 00166 m_iCount=0; 00167 } 00168 00170 inline int mean(){ 00171 int m=0; 00172 00173 if (!m_iCount) return 0; 00174 00175 for(int i=0;i<m_iSize;i++){ 00176 m+=m_piTable[i]*i; 00177 } 00178 return m/m_iCount; 00179 00180 } 00181 00183 inline int variance(){ 00184 int var=0; 00185 int m = mean(); 00186 for(int i=0;i<m_iSize;i++){ 00187 var+=( m_piTable[i]*(int)pow(float(i-m),2) ); 00188 } 00189 return var/m_iCount; 00190 00191 } 00193 inline int getSize(){ 00194 return m_iCount; 00195 } 00196 00197 protected: 00199 int m_iCount; 00200 00202 int m_iSize; 00203 00205 int m_iT2; 00206 00208 int m_iT3; 00209 00211 int m_iT4; 00212 00214 int *m_piTable; 00215 }; 00216 00217 } // namespace utils 00218 } 00219