MacOS 源码安装 OpenCV

在 MacOS 上源码安装 OpenCV 4.0.1

1. 下载源码包和依赖包

2. 安装依赖包

1
2
3
brew install cmake pkg-config
brew install jpeg libpng libtiff openexr
brew install eigen tbb

3. 编译源码包

  • 解压源码包,然后进入源码目录
    1
    2
    3
    4
    5
    6
    7
    8
    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D BUILD_opencv_python2=ON \
    -D BUILD_opencv_python3=ON ..
    make -j8 # 线程数
    sudo make install

4. 关联

  • 找到 cv2.cpython-37m-darwin.so 文件

    1
    /usr/local/Cellar/opencv/4.0.1/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-darwin.so
  • 查看 Python 环境变量

    1
    2
    import sys
    print(sys.path)
  • 找到环境变量中 site-packages 的位置并进入该目录

    1
    /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
  • 关联

    1
    ln -s /usr/local/Cellar/opencv/4.0.1/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-darwin.so

5. 测试

1
import cv2
-------------本文结束 感谢您的阅读-------------
您的支持将鼓励我继续创作!