1 |
|
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();
|
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);
|
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 | }
|