macOS下安装Python的OpenCV环境
因为macOS系统(包括Mac OS X系统)自带python安装,但因为操作系统本身依赖且版本较低,这里采用Homebrew安装隔离的python环境,Homebrew的特性这里不作延展,后续专门写一篇介绍.
安装 OpenCV2
brew tap homebrew/science
brew install opencv
安装结束后 opencv会被安装到 /usr/local/Cellar/opencv/2.4.13.2/ 目录
python调用opencv是通过 /usr/local/Cellar/opencv/2.4.10/lib/python2.7/site-packages/ 目录下面的
cv.py
cv2.so
两个文件实现的,这两个其实就是opencv的python对应版本的扩展,将它们与python扩展包引用路径关联起来就可以了
cd /usr/local/lib/python2.7/site-packages/
ln -s /usr/local/Cellar/opencv/2.4.10/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.10/lib/python2.7/site-packages/cv2.so cv2.so
可以在命令行下面执行
import cv2
可以正常引用则安装正确
如果出现以下情况则需要安装 numpy和matplotlib扩展
>> import cv2
ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ImportError: numpy.core.multiarray failed to import
执行下面的命令即可:
pip install numpy
pip install matplotlib
题图:花瓣网