Compiling OpenCV 3.3 with extra contributed modules

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.3 works with Python 2.7, 3.4, 3.5, 3.6.
  • tested with Ubuntu 17.10 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. Get OpenCV and switch to latest release
    cd ~
    git clone https://github.com/opencv/opencv.git
    cd ~/opencv
    git checkout tags/3.3.1
    
  3. Get OpenCV_contrib and switch to latest release
    cd ~
    git clone https://github.com/opencv/opencv_contrib.git
    cd ~/opencv_contrib
    git checkout tags/3.3.1
    
  4. Back 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 -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())") .. 
    
  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 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.10
  • functions like cv2.createBackgroundSubtractorGMG are not in the standard OpenCV 3 package. The newer functions by design go into this opencv_contrib repository.

Tags:

Categories:

Updated:

Leave a Comment