Table Of Contents
In my last post Compile OpenCV from source with JAVA support I explained how to Compile OpenCV from source with JAVA support, however, the OpenCV nonfree part was not included. If you are planning to use private features from OpenCV like SIFT or SURF descriptors, you should go as follows:
. Download OpenCV 3.2, unpack and create build directory
following the steps of my last post.
. Download and unpack nonfree part
nonfree part has been separated in OpenCV3+, so you need to download it separately from github opencv repo or clone the repository. Then extract opencv_contrib and move it inside your opencv folder :
[email protected] ~ $ cp Downloads/opencv-contrib opencv-3.2.0/
. generate makefiles
we move to the build folder inside opencv folder and type:
[email protected]~$ cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release\\ -DCMAKE_INSTALL_PREFIX=../dist -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ..
DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules we are specifying where to found the nonfree part.
If makefiles generation went ok, you can now build. If using openCV in JAVA, you must make sure
$JAVA_HOME variable is set to JDK’s path and visible to child processes. For that when doing:
echo $JAVA_HOME the JDK path must be displayed. If not, in terminal, set de variable value to JDK path and export it, for example:
[email protected] ~$ export JAVA_HOME=/home/jdk1.8.0_111/
Notice that when generating the makefile, the output in the Java field looks like this:
That is, there is a specified ant and JNI path, and Java wrappers is set to YES.
run make to build openCV with Java and create a jar:
[email protected] ~/opencv-3.2.0/build $ make -j8
- where j8 denotes the desired parallelism level.
Make sure the files opencv-320.jar and libopencv_java320.so (.so or .dll) are created inside /build.
[email protected] ~/opencv-3.2.0/build $ ls -R | grep opencv-320.jar opencv-320.jar opencv-320.jar.dephelper [email protected] ~/opencv-3.2.0/build $ ls -R | grep libopencv_java320.so libopencv_java320.so
. Edit features2d_manual.hpp file
Ok, if building was successfull, then move to
[email protected] ~/opencv-3.2.02 $ cd modules/features2d/misc/java/src/cpp/
and edit features2d_manual.hpp with your favorite text editor, as following:
- In line 8, under
- In line 121, in create method, inside
case SURFreplace :
//name = name + "SIFT"; by
//name = name + "SURF"; by
- 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;
.Rebuild to apply changes
move to your opencv/build folder and run
make install when finished, you just need to include the .so and .jar files on your openCV project and you would be able to use SIFT and SURF decriptors in your code.
Spot a typo?: Help me fix it by contacting me or commenting below!