Skip to content

Instantly share code, notes, and snippets.

@varunagrawal
Last active October 22, 2021 17:35
Show Gist options
  • Save varunagrawal/92fe9c9a4451823c20124f5fe6744096 to your computer and use it in GitHub Desktop.
Save varunagrawal/92fe9c9a4451823c20124f5fe6744096 to your computer and use it in GitHub Desktop.
Compiling OpenCV 3+

Also works for OpenCV 4

Prerequisites

NOTE: We need to use cmake with HTTPS support to download the files when cmake runs. Any version of CMake >= 3.9 should do.

wget --no-check-certificate https://cmake.org/files/v3.9/cmake-3.9.0.tar.gz
tar -zxvf cmake-3.9.0.tar.gz
cd cmake-3.9.0
./bootstrap --system-curl
make && make install

Basic Install

cmake \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -D WITH_TBB=OFF \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D BUILD_PROTOBUF=OFF -D WITH_PROTOBUF=ON \
    -D WITH_LAPACK=OFF ..

With Pyenv

cmake \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -D WITH_TBB=OFF \
    -D ENABLE_CXX11=ON \
    -D BUILD_PROTOBUF=OFF -D WITH_PROTOBUF=ON \
    -D BUILD_opencv_python3=TRUE \
    -D PYTHON3_EXECUTABLE=/home/varun/.pyenv/versions/3.8.0/bin/python \
    -D PYTHON_INCLUDE_DIR=/home/varun/.pyenv/versions/3.8.0/include/python3.8 \
    -D PYTHON3_LIBRARY=/home/varun/.pyenv/versions/3.8.0/lib/libpython3.8.a \
    -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/varun/.pyenv/versions/3.8.0/lib/python3.8/site-packages/numpy/core/include \
    -D PYTHON3_PACKAGES_PATH=/home/varun/.pyenv/versions/3.8.0/lib/python3.8/site-packages \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D WITH_CUDA=OFF \
    -D WITH_LAPACK=OFF ..

Install with Anaconda and CUDA

OpenCV from source works on Ubuntu 16.04. To compile with Anaconda, use the following cmake config:

cmake \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -D WITH_TBB=OFF \
    -D BUILD_PROTOBUF=OFF -D WITH_PROTOBUF=ON \
    -D PYTHON3_EXECUTABLE=/home/varun/anaconda3/bin/python \
    -D PYTHON_INCLUDE_DIR=/home/varun/anaconda3/include/python3.8 \
    -D PYTHON3_LIBRARY=/home/varun/anaconda3/lib/libpython3.8.so \
    -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/varun/anaconda3/lib/python3.8/site-packages/numpy/core/include \
    -D PYTHON3_PACKAGES_PATH=/home/varun/anaconda3/lib/python3.8/site-packages \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D WITH_CUDA=ON \
    -D WITH_LAPACK=OFF ..

Raspberry Pi

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules\
    -D ENABLE_NEON=ON \
    -D ENABLE_VFPV3=ON \
    -D OPENCV_EXTRA_EXE_LINKER_FLAGS=-latomic \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D BUILD_TESTS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D CMAKE_SHARED_LINKER_FLAGS='-latomic' \
    -D BUILD_EXAMPLES=OFF ..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment