User Tools

Site Tools


compile_opencv_with_nonfree_java_support

General Guidelines

You will need to have a toolchain to build source files, a version of java and the java build tool ant. The best resource for getting the OpenCV toolchain and associated dependent libraries is here: http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Your best resource for installing Java for Ubuntu distributions is via the webupd8 PPA repositories: https://launchpad.net/~webupd8team/+archive/ubuntu/java

sudo apt-add-repository ppa:webupd8team/java
sudo apt-get install oracle-java8-installer

You will also need ant, the java build program (like make; but for Java).

sudo apt-get install ant

OpenCV 2.4 with Java Support

arwillis@visionlab16-pc:~$ mkdir opencv-2.4
arwillis@visionlab16-pc:~$ cd opencv-2.4/
arwillis@visionlab16-pc:~/opencv-2.4$ git clone https://github.com/Itseez/opencv.git
Cloning into 'opencv'...
remote: Counting objects: 178721, done.
remote: Compressing objects: 100% (77/77), done.
remote: Total 178721 (delta 30), reused 3 (delta 3), pack-reused 178639
Receiving objects: 100% (178721/178721), 432.49 MiB | 2.52 MiB/s, done.
Resolving deltas: 100% (121306/121306), done.
Checking connectivity... done.
Checking out files: 100% (4587/4587), done.
arwillis@visionlab16-pc:~/opencv-2.4$ cd opencv
arwillis@visionlab16-pc:~/opencv-2.4$ git checkout 2.4
Checking out files: 100% (5091/5091), done.
Branch 2.4 set up to track remote branch 2.4 from origin.
Switched to a new branch '2.4'
arwillis@visionlab16-pc:~/opencv-2.4/opencv$ cd ..
arwillis@visionlab16-pc:~/opencv-2.4$ cd release/
arwillis@visionlab16-pc:~/opencv-2.4/release$ cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../dist ../opencv
....
arwillis@visionlab16-pc:~/opencv-2.4$ make -j4
....
arwillis@visionlab16-pc:~/opencv-2.4$ make install
....

This will compile the latest version of OpenCV 2.4 to the ~/opencv-2.4/dist directory. To compile using these libraries you will need to reference the include files in ~/opencv-2.4/dist/include and link against the libraries in ~/opencv-2.4/dist/lib.

The Java class wrappers for the OpenCV libraries and their associated JNI library is located in ~/opencv-2.4/share.

arwillis@visionlab16-pc:~/opencv-2.4$ cd dist
arwillis@visionlab16-pc:~/opencv-2.4/dist$ ls share/OpenCV/java
libopencv_java2412.so  opencv-2412.jar

Here opencv-2412.jar is the Java classes that wrap OpenCV library calls and libopencv_java2412.so is the shared library that serves as the JNI “glue” that connects Java calls to OpenCV to the functions available in the OpenCV library.

Compiling OpenCL Support into OpenCV

To compile OpenCL support into OpenCV add the WITH_OPENCL=ON flag to the cmake line. Before compiling OpenCV make sure you have OpenCL installed on your system and that you have verified that it will work. I have written some guides on Installing OpenCL.

bash$ arwillis@visionlab16-pc:~/opencv-2.4/release$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../dist -DWITH_OPENCL=ON ../opencv

The configuration will proceed and you can verify that OpenCL has been detected by reading the output and looking for an excerpt of the output to include text signaling that OpenCL is enabled as shown below:

--   Parallel framework:            pthreads
-- 
--   Other third-party libraries:
--     Use IPP:                     8.2.1 [8.2.1]
--          at:                     /home/arwillis/opencv-3.0.0/3rdparty/ippicv/unpack/ippicv_lnx
--     Use IPP Async:               NO
--     Use Eigen:                   YES (ver 3.2.2)
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
-- 
--   OpenCL:
--     Version:                     dynamic
--     Include path:                /home/arwillis/opencv-3.0.0/3rdparty/include/opencl/1.2
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 NO

OpenCV 3.0 with Java Support & nonfree (contrib)

arwillis@visionlab16-pc:~$ mkdir opencv-3.0
arwillis@visionlab16-pc:~$ cd opencv-3.0/
arwillis@visionlab16-pc:~/opencv-3.0$  git clone https://github.com/Itseez/opencv.git
Cloning into 'opencv'...
remote: Counting objects: 178721, done.
remote: Compressing objects: 100% (77/77), done.
remote: Total 178721 (delta 30), reused 3 (delta 3), pack-reused 178639
Receiving objects: 100% (178721/178721), 432.49 MiB | 6.07 MiB/s, done.
Resolving deltas: 100% (121306/121306), done.
Checking connectivity... done.
Checking out files: 100% (4587/4587), done.
arwillis@visionlab16-pc:~/opencv-3.0$ https://github.com/Itseez/opencv_contrib.git
bash: https://github.com/Itseez/opencv_contrib.git: No such file or directory
arwillis@visionlab16-pc:~/opencv-3.0$ git clone https://github.com/Itseez/opencv_contrib.git
Cloning into 'opencv_contrib'...
remote: Counting objects: 9910, done.
remote: Total 9910 (delta 0), reused 0 (delta 0), pack-reused 9910
Receiving objects: 100% (9910/9910), 81.56 MiB | 6.43 MiB/s, done.
Resolving deltas: 100% (5829/5829), done.
Checking connectivity... done.
Checking out files: 100% (1078/1078), done.
arwillis@visionlab16-pc:~/opencv-3.0$ mkdir release
arwillis@visionlab16-pc:~/opencv-3.0$ cd release/
arwillis@visionlab16-pc:~/opencv-3.0/release$ cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../dist -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules ../opencv
....
arwillis@visionlab16-pc:~/opencv-3.0/release$ make -j4
....
arwillis@visionlab16-pc:~/opencv-2.4$ make install
....

Getting OpenCV 3.0 to work in ROS Indigo

When I naively tried to run a package compiled against OpenCV 3.0 in ROS indigo I encountered segfaults. This was actually not a surprise as ROS indigo packages are compiled against the default OpenCV for Ubuntu 14.04 which is 2.4.X which is the OpenCV 2.4 distribution.

Getting OpenCV to work with ROS indigo packages requires one to recompile all the packages that use OpenCV against your OpenCV 3.0 install. For me, I initially needed to recompile the cv_bridge and image_geometry packages. These are both supplied as sub-packages of the ROS “vision_opencv” package. I went to the ROS “vision_opencv” git repository, checked out the “indigo” branch of the package into my existing workspace and attempted to recompile. This failed.

I needed to specify that I wanted an OpenCV 3.0 compile and, for me, I had placed the location of my OpenCV libraries in a custom path.

To indicate this to the “vision_opencv” package I placed the following statement in the top of the CMakeLists.txt file located in the “vision_opencv” top-level folder:

set ( OpenCV_DIR /home.old/arwillis/lib/opencv-3.0/dist-shared/share/OpenCV )

After doing so, I recompiled. I had to invoke compilation several times because my package's CMakeLists.txt file did not have the vision_opencv, image_geometry, or cv_bridge packages listed in the depends sections properly. However, it did eventually compile. I'll have to fix my depends to address this issue. Once compiled, I attempted to launch my package. This time I had segfaults due to ROS' “image_pipeline” package (image_proc subpackage) which was still using OpenCV 2.4.

To fix this, I repeated as I had done above. I checked the “image_pipeline” package source from the ROS indigo branch into my workspace. I then went to each of the subfolders (subpackages) and inserted the line above into the first few lines of the CMakeLists.txt file for each package. Specifically, I did this for the “camera_calibration”, “depth_image_proc”, “image_proc”, “image_rotate”, “image_view” and “stereo_image_proc” folders.

After doing these steps my package would run properly using OpenCV 3. Hope this helps you do the same.

I had to add the following depends to make compilations proceed more smoothly with my locally compiled ROS packages.

cv_bridge
image_geometry
image_proc

These had to go in both the package.xml and CMakeLists.txt files as new <depends> tags (package.xml) and as added packages in the find_package(catkin REQUIRED …) part of CMakeLists.txt.

I wanted to move back and forth between OpenCV 2.4 and OpenCV 3.0 so I actually reverted all these changes and put the line into the top level CMakeLists.txt file (in the workspace/src folder). This is a slight breach in protocol but saves me loads of time as I don't have to go into all of the packages and their subfolders in order to switch them globally to a new OpenCV install. Note: Any time you change the OpenCV_DIR variable you need to do a CLEAN BUILD, not just a build. I remove my build and devel folders and even any local install folders if I am using a local install.

compile_opencv_with_nonfree_java_support.txt · Last modified: 2015/08/31 14:08 by arwillis