mac+python3.6+opencv

自己的mac上有python2.7和python3.6,平时都习惯于用python3.6,实验需要用到opencv,本来以为直接pip3 install 就可以了,结果发现这样安装的opencv还是python2的版本,python3不能使用,因此又重新找了方法。

step 1

使用终端安装opencv
brew install opencv3 --with-contrib --with-python3 --without-python
注:python2和python3一次只能装一个,用without-python可以跳过python2让opencv装在python3里。
$ brew link --force opencv3

step 2

找到python3类库目录site-packages

$ python
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> print(numpy.__file__)
/Users/zhaofangyu/anaconda3/lib/python3.6/site-packages/numpy/__init__.py

注:numpy是任意import的一个在site-packages里的东西,也可以用其他的代替。

step 3

将cv2链接到python3类库目录下
$ cd /Users/zhaofangyu/anaconda3/lib/python3.6/site-packages/
$ ln -s /usr/local/Cellar/opencv/3.4.0/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so

step 4

测试

$ python
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

推荐阅读更多精彩内容