There are useful answers, but I'll add my version (for OpenCV 3.X) just in case the above ones aren't clear (tested and tried):
- Clone opencv from https://github.com/opencv/opencv to home dir
- Clone opencv_contrib from https://github.com/opencv/opencv_contrib to home dir
- Inside opencv, create a folder named
build
- Use this CMake command, to activate non-free modules:
cmake -DOPENCV_EXTRA_MODULES_PATH=/home/YOURUSERNAME/opencv_contrib/modules -DOPENCV_ENABLE_NONFREE:BOOL=ON ..
(Please notice that we showed where the contrib modules resides and also activated the nonfree modules)
- Do
make
and make install
afterwards
The above steps should work out for OpenCV 3.X
After that, you may run the below code using g++ with the appropriate flags:
g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -lutil -lboost_iostreams -lboost_system -lboost_filesystem -lopencv_xfeatures2d -o surftestexecutable
The important thing not to forget is to link the xfeatures2D library with -lopencv_xfeatures2d as shown on the command. And the main.cpp
file is:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/xfeatures2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char* argv[])
{
const cv::Mat input = cv::imread("surf_test_input_image.png", 0); //Load as grayscale
Ptr< cv::xfeatures2d::SURF> surf = xfeatures2d::SURF::create();
std::vector<cv::KeyPoint> keypoints;
surf->detect(input, keypoints);
// Add results to image and save.
cv::Mat output;
cv::drawKeypoints(input, keypoints, output);
cv::imwrite("surf_result.jpg", output);
return 0;
}
This should create and save an image with surf keypoints.