test_KMeans.cpp

simple test to measure k-means time - Boris Mansencal, 2012-08-23 06:41 pm

Download (1.8 kB)

 
1
//g++ -Wall -Wextra -pedantic -ansi -march=native -mtune=native `pkg-config --cflags --libs opencv` -o test_KMeans test_KMeans.cpp
2
3
#include <cstdlib>
4
#include <iostream>
5
6
#include "opencv2/highgui/highgui.hpp"
7
#include "opencv2/calib3d/calib3d.hpp"
8
#include "opencv2/imgproc/imgproc.hpp"
9
#include "opencv2/features2d/features2d.hpp"
10
#if CV_MAJOR_VERSION*100+CV_MINOR_VERSION*10+CV_SUBMINOR_VERSION > 233
11
#include "opencv2/nonfree/nonfree.hpp"
12
#endif
13
14
int
15
main(int argc, char* argv[])
16
{
17
#if CV_MAJOR_VERSION*100+CV_MINOR_VERSION*10+CV_SUBMINOR_VERSION > 233
18
  cv::initModule_nonfree();
19
#endif
20
21
  if (argc != 2) {
22
    std::cerr<<"Usage"<<argv[0]<<" imageFilename\n";
23
    exit(EXIT_FAILURE);
24
  }
25
26
  const char *imageFilename = argv[1];
27
  cv::Mat img = cv::imread(imageFilename);
28
  if (img.empty()) {
29
    std::cerr<<"Error: unable to load imageFilename: "<<imageFilename<<"\n";
30
    exit(EXIT_FAILURE);
31
  }
32
    
33
34
  cv::Ptr<cv::FeatureDetector> detector = new cv::DenseFeatureDetector();
35
  cv::Ptr<cv::DescriptorExtractor> descriptorExtractor = new cv::SiftDescriptorExtractor(); //cv::DescriptorExtractor::create("SIFT");
36
  
37
  std::vector<cv::KeyPoint> keypoints;
38
  detector->detect(img, keypoints );
39
  cv::Mat descriptors;
40
  descriptorExtractor->compute(img, keypoints, descriptors);
41
42
43
  const int cluster_count = keypoints.size() * 0.01; 
44
  
45
46
  std::cerr<<"kmeans of "<<descriptors.rows<<" descriptors of size "<<descriptors.cols<<": ";
47
48
  cv::Mat labels(descriptors.rows, 1, CV_32S);
49
  cv::Mat centers(descriptors.rows, descriptors.rows, CV_32F); //B: do we have to allocate it ???????
50
51
  const int attempts = 3;
52
53
  double t = (double)cv::getTickCount();
54
55
  cv::kmeans(descriptors, cluster_count, labels, cv::TermCriteria(), attempts, cv::KMEANS_PP_CENTERS, centers);
56
57
  t = ((double)cv::getTickCount() -t )/cv::getTickFrequency();
58
  
59
  std::cerr<<t<<"s\n";
60
61
  return 0;
62
}