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

advertisement

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 https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
$ git clone https://github.com/opencv/opencv_extra.git

$ 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

$ cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local \
	–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 cap.open() 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/libpng.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libtiff.so /usr/lib/x86_64-linux-gnu/libjasper.so /usr/lib/x86_64-linux-gnu/libjpeg.so 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/libz.so (ver 1.2.8)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.54)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.6)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (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/ld.so.conf.d

$ ldconfig -p | grep opencv

libopencv_videostab.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_videostab.so.3.2
libopencv_videostab.so (libc6,x86-64) => /usr/local/lib/libopencv_videostab.so
libopencv_videoio.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_videoio.so.3.2
libopencv_videoio.so (libc6,x86-64) => /usr/local/lib/libopencv_videoio.so
libopencv_video.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_video.so.3.2
libopencv_video.so (libc6,x86-64) => /usr/local/lib/libopencv_video.so
libopencv_superres.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_superres.so.3.2
libopencv_superres.so (libc6,x86-64) => /usr/local/lib/libopencv_superres.so
libopencv_stitching.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_stitching.so.3.2
libopencv_stitching.so (libc6,x86-64) => /usr/local/lib/libopencv_stitching.so
libopencv_shape.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_shape.so.3.2
libopencv_shape.so (libc6,x86-64) => /usr/local/lib/libopencv_shape.so
libopencv_photo.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_photo.so.3.2
libopencv_photo.so (libc6,x86-64) => /usr/local/lib/libopencv_photo.so
libopencv_objdetect.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_objdetect.so.3.2
libopencv_objdetect.so (libc6,x86-64) => /usr/local/lib/libopencv_objdetect.so
libopencv_ml.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_ml.so.3.2
libopencv_ml.so (libc6,x86-64) => /usr/local/lib/libopencv_ml.so
libopencv_imgproc.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_imgproc.so.3.2
libopencv_imgproc.so.2.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4
libopencv_imgproc.so (libc6,x86-64) => /usr/local/lib/libopencv_imgproc.so
libopencv_imgcodecs.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_imgcodecs.so.3.2
libopencv_imgcodecs.so (libc6,x86-64) => /usr/local/lib/libopencv_imgcodecs.so
libopencv_highgui.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_highgui.so.3.2
libopencv_highgui.so (libc6,x86-64) => /usr/local/lib/libopencv_highgui.so
libopencv_flann.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_flann.so.3.2
libopencv_flann.so (libc6,x86-64) => /usr/local/lib/libopencv_flann.so
libopencv_features2d.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_features2d.so.3.2
libopencv_features2d.so (libc6,x86-64) => /usr/local/lib/libopencv_features2d.so
libopencv_core.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_core.so.3.2
libopencv_core.so.2.4 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4
libopencv_core.so (libc6,x86-64) => /usr/local/lib/libopencv_core.so
libopencv_calib3d.so.3.2 (libc6,x86-64) => /usr/local/lib/libopencv_calib3d.so.3.2
libopencv_calib3d.so (libc6,x86-64) => /usr/local/lib/libopencv_calib3d.so

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

Comment:

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