Mind Chasers Inc.
Mind Chasers Inc.

Build and test OpenCV 4 from Git on Ubuntu 20.04

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

X-ray Engineering Services

This article reviews building a simple OpenCV 4 video playback application from source using the OpenCV Git repos with Ubuntu Linux 20.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

$ cd opencv

$ git branch -r
  origin/HEAD -> origin/4.x
$ git branch
* 4.x

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  ffmpeg                                     7:4.2.4-1ubuntu0.1                         amd64        Tools for transcoding, streaming and playing of multimedia files

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

$ sudo -H pip3 install numpy

Next we build the opencv library:

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

$ cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/opt/opencv –DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..

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.5.5-dev =====================================
--   Version control:               4.5.5-300-g13a995cc1d
--   Platform:
--     Timestamp:                   2022-04-03T03:07:12Z
--     Host:                        Linux 5.4.0-104-generic x86_64
--     CMake:                       3.16.3
--     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 (18 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 (33 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (8 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++ standard:                11
--     C++ Compiler:                /usr/bin/c++  (ver 9.4.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Wi
nit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnost
ics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  
--     C++ flags (Debug):           -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Wi
nit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnost
ics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBU
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-
prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -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 -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-
prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -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,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     Linker flags (Debug):        -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a   -Wl,--gc-sections -Wl,--as-needed -Wl,--no-undefined  
--     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
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--   GUI:                           GTK3
--     GTK+:                        YES (ver 3.24.20)
--       GThread :                  YES (ver 2.64.6)
--       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: 0x020f)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.1.0)
--     JPEG 2000:                   build (ver 2.4.0)
--     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 (58.54.100)
--       avformat:                  YES (58.29.100)
--       avutil:                    YES (56.31.100)
--       swscale:                   YES (5.5.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:                   2020.0.0 Gold [2020.0.0]
--            at:                   /build/opencv/opencv/build/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2020.0.0)
--               at:                /build/opencv/opencv/build/3rdparty/ippicv/ippicv_lnx/iw
--     VA:                          NO
--     Lapack:                      NO
--     Eigen:                       YES (ver 3.3.7)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.19.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.18)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.18)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.16.5)
--     install path:                lib/python2.7/dist-packages/cv2/python-2.7
--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.8.10)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.8.so (ver 3.8.10)
--     numpy:                       /usr/local/lib/python3.8/dist-packages/numpy/core/include (ver 1.21.0)
--     install path:                lib/python3.8/site-packages/cv2/python-3.8
--   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
-- -----------------------------------------------------------------

Next, we continue on to make:

$ make
[ 98%] Generate files for Python bindings and documentation
Note: Class Feature2D has more than 1 base class (not supported by Python C extensions)
      Bases:  cv::Algorithm, cv::class, cv::Feature2D, cv::Algorithm
      Only the first base class will be used
Note: Class detail_GraphCutSeamFinder has more than 1 base class (not supported by Python C extensions)
      Bases:  cv::detail::GraphCutSeamFinderBase, cv::detail::SeamFinder
      Only the first base class will be used
[ 98%] Built target gen_opencv_python_source
Scanning dependencies of target opencv_python2
[ 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-38-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
[ 99%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibPipeline.cpp.o
[ 99%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/frameProcessor.cpp.o
[ 99%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/main.cpp.o
[ 99%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/parametersController.cpp.o
[ 99%] 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
Scanning dependencies of target opencv_model_diagnostics
[100%] Building CXX object apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/model_diagnostics.cpp.o
[100%] Linking CXX executable ../../bin/opencv_model_diagnostics
[100%] Built target opencv_model_diagnostics
$ 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"
-- Installing: /opt/opencv/bin/opencv_model_diagnostics
-- Set runtime path of "/opt/opencv/bin/opencv_model_diagnostics" 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

$ more /etc/ld.so.conf.d/libc.conf 


$ sudo ldconfig

$ ldconfig -p | grep opencv
	libopencv_videoio.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_videoio.so.405
	libopencv_videoio.so (libc6,x86-64) => /opt/opencv/lib/libopencv_videoio.so
	libopencv_video.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_video.so.405
	libopencv_video.so (libc6,x86-64) => /opt/opencv/lib/libopencv_video.so
	libopencv_stitching.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_stitching.so.405
	libopencv_stitching.so (libc6,x86-64) => /opt/opencv/lib/libopencv_stitching.so
	libopencv_photo.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_photo.so.405
	libopencv_photo.so (libc6,x86-64) => /opt/opencv/lib/libopencv_photo.so
	libopencv_objdetect.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_objdetect.so.405
	libopencv_objdetect.so (libc6,x86-64) => /opt/opencv/lib/libopencv_objdetect.so
	libopencv_ml.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_ml.so.405
	libopencv_ml.so (libc6,x86-64) => /opt/opencv/lib/libopencv_ml.so
	libopencv_imgproc.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_imgproc.so.405
	libopencv_imgproc.so (libc6,x86-64) => /opt/opencv/lib/libopencv_imgproc.so
	libopencv_imgcodecs.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_imgcodecs.so.405
	libopencv_imgcodecs.so (libc6,x86-64) => /opt/opencv/lib/libopencv_imgcodecs.so
	libopencv_highgui.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_highgui.so.405
	libopencv_highgui.so (libc6,x86-64) => /opt/opencv/lib/libopencv_highgui.so
	libopencv_gapi.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_gapi.so.405
	libopencv_gapi.so (libc6,x86-64) => /opt/opencv/lib/libopencv_gapi.so
	libopencv_flann.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_flann.so.405
	libopencv_flann.so (libc6,x86-64) => /opt/opencv/lib/libopencv_flann.so
	libopencv_features2d.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_features2d.so.405
	libopencv_features2d.so (libc6,x86-64) => /opt/opencv/lib/libopencv_features2d.so
	libopencv_dnn.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_dnn.so.405
	libopencv_dnn.so (libc6,x86-64) => /opt/opencv/lib/libopencv_dnn.so
	libopencv_core.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_core.so.405
	libopencv_core.so (libc6,x86-64) => /opt/opencv/lib/libopencv_core.so
	libopencv_calib3d.so.405 (libc6,x86-64) => /opt/opencv/lib/libopencv_calib3d.so.405
	libopencv_calib3d.so (libc6,x86-64) => /opt/opencv/lib/libopencv_calib3d.so

Let's run an opencv binary to test our install:

$ PATH=$PATH:/opt/opencv/bin/
$ opencv_version 

Build an example application with our OpenCV libraries

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

$ cd /build/opencv/
$ git clone https://github.com/oreillymedia/Learning-OpenCV-3_examples.git learnOCV_examples
$ cd /build/opencv/learnOCV_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

$ 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 join our mailing list.

Related articles on this site:

subscribe to mailing list:

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

Date: Nov. 30, 2018

Author: Erdem Tuna


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