00001 #include <vocabulary_tree/simple_kmeans.h> 00002 #include <Eigen/Core> 00003 #include <vector> 00004 #include <cstdio> 00005 #include <fstream> 00006 00007 int main(int argc, char** argv) 00008 { 00009 static const size_t K = 5; 00010 typedef Eigen::Vector2f Feature; 00011 typedef std::vector<Feature, Eigen::aligned_allocator<Feature> > FeatureVector; 00012 00013 FeatureVector features; 00014 FeatureVector centers; 00015 std::vector<unsigned int> membership; 00016 00017 std::ifstream infile(argv[1]); 00018 while (infile.good()) { 00019 Feature f; 00020 infile >> f[0] >> f[1]; 00021 if (infile.good()) 00022 features.push_back(f); 00023 } 00024 00025 vt::SimpleKmeans<Feature> kmeans(Feature::Zero()); 00026 #if 1 00027 kmeans.setRestarts(3); 00028 #else 00029 kmeans.setInitMethod(vt::InitGiven()); 00030 centers.reserve(5); 00031 centers.push_back(Feature(1000, 0)); 00032 centers.push_back(Feature(306, 963)); 00033 centers.push_back(Feature(-812, 594)); 00034 centers.push_back(Feature(-812, -594)); 00035 centers.push_back(Feature(306, -963)); 00036 #endif 00037 00038 double sse = kmeans.cluster(features, K, centers, membership); 00039 00040 for (size_t i = 0; i < centers.size(); ++i) { 00041 printf("%f %f\n", centers[i][0], centers[i][1]); 00042 } 00043 printf("sse = %f\n", sse); 00044 }