Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Last active April 18, 2017 17:43
Show Gist options
  • Save Ni55aN/497007cea5eed3b86317652f8080cce4 to your computer and use it in GitHub Desktop.
Save Ni55aN/497007cea5eed3b86317652f8080cce4 to your computer and use it in GitHub Desktop.
ORB vs SURF
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <ctime>
#include <math.h>
#include <fstream>
#include <opencv2/core.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;
clock_t begin_time = 0;
void start() {
begin_time = clock();
}
void end(const char* prefix) {
std::cout << prefix << float( clock() - begin_time) / CLOCKS_PER_SEC << " sec" << std::endl;
}
void detect(const char * name, Ptr<Feature2D> detector, Mat img, bool writeFile = false) {
std::vector<KeyPoint> keypoints;
start();
detector->detect(img, keypoints);
end("Keypoints detected: ");
Mat descriptors;
start();
detector->compute(img, keypoints, descriptors);
end("Descriptors computed: ");
Mat img_keypoints;
drawKeypoints(img, keypoints, img_keypoints, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
char* winName = new char[100];
sprintf(winName, "%s: %lu %s", name, keypoints.size(), "points");
imshow(winName, img_keypoints);
if (writeFile) {
ofstream myfile;
myfile.open("descriptors.json");
myfile << "{\"points\":[\n";
for (int i = 0, s = keypoints.size(); i < s; i++) {
myfile << "{\"x\":" << round(keypoints.at(i).pt.x)
<< ",\"y\":" << round(keypoints.at(i).pt.y)
<< ",\"descriptor\":"
<< descriptors.row(i)
<< (i + 1 < s ? "},\n" : "}\n");
}
myfile << "],\"type\":\"" << name << "\", \"width\":" << img.cols << ", \"height\":" << img.rows << "}";
myfile.close();
}
}
int main(int argc, char** argv) {
string type(argv[1]);
Mat img = imread(argv[2], IMREAD_GRAYSCALE);
if (!img.data) {
cout << " --(!) Error reading images " << endl;
return -1;
}
if (string("ORB").compare(type) == 0)
detect("ORB", ORB::create(500, 1.2f, 2), img, true);
else if (string("SURF").compare(type) == 0)
detect("SURF", SURF::create(200), img, true);
waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment