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 ffmpeg 7:3.4.6-0ubuntu0.18.04.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/release $ cd /build/opencv/opencv/release $ 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.3.0-dev ===================================== -- Version control: 4.3.0-344-gc3e8a82c9c -- -- Platform: -- Timestamp: 2020-05-31T14:17:30Z -- Host: Linux 4.15.0-1079-oem 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 (17 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 (31 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 -- AVX512_SKX (7 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 7.5.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=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=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,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed -- Linker flags (Debug): -Wl,--exclude-libs,libippicv.a -Wl,--exclude-libs,libippiw.a -Wl,--gc-sections -Wl,--as-needed -- ccache: NO -- 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 python3 stitching ts video videoio -- Disabled: world -- Disabled by dependency: - -- Unavailable: java js python2 -- Applications: tests perf_tests apps -- Documentation: NO -- Non-free algorithms: NO -- -- GUI: -- GTK+: YES (ver 2.24.32) -- 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: 0x020f) -- 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: build Jasper (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: 2020.0.0 Gold [2020.0.0] -- at: /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx/icv -- Intel IPP IW: sources (2020.0.0) -- at: /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx/iw -- Lapack: NO -- Eigen: NO -- 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 3: -- Interpreter: /usr/bin/python3 (ver 3.6.9) -- Libraries: /usr/lib/x86_64-linux-gnu/libpython3.6m.so (ver 3.6.9) -- numpy: /usr/local/lib/python3.6/dist-packages/numpy/core/include (ver 1.18.4) -- install path: lib/python3.6/dist-packages/cv2/python-3.6 -- -- Python (for build): /usr/bin/python2.7 -- -- Java: -- ant: NO -- JNI: NO -- Java wrappers: NO -- Java tests: NO -- -- Install to: /opt/opencv -- -----------------------------------------------------------------
Next, we continue on to make:
$ make ... [ 99%] 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 [ 99%] Built target gen_opencv_python_source 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 [100%] 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.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_videoio.so.4.3 libopencv_videoio.so (libc6,x86-64) => /opt/opencv/lib/libopencv_videoio.so libopencv_video.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_video.so.4.3 libopencv_video.so (libc6,x86-64) => /opt/opencv/lib/libopencv_video.so libopencv_stitching.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_stitching.so.4.3 libopencv_stitching.so (libc6,x86-64) => /opt/opencv/lib/libopencv_stitching.so libopencv_photo.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_photo.so.4.3 libopencv_photo.so (libc6,x86-64) => /opt/opencv/lib/libopencv_photo.so libopencv_objdetect.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_objdetect.so.4.3 libopencv_objdetect.so (libc6,x86-64) => /opt/opencv/lib/libopencv_objdetect.so libopencv_ml.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_ml.so.4.3 libopencv_ml.so (libc6,x86-64) => /opt/opencv/lib/libopencv_ml.so libopencv_imgproc.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_imgproc.so.4.3 libopencv_imgproc.so (libc6,x86-64) => /opt/opencv/lib/libopencv_imgproc.so libopencv_imgcodecs.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_imgcodecs.so.4.3 libopencv_imgcodecs.so (libc6,x86-64) => /opt/opencv/lib/libopencv_imgcodecs.so libopencv_highgui.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_highgui.so.4.3 libopencv_highgui.so (libc6,x86-64) => /opt/opencv/lib/libopencv_highgui.so libopencv_gapi.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_gapi.so.4.3 libopencv_gapi.so (libc6,x86-64) => /opt/opencv/lib/libopencv_gapi.so libopencv_flann.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_flann.so.4.3 libopencv_flann.so (libc6,x86-64) => /opt/opencv/lib/libopencv_flann.so libopencv_features2d.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_features2d.so.4.3 libopencv_features2d.so (libc6,x86-64) => /opt/opencv/lib/libopencv_features2d.so libopencv_dnn.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_dnn.so.4.3 libopencv_dnn.so (libc6,x86-64) => /opt/opencv/lib/libopencv_dnn.so libopencv_core.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_core.so.4.3 libopencv_core.so (libc6,x86-64) => /opt/opencv/lib/libopencv_core.so libopencv_calib3d.so.4.3 (libc6,x86-64) => /opt/opencv/lib/libopencv_calib3d.so.4.3 libopencv_calib3d.so (libc6,x86-64) => /opt/opencv/lib/libopencv_calib3d.so
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 $ 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

References
- CMake Home Page
- FFmpeg Home Page
- OpenCV 4.3.0 Documentation and Introduction
- OpenCV on GitHub
- OpenCV Issue Tracker
Date: Sept. 12, 2017
Author: HenryC
Comment: