Compiling OpenCV 3.4 with extra contributed modules

Related: GNU Make parallel build

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 Check which python to be sure it’s NOT pointing to /usr/bin/python or this install will not work!

  • OpenCV 3 works with Python 2.7, 3.4, 3.5, 3.6, 3.7.
  • 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 output text for Ubuntu 17.10
    • cmake output text for Raspberry Pi 3 and Raspbian 9 stretch
  6. Build OpenCV and OpenCV_contrib.
    make -j
    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).


This Python command should not error:

import cv2 
x = cv2.bgsegm.createBackgroundSubtractorGMG() 


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


  • 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())")




Written by Michael Hirsch, Ph.D. //