Go to the documentation of this file.00001 #include <vocabulary_tree/simple_kmeans.h>
00002 #include <Eigen/Core>
00003 #include <vector>
00004 #include <cstdio>
00005
00006 int main(int argc, char** argv)
00007 {
00008 static const size_t K = 3;
00009
00010 typedef double Scalar;
00011 typedef Eigen::Matrix<Scalar, 1, K> Feature;
00012 typedef std::vector<Feature, Eigen::aligned_allocator<Feature> > FeatureVector;
00013 FeatureVector features(1000*K);
00014 FeatureVector centers;
00015 std::vector<unsigned int> membership;
00016
00017 FILE* data = fopen(argv[1], "r");
00018 for (size_t i = 0; i < features.size(); ++i) {
00019 Feature& f = features[i];
00020 fscanf(data, "%lf %lf %lf", &f[0], &f[1], &f[2]);
00021 }
00022
00023 vt::SimpleKmeans<Feature> kmeans(Feature::Zero());
00024 double sse = kmeans.cluster(features, K, centers, membership);
00025
00026 for (size_t i = 0; i < centers.size(); ++i) {
00027 printf("%f %f %f\n", centers[i][0], centers[i][1], centers[i][2]);
00028 }
00029 printf("sse = %f\n", sse);
00030
00031 unsigned int pattern[K];
00032 pattern[0] = membership[0];
00033 pattern[1] = membership[1];
00034 pattern[2] = membership[2];
00035 for (size_t i = 0; i < membership.size(); i+=K) {
00036 if (membership[i] != pattern[0] ||
00037 membership[i+1] != pattern[1] ||
00038 membership[i+2] != pattern[2]) {
00039 printf("Misassignment!\n");
00040 return -1;
00041 }
00042 }
00043 }