4

I'm implementing an algorithm in OpenCV that I've designed in MATLAB. I'm writing a unit test for the SURF feature extractor in OpenCV, and I want to compare the output of MATLAB's extracted SURF features to OpenCV.

This issue is, using the same parameters for both MATLAB and OpenCV extractors I'm getting different numbers of features. How is this possible? Are there different ways to implement SURF?

For MATLAB (http://www.mathworks.com/help/vision/ref/detectsurffeatures.html) I'm using:

MetricThresh: 200
NumOctaves: 3
NumScaleLevels: 4
SURFSize: 64

For OpenCV I'm using:

HessianThreshold: 200
nOctaves: 3
nOctaveLayers: 4
extended: false
upright: true

What's going on here? Is there a better way to test that openCV and MATLAB are producing the same extracted SURF features from the same image?

Thank you for your help!

Dima
  • 38,860
  • 14
  • 75
  • 115
trianta2
  • 3,952
  • 5
  • 36
  • 52
  • 2
    fyi you might might this project useful: https://github.com/kyamagu/mexopencv/ (exposes OpenCV as MEX-functions in MATLAB) – Amro Jul 26 '13 at 01:15

1 Answers1

4

Under the hood, MATLAB uses OpenCV for some of its computer vision functions, including detecting SURF features. If you look inside the $matlabroot/bin/$arch folder, you'll find OpenCV shared libraries in addition to a gateway library ocv.dll).

In fact, the same reference paper is mentioned in the documentation of both, which suggests that the algorithm parameters have the same meaning in both frameworks.

MATLAB

Herbert Bay, Andreas Ess, Tinne Tuytelaars, Luc Van Gool "SURF: Speeded Up Robust Features", Computer Vision and Image Understanding (CVIU), Vol. 110, No. 3, pp. 346--359, 2008

OpenCV

Bay, H. and Tuytelaars, T. and Van Gool, L. "SURF: Speeded Up Robust Features", 9th > European Conference on Computer Vision, 2006


First thing, make sure you are using the same parameter values in both, taking into account the default values. Here are the doc pages for OpenCV and MATLAB for reference.

So try the following codes:

In MATLAB:

>> img = [];     % some 2d grayscale image
>> pts = detectSURFFeatures(img, 'MetricThreshold',200, ...
       'NumOctaves',3, 'NumScaleLevels',4);

In C++ OpenCV:

cv::Mat img;     // some grayscale image
cv::SURF surf(200.0, 3, 4-2, false, true);

cv::Mat mask;    // optional mask (unused here)
std::vector<cv::KeyPoint> pts;
surf(img, mask, pts);

Other than that, MATLAB usually include an older version of OpenCV (my MATLAB R2013a ships with v2.4.2 DLLs), which could result in different results from whatever OpenCV version you are using (latest as of now is v2.4.6)

Amro
  • 123,847
  • 25
  • 243
  • 454
  • 2
    note that I subtracted 2 from `NumScaleLevels` to get `nOctaveLayers` (check the source code of `edit detectSURFFeatures.m` to understand) – Amro Jul 26 '13 at 01:22
  • Hello, thanks for your thorough answer! I tried your approach with those parameters and from the same grayscale image OpenCV detected 254 points while MATLAB detected 267 points. I'm using MATLAB 2012a, and the OpenCV version I'm using is v2.4.5. I suppose the next logical step is to repeat this test, but with MATLAB using the v2.4.5 libraries. If you have any other suggestions I would appreciate them. Thank you for your time and help. – trianta2 Jul 30 '13 at 16:29
  • 2
    @trianta2: like I said, If you want to obtain the exact same results, you must use the same OpenCV version in both cases (which may not be possible). From what I can tell, MATLAB R2012a ships with OpenCV DLLs v2.1.0 (considered pretty old by now). Seeing that those are hard-coded (you can't really change what MATLAB is linked against), your only option is to [download](http://opencv.org/downloads.html) this old version and build your C++ program against it... Otherwise I suggest your ignore these minor differences and stick with the latest version :) – Amro Jul 30 '13 at 17:28