Build and test OpenCV 3.2 on Ubuntu 16.04

Brief summary of configuring Linux Ubuntu 16.04 and gcc invocation for a simple OpenCV 3.2 video playback application


This brief article summarizes the configuration of Linux Ubuntu 16.04 and gcc invocation for a simple OpenCV 3.2 video playback application. This may be helpful to others since some tweaking was required even after following other resources. If you are having problems getting simple applications to work, maybe this article will help.

First opencv, opencv_contrib, and opencv_extra were all cloned at the same directory level using git:

$ mkdir /build/opencv
$ cd /build/opencv

$ git clone
$ git clone
$ git clone

$ ls
opencv  opencv_contrib  opencv_extra

Listed below are the packages that were installed for OpenCV ( just add 'sudo apt install' in front of each line). Of course, other standard build related packages are required (e.g., build-essential, python, etc.). However, the tricky part seems to be installing the right video support libraries. And make sure to notice that the -dev packages are required; the standard (non dev) package will be installed by default.

  • cmake, libtbb2
  • ffmpeg
  • libgtk2.0-dev
  • libavcodec, libavcodec-dev, libavformat-dev, libswscale-dev
  • libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Use dpkg to verify packages are installed. An example is show below:

$ dpkg -l | grep libjasper
ii  libjasper-dev                              1.900.1-debian1-2.4ubuntu1                    amd64        Development files for the JasPer JPEG-2000 library
ii  libjasper1:amd64                           1.900.1-debian1-2.4ubuntu1                    amd64        JasPer JPEG-2000 runtime library

Next we build the opencv library following the guidance of the O'Reilly Learning OpenCV 3 book:

$ mkdir /build/opencv/opencv/release
$ cd /build/opencv/opencv/release

	–DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
$ make
$ sudo make install

Below is the configuration summary output from cmake. Note the availability of the ffmpeg video I/O support. Without this, calls to produce the following error: "Unable to stop the stream: Inappropriate ioctl for device"

-- General configuration for OpenCV 3.2.0-dev =====================================
--   Version control:               3.2.0-225-gdcbed8d
--   Platform:
--     Timestamp:                   2017-02-27T02:02:55Z
--     Host:                        Linux 4.4.0-64-generic x86_64
--     CMake:                       3.5.1
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 5.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:          /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ /usr/lib/x86_64-linux-gnu/ gtk-x11-2.0 gdk-x11-2.0 pangocairo-1.0 atk-1.0 cairo gdk_pixbuf-2.0 gio-2.0 pangoft2-1.0 pango-1.0 gobject-2.0 glib-2.0 fontconfig freetype gthread-2.0 dc1394 v4l1 v4l2 avcodec-ffmpeg avformat-ffmpeg avutil-ffmpeg swscale-ffmpeg dl m pthread rt
--     3rdparty dependencies:       libwebp IlmImf
--   OpenCV modules:
--     To be built:                 core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz
--   GUI: 
--     QT:                          NO
--     GTK+ 2.x:                    YES (ver 2.24.30)
--     GThread :                    YES (ver 2.48.2)
--     GtkGlExt:                    NO
--     OpenGL support:              NO
--     VTK support:                 NO
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/ (ver 1.2.8)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/ (ver )
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/ (ver 1.2.54)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/ (ver 42 - 4.0.6)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/ (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--     GDAL:                        NO
--     GDCM:                        NO
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  YES (ver 2.2.4)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 56.60.100)
--       avformat:                  YES (ver 56.40.101)
--       avutil:                    YES (ver 54.31.100)
--       swscale:                   YES (ver 3.1.101)
--       avresample:                NO
--     GStreamer:                   NO
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     OpenNI2:                     NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     Aravis SDK:                  NO
--     UniCap:                      NO
--     UniCap ucil:                 NO
--     V4L/V4L2:                    Using libv4l1 (ver 1.10.0) / libv4l2 (ver 1.10.0)
--     XIMEA:                       NO
--     Xine:                        NO
--     gPhoto2:                     NO
--   Parallel framework:            pthreads
--   Other third-party libraries:
--     Use IPP:                     9.0.1 [9.0.1]
--          at:                     /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx
--     Use IPP Async:               NO
--     Use VA:                      NO
--     Use Intel VA-API/OpenCL:     NO
--     Use Lapack:                  NO
--     Use Eigen:                   NO
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
--     Use OpenVX:                  NO
--     Use custom HAL:              NO
--   OpenCL:                        <Dynamic loading of OpenCL library>
--     Include path:                /build/opencv/opencv/3rdparty/include/opencl/1.2
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 NO
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--   Python (for build):            /usr/bin/python2.7
--   Java:
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/jdk1.8.0_102/include /usr/lib/jvm/jdk1.8.0_102/include/linux /usr/lib/jvm/jdk1.8.0_102/include
--     Java wrappers:               NO
--     Java tests:                  NO
--   Matlab:                        Matlab not found or implicitly disabled
--   Documentation:
--     Doxygen:                     /usr/bin/doxygen (ver 1.8.11)
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              NO
--   Install path:                  /usr/local
--   cvconfig.h is in:              /build/opencv/opencv/release

Make sure your libraries are being found by the dynamic linker. The output below assumes you installed your libraries to /usr/local/lib and that this directory is listed in one of the conf files (e.g., libc.conf) in /etc/

$ ldconfig -p | grep opencv (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/ (libc6,x86-64) => /usr/local/lib/

Finally, we build a simple video playback application found on Learning OpenCV 3's github page: example_02-03.cpp.

We test it with the big_buck_bunny.mpg found in opencv_extra/testdata/highgui/video/.

$ gcc -g example_02-03.cpp -I/usr/local/include/ -lstdc++ -lopencv_imgcodecs -lopencv_highgui \
-lopencv_core -lopencv_imgproc -lopencv_videoio -lopencv_video -lopencv_videostab \
-o example_02-03

$ ./example_02-03 big_buck_bunny.mpg

Date: Sept. 12, 2017

Author: HenryC


Absolutely fantastic explanation.. it was the missing information that I needed to enable compiling opencv with video support. Using code::blocks also. super thanks

Add a new comment here or reply to one above:

email addresses are neither displayed nor shared