Python3

Option 1:

pip3 install virtualenv virtualenvwrapper

# modify ~/.zshrc
export WORKON_HOME=/Volumes/sm/repos/venv
export VIRTUALENVWRAPPER_PYTHON=${PY3_HOME}/bin/python3
source ${PY3_HOME}/bin/virtualenvwrapper.sh

mkvirtualenv cv -p python3
workon cv

Option 2(推荐):

python3 -m venv opencv
source opencv/bin/activate

进入虚拟环境

pip install numpy

Build OpenCV

git clone --depth 1 git@github.com:opencv/opencv.git
git clone --depth 1 git@github.com:opencv/opencv_contrib.git
mkdir build ; cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/Users/yakir/local/opencv \
    -D OPENCV_EXTRA_MODULES_PATH=/Volumes/sm/build/opencv/opencv_contrib/modules \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=ON \
    -D BUILD_opencv_apps=ON \
    ../opencv

make -j 14 && make install


# macOS framework
python ../opencv/platforms/osx/build_framework.py osx --macos_archs arm64

versions

  • OpenCV 4.0.0 2018/11/08
  • OpenCV 4.1.1 2019/08/27
  • OpenCV 4.5.4 2021/10/11

Xcode 项目设置

xcode-opencv项目

Reference

Install OpenCV 4 on macOS