3

Took me a while to figure this out so I thought I'd write it down.

Wesley
  • 1,412
  • 1
  • 15
  • 23

1 Answers1

9

Install python with shared library support through pyenv:

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.5.0

Make a new virtualenv named opencv

pyenv virtualenv 3.5.0 opencv

Activate the virtualenv and install numpy

pyenv activate opencv
pyenv rehash
pip install numpy

Clone opencv and opencv_contrib and put it in ~/

cd ~
git clone https://github.com/Itseez/opencv.git
cd opencv
git checkout 3.1.0

cd ~
git clone https://github.com/Itseez/opencv_contrib
cd opencv_contrib
git checkout 3.1.0

cd ~/opencv
mkdir build
cd build

Run CMake and install

PREFIX_MAIN=`pyenv virtualenv-prefix`
PREFIX=`pyenv prefix`
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX="$PREFIX" \
    -D PYTHON3_EXECUTABLE="$PREFIX"/bin/python3.5 \
    -D PYTHON3_PACKAGES_PATH="$PREFIX"/lib/python3.5/site-packages \
    -D PYTHON3_LIBRARY="$PREFIX_MAIN"/lib/libpython3.5m.dylib \
    -D PYTHON3_INCLUDE_DIR="$PREFIX_MAIN"/include/python3.5m \
    -D PYTHON3_NUMPY_INCLUDE_DIRS="$PREFIX"/lib/python3.5/site-packages/numpy/core/include \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D BUILD_EXAMPLES=ON \
    -D BUILD_opencv_python3=ON \
    -D INSTALL_NAME_DIR=${CMAKE_INSTALL_PREFIX}/lib \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules ..

make -j8

make install

Note: if you are using python 3.5.2 you need to change

PYTHON3_EXECUTABLE="$PREFIX"/bin/python3
PYTHON3_LIBRARY="$PREFIX_MAIN"/lib/libpython3.5m.a

source: basically mashed these guides together:

linusg
  • 6,289
  • 4
  • 28
  • 78
Wesley
  • 1,412
  • 1
  • 15
  • 23
  • I needed to add `-D BUILD_SHARED_LIBS=OFF` because the `cv2.so` binary it made couldn't find my dylibs(?), and I also followed the update on your first site saying that OpenCV 3.1.0 is broken unless you switch to `-D INSTALL_C_EXAMPLES=OFF` (didn't bother trying with it ON) – Nick T Apr 20 '16 at 19:57
  • I got this error with I run `make - j8`: `fatal error: 'opencv2/core/hal/intrin.hpp' file not found` . Any idea how to resolve this? – sooon Jul 28 '16 at 00:15
  • @NickT Thanks, I've updated the answer for 3.1.0. Not sure what the deal is with BUILD_SHARED_LIBS though. – Wesley Jul 28 '16 at 05:55
  • 1
    @sooon Try checking out 3.1.0 for both repos and building again. – Wesley Jul 28 '16 at 05:55
  • I'd use just a single prefix of the virtual environment. Mixing different Pythons calls for problems... Also, it's enough to set the CMake flag -DPython3_ROOT_DIR="$(pyenv prefix)", it will set the other flags accordingly. – normanius Oct 14 '22 at 17:53