Compiling OpenCV 3.4 with extra contributed modules

1 minute read

OpenCV with OpenCV_contrib extra modules are easy to add to OpenCV for Python. It’s best to do this procedure with Anaconda Python (or other non-system Python install).

  • assumes preferred Python exe is aliased to (it runs when you type)
    python
    
  • OpenCV 3 works with Python 2.7, 3.4, 3.5, 3.6.
  • tested with Ubuntu on laptop, and Raspberry Pi 3 with Raspbian 9 stretch
  1. prereqs
    apt install cmake gcc g++ git libjpeg-dev libpng-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev pkg-config libgtk2.0-dev libopenblas-dev libatlas-base-dev liblapack-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavcodec-dev libavutil-dev libavfilter-dev libavformat-dev libavresample-dev ffmpeg
    
  2. Download and unzip latest OpenCV 3 release Source Code.
  3. Download and unzip the corresponding source code for opencv_contrib
  4. go to main OpenCV directory sh mkdir ~/opencv/release cd ~/opencv/release `
  5. Configure OpenCV and OpenCV_contrib.
    cmake -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE .. 
    
    • cmake output text for Ubuntu 17.10
    • cmake output text for Raspberry Pi 3 and Raspbian 9 stretch
  6. Build OpenCV and OpenCV_contrib.
    make -j4
    make install   # not sudo, except for Raspberry Pi
    

    Note: on the Raspberry Pi, consider make -j2 to avoid over-temperature and under-voltage warnings (in general when compiling on Raspberry Pi, not just for OpenCV).

Test

This Python command should not error:

import cv2 
x = cv2.bgsegm.createBackgroundSubtractorGMG() 

Verify

After cmake, check for the following “extra” OpenCV components. For example, look for bgsegm, that’s one of the opencv_contrib modules:

OpenCV modules:
-- To be built: core imgproc imgcodecs videoio highgui xobjdetect adas video bgsegm bioinspired flann ml features2d calib3d ccalib face text datasets line_descriptor objdetect optflow photo reg rgbd saliency shape xfeatures2d stitching superres surface_matching videostab ximgproc xphoto python2 tracking ts
-- Disabled: java world
-- Disabled by dependency: -
-- Unavailable: androidcamera cuda cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaoptflow cudastereo cudawarping cudev python3 viz cvv matlab

Notes

  • libjasper-dev was removed from Ubuntu > 16.04 due to security concerns (still outstanding) with JasPer. WebP is a better performing format from Google.
  • functions like cv2.createBackgroundSubtractorGMG are not in the standard OpenCV 3 package. The newer functions by design go into this opencv_contrib repository.

trouble finding desired Python

try adding to your shell cmake command:

-DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON_EXECUTABLE=$(which python) -DPYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")

Leave a Comment