Skip to content

Instantly share code, notes, and snippets.

@rjw57
Created September 8, 2015 16:01
Show Gist options
  • Save rjw57/d50dd8a023afa84a5bdb to your computer and use it in GitHub Desktop.
Save rjw57/d50dd8a023afa84a5bdb to your computer and use it in GitHub Desktop.

Compiling OpenCV 3 with Python bindings and pyenv

Dependencies

Luckily Ubuntu has support for getting the build dependencies for a package directly via apt-get:

sudo apt-get build-dep opencv

In addition, we'll need a more modern CMake:

sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
sudo apt-get install cmake

Setting up the environment

Create somewhere to install OpenCV:

mkdir -p $HOME/opt/opencv
cd $HOME/opt/opencv

Getting the source

cURL is our friend. Use the -L option to process redirects.

curl -L https://github.com/Itseez/opencv/archive/3.0.0.tar.gz | tar xvz
curl -L https://github.com/Itseez/opencv_contrib/archive/3.0.0.tar.gz | tar xvz

Configuring and compiling

cd opencv-3.0.0
mkdir release; cd release
ccmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/opt/opencv/ \
    OPENCV_EXTRA_MODULES_PATH=$HOME/opt/opencv/opencv_contrib-3.0.0/modules ..

Set python include dirs manually (both INCLUDE_DIRS and INCLUDE_DIRS2) and set library. Paths will be of form $HOME/.pyenv/versions/X.Y.Z/include/pythonX.Y and $HOME/.pyenv/versions/X.Y.Z/lib/libpythonX.Y.so. For Python 3, the pythonX.Y part may be pythonX.Ym.

make -j8 all install

Environment setup script

Add an environment setup script at ~/opt/opencv/vars.sh

cat >$HOME/opt/opencv/vars.sh <<EOI
# source this file to set up environment
export OPENCV_PREFIX=$HOME/opt/opencv/
export PATH=$OPENCV_PREFIX/bin:$PATH
export LD_LIBRARY_PATH=$OPENCV_PREFIX/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$OPENCV_PREFIX/lib/python3.4/site-packages:$OPENCV_PREFIX/lib/python2.7/site-packages
EOI

Testing

source $HOME/opt/opencv/vars.sh
python -c 'import cv2'
python2 -c 'import cv2'
@hiagop
Copy link

hiagop commented Dec 2, 2015

Excuse my lack of knowledge, but where should I set those INCLUDE_DIRS on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment