Compiling opencv in Ubuntu 16.04

Getting Source Code:

Source for opencv master download:

~$ wget https://github.com/opencv/opencv/archive/master.zip

Unzip and get it ready for build:

  • $ unzip master.zip
  • $ cd opencv-master
  • $ opencv-master/mkdir build
  • $ opencv-master/cd build
  • $ opencv-master/build

Source for opencv-contrib:

  • $ git clone https://github.com/opencv/opencv_contrib.git
  • $ cd opencv_contrib
  • Note: Make sure opencv_contrib folder is at $HOME folder because we will set the following configuration parameter:
    • -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

 

Configure Make:

~/opencv-master/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D BUILD_EXAMPLES=ON -DCMAKE_C_COMPILER=gcc-4.9 -DCMAKE_CXX_COMPILER=g++-4.9  -D WITH_OPENGL=ON ..

See full configure log: https://aichamp.wordpress.com/2016/11/11/ubuntu-16-04-opencv-master-build-with-gcc-4-9/

Possible Issues [1] – Error Message

CMake Error at CMakeLists.txt:68 (project):

The CMAKE_CXX_COMPILER:

/usr/bin/c++

is not a full path to an existing compiler tool.

Root Cause:

This is because gcc version is not set properly:

Solution:

First check the gcc version in the machine and I have gcc 4.9.

Note: I had to downgrade gcc from 5.4 to 4.9 to solve CUDA/opencv problem in Ubuntu 16.04

$ gcc-4.9 –version
gcc-4.9 (Ubuntu 4.9.3-13ubuntu2) 4.9.3
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Now add the following parameter with cmake command as below:

-DCMAKE_C_COMPILER=gcc-4.9 -DCMAKE_CXX_COMPILER=g++-4.9

 

Possible Issues [2] – Error Message

CMake Warning at cmake/OpenCVPackaging.cmake:23 (message):
CPACK_PACKAGE_VERSION does not match version provided by version.hpp
header!
Call Stack (most recent call first):
CMakeLists.txt:1105 (include)

Root Cause: If you get above error means you are using opencv code from a particular version 2.4.13 or 3.1.0.

Solution: Get opencv source from master branch instead of any particular version.

Possible Issues [3] – Error Message

If you hit the following error while running above cmake command:

CMake Error at /usr/share/cmake-3.5/Modules/FindQt4.cmake:1326 (message):
Found unsuitable Qt version “” from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
cmake/OpenCVFindLibsGUI.cmake:34 (find_package)
CMakeLists.txt:556 (include)

Here is the solution:

$ qmake –version
qmake: could not find a Qt installation of ”

$ sudo apt-get install qt4-dev-tools libqt4-dev

$ qmake -v
QMake version 2.01a
Using Qt version 4.8.7 in /usr/lib/x86_64-linux-gnu

 

Finally Making binaries:

~/opencv-master/build$ make -j4

See full make log: https://aichamp.wordpress.com/2016/11/11/ubuntu-16-04-opencv-master-build-output/

 

Results are available inside lib folder as below:

cv2.so* libopencv_features2d.so@ libopencv_phase_unwrapping.so@ libopencv_text.so@ libopencv_aruco.so@ libopencv_flann.so@ libopencv_photo.so@ libopencv_tracking.so@libopencv_bgsegm.so@ libopencv_fuzzy.so@ libopencv_plot.so@ libopencv_videoio.so@libopencv_bioinspired.so@ libopencv_hdf.so@ libopencv_reg.so@ libopencv_video.so@
libopencv_calib3d.so@ libopencv_highgui.so@ libopencv_rgbd.so@ libopencv_videostab.so@libopencv_ccalib.so@ libopencv_imgcodecs.so@ libopencv_saliency.so@ libopencv_xfeatures2d.so@ libopencv_core.so@ libopencv_imgproc.so@ libopencv_shape.so@ libopencv_ximgproc.so@
libopencv_cvv.so@ libopencv_java310.so* libopencv_stereo.so@ libopencv_xobjdetect.so@libopencv_datasets.so@ libopencv_line_descriptor.so@ libopencv_stitching.so@ libopencv_xphoto.so@libopencv_dnn.so@ libopencv_ml.so@ libopencv_structured_light.so@ libopencv_dpm.so@ libopencv_objdetect.so@ libopencv_superres.so@ libopencv_face.so@ libopencv_optflow.so@ libopencv_surface_matching.so@

Build installer:

~/opencv-master/build$ sudo make install

 

Test opencv with python:

~/opencv-master/build$ python
Python 2.7.12 (default, Jul 1 2016, 15:12:24)
[GCC 5.4.0 20160609] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2

>>

 

Advertisements

One thought on “Compiling opencv in Ubuntu 16.04

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s