IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
KernelKMeansCentroid.h
1 // KernelKMeansCentroid.h:
3 // -----------------------
4 //
23 
24 #ifndef __IPSDKMATH_KERNELKMEANSCENTROID_H__
25 #define __IPSDKMATH_KERNELKMEANSCENTROID_H__
26 
27 // suppression warnings
28 // warning C4251: 'ipsdk::math::KernelKMeansCentroid<KernelType>::_weights': class 'boost::numeric::ublas::vector<ipsdk::ipReal64,boost::numeric::ublas::unbounded_array<ipsdk::ipReal64,std::allocator<_Ty>>>' needs to have dll-interface to be used by clients of class 'ipsdk::math::KernelKMeansCentroid<KernelType>'
29 #pragma warning (push)
30 #pragma warning (disable : 4251)
31 
34 #include <IPSDKMath/Constants.h>
36 #include <IPSDKMath/Clustering/KernelKMeansClusteringConfig.h>
37 
38 namespace ipsdk {
39 namespace math {
40 namespace clustering {
41 
44 
45 template <typename KernelType>
46 class IPSDKMATH_API KernelKMeansCentroid
47 {
48 public:
51  KernelKMeansCentroid(const KernelType& kernel);
52  ~KernelKMeansCentroid();
54 
55 // methods
56 public:
57  // ---------------------------
60  // ---------------------------
61 
63  const KernelType& getKernel() const;
64 
67  {
69  }
70 
75  void setAccuracyThreshold(const ipReal64 accuracyThreshold);
76 
79  ipReal64 getAccuracyThreshold() const;
80 
83  {
85  }
86 
90  void setMaxDictionarySize(const ipUInt32 maxDictionarySize);
91 
93  ipUInt32 getMaxDictionarySize() const;
95 
97  ipUInt64 getNbSamples() const;
98 
100  ipUInt32 getDictionarySize() const;
101 
104  void addSample(const Vector& sample);
105 
108  IPSDK_FORCEINLINE ipReal64 comparable_distance(const Vector& sample) const
109  {
110  // compute sample term
111  const ipReal64 sampleTerm = _kernel.value(sample);
112 
113  // compute mixed term
114  ipReal64 mixedTerm = 0;
115  typename VectorColl::const_iterator iterDict = _dictionary.begin();
116  typename Real64Vector::const_iterator iterWeight = _weights.begin();
117  while (iterDict != _dictionary.end()) {
118 
119  // retrieve current point and associated weight
120  const Vector& curPoint = *iterDict;
121  const ipReal64 curWeight = *iterWeight;
122 
123  // compute associated contribution
124  mixedTerm += curWeight * _kernel.value(curPoint, sample);
125 
126  ++iterDict;
127  ++iterWeight;
128  }
129 
130  // update cluster term if needed
131  updateClusterTerm();
132 
133  // compute comparable distance
134  return sampleTerm + _clusterTermInfo.first - 2*mixedTerm;
135  }
136 
139  IPSDK_FORCEINLINE ipReal64 distance(const Vector& sample) const
140  {
141  // compute comparable distance
142  const ipReal64 comparableDistance = comparable_distance(sample);
143 
144  // compute associated distance
145  if (comparableDistance > 0)
146  return std::sqrt(comparableDistance);
147  else
148  return 0;
149  }
150 
152  ipReal64 getClusterTerm() const;
153 
155  const VectorColl& getDictionary() const;
156 
158  const Real64Vector& getWeights() const;
159 
161  void clear();
162 
163 protected:
166  void updateClusterTerm() const;
167 
170  void updateMinStrength();
171 
172 // attributes
173 protected:
175  KernelType _kernel;
176 
179 
182 
186 
189 
192 
195  mutable std::pair<ipReal64, bool> _clusterTermInfo;
196 
199 
202 
205  std::pair<ipReal64, ipUInt32> _minStrengthInfo;
206 
214 };
215 
218 
219 } // end of namespace clustering
220 } // end of namespace math
221 } // end of namespace ipsdk
222 
223 #pragma warning (pop)
224 
225 #endif // __IPSDKMATH_KERNELKMEANSCENTROID_H__
Definition of import/export macro for library.
boost::numeric::ublas::matrix< ipReal64 > Matrix
matrix type associated to library
Definition: LinearAlgebraTypes.h:48
KernelType _kernel
kernel associated to cluster dictionary
Definition: KernelKMeansCentroid.h:175
Defines the IPSDK_FORCEINLINE.
static ipReal64 getDefaultAccuracyThreshold()
function allowing to retrieve default accuracy threshold value for approximate linear dependence cond...
Definition: KernelKMeansClusteringConfig.h:100
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
ipUInt32 _maxDictionarySize
maximum dictionary size for processing
Definition: KernelKMeansCentroid.h:181
Real64Vector _weights
weight factors for each elements of dictionary
Definition: KernelKMeansCentroid.h:188
std::vector< ipReal64 > Real64Vector
stl vector collections
Definition: BaseCollections.h:37
uint64_t ipUInt64
Base types definition.
Definition: BaseTypes.h:55
Matrix _matK
K Matrix used to store approximate linear dependance system condition.
Definition: KernelKMeansCentroid.h:198
IPSDK_FORCEINLINE PackT sqrt(const PackT &in)
returns the square root of a pack
Definition: sqrt.h:40
double ipReal64
Base types definition.
Definition: BaseTypes.h:57
Predefined constants for ipsdk math library.
boost::numeric::ublas::vector< ipReal64 > Vector
vector type associated to library
Definition: LinearAlgebraTypes.h:36
Vector _wkVec
working array
Definition: KernelKMeansCentroid.h:210
std::vector< Vector > VectorColl
collection of vectors
Definition: ClusteringTypes.h:31
Matrix _wkMat
working array
Definition: KernelKMeansCentroid.h:209
#define IPSDKMATH_API
Import/Export macro for library IPSDKMath.
Definition: IPSDKMathExports.h:27
static ipUInt32 getDefaultMaxDictionarySize()
retrieve default maximum dictionary size for processing
Definition: KernelKMeansCentroid.h:82
IPSDK_FORCEINLINE ipReal64 distance(const T x1, const T y1, const T x2, const T y2)
Euclidian distance between 2d points.
Definition: Point2d.h:30
ipUInt64 _nbSamples
number of added samples
Definition: KernelKMeansCentroid.h:191
Matrix _invMatK
Inverse of K Matrix.
Definition: KernelKMeansCentroid.h:201
static ipUInt32 getDefaultMaxDictionarySize()
function allowinf to retrieve default maximum dictionary size for processing
Definition: KernelKMeansClusteringConfig.h:116
Vector _wkVecMixedTerms
working array
Definition: KernelKMeansCentroid.h:212
VectorColl _dictionary
Definition: KernelKMeansCentroid.h:185
static ipReal64 getDefaultAccuracyThreshold()
retrieve default accuracy threshold value
Definition: KernelKMeansCentroid.h:66
std::pair< ipReal64, ipUInt32 > _minStrengthInfo
Definition: KernelKMeansCentroid.h:205
Predefined types for clustering algorithm.
ipReal64 _accuracyThreshold
accuracy threshold value for approximate linear dependence condition
Definition: KernelKMeansCentroid.h:178
IPSDK_FORCEINLINE ipReal64 comparable_distance(const ipsdk::math::Vector &v0, const ipsdk::math::Vector &v1)
function allowing to compute comparable distance between two vectors
Definition: LinearAlgebraUtils.h:33
std::pair< ipReal64, bool > _clusterTermInfo
Definition: KernelKMeansCentroid.h:195
Vector _wkVecA
working array
Definition: KernelKMeansCentroid.h:211
uint32_t ipUInt32
Base types definition.
Definition: BaseTypes.h:53