Mind Chasers Inc.
Mind Chasers Inc.

Build and test OpenCV 4 from Git on Ubuntu 18.04

This article reviews building a simple OpenCV 4 video playback application using the OpenCV Git repos with Ubuntu Linux 18.04.

This article reviews building a simple OpenCV 4 video playback application from source using the OpenCV Git repos with Ubuntu Linux 18.04. This article may prove helpful since we have found that some tweaking can be required when following other documentation resources. If you are having problems getting simple applications to work, this article may help.

If you're unfamiliar with OpenCV, you should start with this introduction.

First, clone opencv, opencv_contrib, and opencv_extra 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 we installed for OpenCV in addition to standard build packages (e.g., build-essential, python, etc.). 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.

$ sudo apt install cmake libtbb2 ffmpeg libgtk2.0-dev libavformat-dev libswscale-dev libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev

Optionally, use dpkg to verify packages were properly installed. An example is shown below to verify ffmpeg support has been installed:

$ dpkg -l | grep ffmpeg
ii  chromium-codecs-ffmpeg-extra               77.0.3865.90-0ubuntu0.18.04.1                amd64        Extra ffmpeg codecs for the Chromium Browser
ii  ffmpeg                                     7:3.4.6-0ubuntu0.18.04.1                     amd64        Tools for transcoding, streaming and playing of multimedia files
ii  ffmpeg-doc                                 7:3.4.6-0ubuntu0.18.04.1                     all          Documentation of the FFmpeg multimedia framework
ii  libffms2-4:amd64                           2.23-2                                       amd64        Cross platform ffmpeg wrapper library

We also found that for OpenCV python3 support, we needed to make sure numpy was installed:

$ sudo -H pip install numpy

Next we build the opencv library:

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

$ cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/opt/opencv –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 4.1.2-pre =====================================
--   Version control:               4.1.1-344-g9ab5e52f0
-- 
--   Platform:
--     Timestamp:                   2019-10-12T04:10:23Z
--     Host:                        Linux 4.15.0-64-generic x86_64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
-- 
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (15 files):         + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (5 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX
-- 
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 7.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 -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -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 -Wuninitialized -Winit-self -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -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 -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -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 -Wuninitialized -Winit-self -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      -Wl,--gc-sections  
--     Linker flags (Debug):        -Wl,--gc-sections  
--     ccache:                      YES
--     Precompiled headers:         NO
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
-- 
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python2 python3 stitching ts video videoio
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 java js
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.22.30)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                NO
--     GStreamer:                   NO
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Intel IPP:                   2019.0.0 Gold [2019.0.0]
--            at:                   /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2019.0.0)
--               at:                /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.4)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /build/opencv/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.15)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.15+)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
-- 
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.6.8)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.8)
--     numpy:                       /usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.17.0)
--     install path:                lib/python3.6/dist-packages/cv2/python-3.6
-- 
--   Python (for build):            /usr/bin/python2.7
-- 
--   Java:                          
--     ant:                         NO
--     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
--     Java wrappers:               NO
--     Java tests:                  NO
-- 
--   Install to:                    /opt/opencv
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /build/opencv/opencv/release

Next, we continue on to make:

$ make

...
[ 99%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o

[ 99%] Linking CXX shared module ../../lib/cv2.so
[ 99%] Built target opencv_python2
Scanning dependencies of target opencv_python3
[ 99%] Building CXX object modules/python3/CMakeFiles/opencv_python3.dir/__/src2/cv2.cpp.o
[ 99%] Linking CXX shared module ../../lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so
[ 99%] Built target opencv_python3
Scanning dependencies of target opencv_annotation
[ 99%] Building CXX object apps/annotation/CMakeFiles/opencv_annotation.dir/opencv_annotation.cpp.o
[ 99%] Linking CXX executable ../../bin/opencv_annotation
[ 99%] Built target opencv_annotation
Scanning dependencies of target opencv_visualisation
[ 99%] Building CXX object apps/visualisation/CMakeFiles/opencv_visualisation.dir/opencv_visualisation.cpp.o
[ 99%] Linking CXX executable ../../bin/opencv_visualisation
[ 99%] Built target opencv_visualisation
Scanning dependencies of target opencv_interactive-calibration
[ 99%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibController.cpp.o
[100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibPipeline.cpp.o
[100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/frameProcessor.cpp.o
[100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/main.cpp.o
[100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/parametersController.cpp.o
[100%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/rotationConverters.cpp.o
[100%] Linking CXX executable ../../bin/opencv_interactive-calibration
[100%] Built target opencv_interactive-calibration
Scanning dependencies of target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o
[100%] Linking CXX executable ../../bin/opencv_version
[100%] Built target opencv_version
$ make install
...
-- Set runtime path of "/opt/opencv/bin/opencv_annotation" to "/opt/opencv/lib"
-- Installing: /opt/opencv/bin/opencv_visualisation
-- Set runtime path of "/opt/opencv/bin/opencv_visualisation" to "/opt/opencv/lib"
-- Installing: /opt/opencv/bin/opencv_interactive-calibration
-- Set runtime path of "/opt/opencv/bin/opencv_interactive-calibration" to "/opt/opencv/lib"
-- Installing: /opt/opencv/bin/opencv_version
-- Set runtime path of "/opt/opencv/bin/opencv_version" to "/opt/opencv/lib"

$ cd /opt/opencv/

$ ls
bin  include  lib  share


Make sure your libraries can be located by the dynamic linker. The output below assumes you installed your libraries to /opt/opencv/lib and that this directory is listed in one of the conf files (e.g., libc.conf) in /etc/ld.so.conf.d

$ sudo ldconfig
$ ldconfig -p | grep opencv
	libopencv_videoio.so.4.1 (libc6,x86-64) => /opt/opencv/lib/libopencv_videoio.so.4.1
	libopencv_videoio.so (libc6,x86-64) => /opt/opencv/lib/libopencv_videoio.so
	libopencv_video.so.4.1 (libc6,x86-64) => /opt/opencv/lib/libopencv_video.so.4.1
	libopencv_video.so (libc6,x86-64) => /opt/opencv/lib/libopencv_video.so
	libopencv_stitching.so.4.1 (libc6,x86-64) => /opt/opencv/lib/libopencv_stitching.so.4.1
	libopencv_stitching.so (libc6,x86-64) => /opt/opencv/lib/libopencv_stitching.so
	libopencv_photo.so.4.1 (libc6,x86-64) => /opt/opencv/lib/libopencv_photo.so.4.1
	libopencv_photo.so (libc6,x86-64) => /opt/opencv/lib/libopencv_photo.so
	libopencv_objdetect.so.4.1 (libc6,x86-64) => /opt/opencv/lib/libopencv_objdetect.so.4.1
	libopencv_objdetect.so (libc6,x86-64) => /opt/opencv/lib/libopencv_objdetect.so
	libopencv_ml.so.4.1 (libc6,x86-64) => /opt/opencv/lib/libopencv_ml.so.4.1
	...

Build an example application with our OpenCV libraries

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

$ cd /build/opencv/
$ git clone https://github.com/oreillymedia/Learning-OpenCV-3_examples.git
$ cd /build/opencv/Learning-OpenCV-3_examples/

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

$ cp /build/opencv/opencv_extra/testdata/highgui/video/big_buck_bunny.mpg .
$ ls *.mpg
big_buck_bunny.mpg


$ gcc -g example_02-03.cpp -I/opt/opencv/include/opencv4 -L/opt/opencv/lib -lstdc++ -lopencv_imgcodecs -lopencv_highgui \
-lopencv_core -lopencv_imgproc -lopencv_videoio -lopencv_video -o example_02-03

$ ./example_02-03 big_buck_bunny.mpg
bunny from mpg
Screenshot from running example_02_03 with OpenCV

Didn't find an answer to your question? Post your issue below or in our new FORUM, and we'll try our best to help you find a solution.

And please note that we update our site daily with new content related to our open source approach to network security and system design. If you would like to be notified about these changes, then please follow us on Twitter and join our mailing list.

Related articles on this site:

share
subscribe to mailing list:

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

Date: Nov. 30, 2018

Author: Erdem Tuna

Comment:

Thank you a lot for the tutorial. This is the only tutorial that made the opencv workspace work for me. Maybe could you add some more details about the "cmake" building options?

Add a new comment here or reply to one above:

your email address will be kept private
authenticate with a 3rd party for enhanced features, such as image upload
previous month
next month
Su
Mo
Tu
Wd
Th
Fr
Sa
loading