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
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 ../