-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfuzzy_clustering.hpp
111 lines (90 loc) · 2.67 KB
/
fuzzy_clustering.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef FUZZY_CLUSTERING
#define FUZZY_CLUSTERING
#include <iostream>
//#include <cxcore.h>
#include <opencv2/core/core.hpp>
#include <time.h>
#include <stdlib.h>
typedef enum {
kSoftCInitRandom = 0,
kSoftCInitKmeansPP,
} SoftCInitType;
typedef enum {
kSoftCDistL1 = 0,
kSoftCDistL2,
kSoftCDistHistInter, // Not implemented
} SoftCDistType;
namespace SoftC {
typedef float Value;
typedef unsigned int Index;
class Fuzzy {
public:
Fuzzy (
const cv::Mat &rows,
const int number_clusters,
const float fuzziness,
const float epsilon,
const SoftCDistType dist_type,
const SoftCInitType init_type
)
:
fuzziness_ (fuzziness),
epsilon_ (epsilon),
dist_type_ (dist_type),
init_type_ (init_type),
number_clusters_ (number_clusters),
number_points_ (rows.rows),
dimension_ (rows.cols),
rows_ (rows)
{
centroids_ = cv::Mat::zeros (number_clusters_, dimension_, CV_32FC1);
membership_
= cv::Mat::zeros (number_points_, number_clusters_, CV_32FC1);
new_membership_
= cv::Mat::zeros (number_points_, number_clusters_, CV_32FC1);
initEverything ();
};
void initEverything ();
void initRandom ();
void initKmeansPP ();
void computeCentroids ();
void computeCentroids2 ();
bool updateMembership ();
float calc_dist (
const cv::Mat &point,
const cv::Mat ¢er,
const SoftCDistType dist_type);
inline const bool can_stop ()
{
float t = cv::norm (membership_ - new_membership_);
return t < epsilon_;
}
//
// clustering
// このnum_iterationは何度も初期化してやり直すという意味ではなく
// 単に何ステップ行うか,というだけ
//
inline void clustering (const unsigned int num_iteration = 10000) {
unsigned int iteration = 0;
while (!updateMembership () && iteration++ < num_iteration) {
computeCentroids2();
}
}
inline const cv::Mat get_centroids_ () { return centroids_; }
inline const cv::Mat get_membership_ () { return membership_; }
inline const cv::Mat get_new_membership_ () { return new_membership_; }
private:
float fuzziness_;
float epsilon_;
int number_clusters_;
int number_points_;
int dimension_;
SoftCDistType dist_type_;
SoftCInitType init_type_;
cv::Mat centroids_;
cv::Mat membership_;
cv::Mat new_membership_;
cv::Mat rows_;
};
};
#endif