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.GPL ** 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 <string.h> 00034 #include <cmath> 00035 namespace icl{ 00036 namespace utils{ 00038 00083 class FastMedianList{ 00084 public: 00086 00087 inline FastMedianList(int size=0,int t2=-1, int t3=-1, int t4=-1): 00088 m_iSize(size),m_iT2(t2),m_iT3(t3),m_iT4(t4), m_piTable(size ? new int[size]: 0){ 00089 clear(); 00090 } 00091 00093 inline FastMedianList(const FastMedianList &l): 00094 m_iSize(l.m_iSize),m_iT2(l.m_iT2),m_iT3(l.m_iT3),m_iT4(l.m_iT4){ 00095 m_piTable = m_iSize ? new int[m_iSize] : 0; 00096 clear(); 00097 } 00098 00100 inline FastMedianList &operator=(const FastMedianList &l){ 00101 m_iSize = l.m_iSize; 00102 m_iT2 = l.m_iT2; 00103 m_iT3 = l.m_iT3; 00104 m_iT4 = l.m_iT4; 00105 m_piTable = m_iSize ? new int[m_iSize] : 0; 00106 clear(); 00107 return *this; 00108 } 00109 00111 inline ~FastMedianList(){ 00112 if(m_piTable) delete [] m_piTable; 00113 } 00114 00116 inline void init(int size, int t2=-1, int t3=-1, int t4=-1){ 00117 m_iSize = size; 00118 m_iT2 = t2; 00119 m_iT3 = t3; 00120 m_iT4 = t4; 00121 if(m_piTable)delete [] m_piTable; 00122 m_piTable = new int[size]; 00123 } 00124 00126 inline int add(int index){ 00127 m_piTable[index]++; 00128 m_iCount++; 00129 return m_iCount; 00130 } 00131 00133 inline void add(int index, int val){ 00134 m_piTable[index]++; 00135 m_iCount++; 00136 if(val > m_iT2)return; 00137 m_piTable[index]++; 00138 m_iCount++; 00139 if(val > m_iT3)return; 00140 m_piTable[index]++; 00141 m_iCount++; 00142 if(val > m_iT4)return; 00143 m_piTable[index]++; 00144 m_iCount++; 00145 } 00146 00148 inline int median(){ 00149 if(m_iCount == 0)return -1; 00150 int mass=0; 00151 int count2 = m_iCount/2; 00152 for(int i=0;i<m_iSize;i++){ 00153 mass+=m_piTable[i]; 00154 if(mass > count2){ 00155 return i; 00156 } 00157 } 00158 return 0; 00159 } 00160 00162 inline void clear(){ 00163 memset(m_piTable,0,m_iSize*sizeof(int)); 00164 m_iCount=0; 00165 } 00166 00168 inline int mean(){ 00169 int m=0; 00170 00171 if (!m_iCount) return 0; 00172 00173 for(int i=0;i<m_iSize;i++){ 00174 m+=m_piTable[i]*i; 00175 } 00176 return m/m_iCount; 00177 00178 } 00179 00181 inline int variance(){ 00182 int var=0; 00183 int m = mean(); 00184 for(int i=0;i<m_iSize;i++){ 00185 var+=( m_piTable[i]*(int)pow(float(i-m),2) ); 00186 } 00187 return var/m_iCount; 00188 00189 } 00191 inline int getSize(){ 00192 return m_iCount; 00193 } 00194 00195 protected: 00197 int m_iCount; 00198 00200 int m_iSize; 00201 00203 int m_iT2; 00204 00206 int m_iT3; 00207 00209 int m_iT4; 00210 00212 int *m_piTable; 00213 }; 00214 00215 } // namespace utils 00216 } 00217