After browsing the internet and trying to make OpenCV + SURF work in a Java application, I write this guide for future reference and hope it might save you sometime!

OpenCV for Java Setup Guide

Make sure yuo have installed package libv4l-dev to enable V4L/V4L2 support before compiling openCV.
Download OpenCV 2.4 or 3.3 source code (this GUI relies on OpenCV 2.4 to use the SURF/SIFT features) from the official website https://opencv.org/releases.html

Go to current opencv folder, create a “build” directory.
In terminal, cd opencv/build

cmake -D BUILD_SHARED_LIBS=OFF ..
In build directory, do make -j8

If the compiling process doesn’t fail, you will see a “opencv-2xx.jar” file in build/bin

 

If you install OpenCV 3.3, you have to download OpenCV-3.3 Contrib to use additional features like SURF and SIFT (they’re not free in version 3)

 

Download the opencv_contrib zip (make sure it matches the opencv version): https://github.com/opencv/opencv_contrib/archive/3.3.1.zip

Extract the opencv_contrib-3.3.1 folder to be in the same level as the other opencv-3.3.1

Make a new directory called build in opencv-3.3.1
cd/opencv-3.3.1/build
cmake -D OPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules <opencv_source>
For example: cmake -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.3.1/modules ../../opencv-3.3.1
Run make -j8
If the build is successful, within your favourite text editor, open the file features2d_manual.hpp in opencv-3.3.1/modules/features2d/misc/java/src/cpp

 

In line 8, under #include “features2d_converters.hpp” add #include “opencv2/xfeatures2d.hpp”

In line 121, in create method, inside case SITF and case SURF replace :
//name = name + “SIFT”; by fd=xfeatures2d::SIFT::create(); and //name = name + “SURF”; by fd=xfeatures2d::SURF::create();
do the same in line 242 for SIFT and SURF extractors:

case SIFT:
de = xfeatures2d::SIFT::create();
break;
case SURF:
de = xfeatures2d::SURF::create();
break;

After enabling SURF and SIFT features, do sudo make install in the existing build folder.

 

Set up OpenCV for Java in Eclipse

Go to Eclipse Preferences menu.
Go to Java > Build Path > User Libraries and choose New…
Enter a name for the library (e.g. “opencv2.4”) and select the newly created user library.
Choose Add External JARs…, browse to select the opencv-2xx.jar in the opencv/build/ folder.
After adding the jar, extend it, select Native library location, and press Edit

Select External Folder… and browse to select the folder containing the OpenCV libraries (e.g., /downloads/opencv2/build/lib)