在Mac上安装opencv

原文:Mac 安装opencv,并与Python做关联

1. 使用Homebrew安装

brew install python3 # 必须要安装,尤其要做好 brew link python3
brew install opencv

最新版本的opencv已经不分opencv3了,而是在目录下分成python2python3版本的文件夹。
安装好的opencv库在/usr/local/Cellar/opencv/

不同版本分别在

# python3
opencv/3.4.1_5/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so
# python2
opencv/3.4.1_5/lib/python2.7/site-packages/cv2.so

2. 进入python 的site-packages 目录下制作替身

cd 到Python的site-packages中
ln -s 目标文件 文件名称

# 例如:
cd my_tf/lib/python3.5/site-packages  
ln -s /usr/local/Cellar/opencv3/3.1.0_4/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so cv2.so

3. 依赖包

这时候试试看是否成功

Python 3.5.3 |Anaconda custom (x86_64)| (default, Mar  6 2017, 12:15:08) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
RuntimeError: module compiled against API version 0xc but this version of numpy is 0xb
Traceback (most recent call last):
  File "", line 1, in 
ImportError: numpy.core.multiarray failed to import

升级响应的依赖包

pip install --upgrade numpy

成功

Python 3.5.3 |Anaconda custom (x86_64)| (default, Mar  6 2017, 12:15:08) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

你可能感兴趣的:(在Mac上安装opencv)