Skip to content

Instantly share code, notes, and snippets.

@jruizvar
Last active February 27, 2021 12:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jruizvar/0535fb8612afb105e0eef64051dc0d00 to your computer and use it in GitHub Desktop.
Save jruizvar/0535fb8612afb105e0eef64051dc0d00 to your computer and use it in GitHub Desktop.
Building OpenCV 3.2.0 from source on macOS Sierra with Python 3 support

Building OpenCV 3.2.0 from source with Python 3 support

Install OpenCV on macOS Sierra enabling Python 3 with the following instructions:

  • Install CMake, Python 3 + Numpy in advance
  • Download latest OpenCV source code (https://github.com/opencv/opencv/releases)
  • Move the folder opencv-3.2.0 to the current directory
  • In the current directory, execute the following steps:
mkdir build
cd build
cmake -DPYTHON_DEFAULT_EXECUTABLE=python3 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_opencv_python2=False -DBUILD_opencv_python3=True -DPYTHON3_EXECUTABLE=python3 ../opencv-3.2.0
make -j2
sudo make install
echo /usr/local/lib/python3.6/site-packages >> /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/opencv3.pth
  • Verify installation
python3 -c "import cv2; print(cv2.__version__)"

For python2 support

cmake .. -DBUILD_opencv_python2=True -DPYTHON2_LIBRARY=$CONDA_PREFIX/lib/libpython2.7.dylib -DPYTHON2_INCLUDE_DIR=$CONDA_PREFIX/include/python2.7 -DBUILD_opencv_python3=False -DCMAKE_CONFIGURATION_TYPES=Release -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX -DMKL_INCLUDE_DIRS=/opt/intel/mkl/include -DMKL_ROOT_DIR=/opt/intel/mkl -DWITH_CUDA=OFF -DWITH_CUFFT=OFF -DWITH_MATLAB=OFF
@javadba
Copy link

javadba commented May 24, 2019

made following tweak for g++-9.1: add -DCMAKE_CXX_COMPILER=/usr/local/gcc-9.1/bin/g++-9.1

cmake -DPYTHON_DEFAULT_EXECUTABLE=python3 -DCMAKE_CXX_COMPILER=/usr/local/gcc-9.1/bin/g++-9.1 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_opencv_python2=False -DBUILD_opencv_python3=True -DPYTHON3_EXECUTABLE=python3 ../

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