本文地址:http://www.cnblogs.com/QingHuan/p/7354074.html
轉載請注明本文地址,方便讀者查看本文更新,謝謝!
今天要在Mac上安裝OpenCV,過程非常曲折,充分體會到了每個人的安裝過程都是不同的
具體參考了以下幾篇博客:
[1] http://www.jianshu.com/p/6e345b3aa988
[2] http://www.jianshu.com/p/b5424e9cb7ad
[3] http://blog.csdn.net/willduan1/article/details/53898440
[4] http://nooverfit.com/wp/手把手教你,在ubuntu上安裝opencv-3-0-和-python-2-7/
在參考 [1] 的過程中,還下載了Anaconda,其實最后都沒有用上
另外推薦一本學習Python的書:
https://www.gitbook.com/book/lenkimo/byte-of-python-chinese-edition/details
下面是正式安裝過程,目的是使用PyCharm進行Python開發
// 更新:可以不安裝虛擬環境,直接跳到第二節,選擇系統默認的python2.7即可
// 再更新,其實可以在Pycharm里配置安裝虛擬環境(實在太贊了),如下圖

第一節 安裝虛擬環境
參考 [2] 和 [4] ,感謝原作者!
安裝virtualenv和virtualenvwrapper. 用來分割python虛擬環境. 這不是必須的, 但是強烈推薦:
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
如果報以下的錯誤:
Operation not permitted: '/tmp/pip-bxaFhj-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
參考[7] https://github.com/pypa/pip/issues/3165
輸入
sudo pip install virtualenv virtualenvwrapper --upgrade --ignore-installed six
即可
原因是:
This is because OS X El Capitan ships with six 1.4.1 installed already and when it attempts to uninstall it (because awscli depends on botocore, botocore depends on python-dateutil, and python-dateutil depends on six >= 1.5) it doesn't have permission to do so because System Integrity Protection doesn't allow even root to modify those directories.
Ideally, pip should just skip uninstalling those items since they aren't installed to site-packages they are installed to a special Apple directory. However, even if pip skips uninstalling those items and installs six into site-packages we'll hit another bug where Apple puts their pre-installed stuff earlier in the
sys.paththan site-packages. I've talked to Apple about this and I'm not sure if they're going to do anything about it or not.
也就是six的版本問題
現在我們有了virtualenv和virtualenvwrapper, 我們要更新我們的~/.bashrc 文件:(Mac中為.bash_profile)(修改原因參考 [5] )
在Mac中: vi ~/.bash_profile 如果提示Read-only file,不允許寫入的話,執行: chmod a+w ~/.bash_profile 如果在Linux系統中: vi ~/.bashrc
然后將下面的幾行復制到最末尾
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
為了使~/.bashrc 文件生效 , 你可以用以下這些方法的其中之一(1) 注銷后重新登錄, (2)關閉終端開一個新終端, (3)直接使得~/.bashrc文件在當前生效,執行:
$ source ~/.bashrc
最后我們生成虛擬開發環境:
$ mkvirtualenv python2.7forOpencvExp
1.1 安裝Python
安裝python包管理器pip:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
安裝Python-2.7.13:
brew install python
安裝好后的python路徑為 /usr/local/Cellar/python/2.7.13_1
可執行文件的位置在 /usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7
可以參考 [6] 來修改虛擬環境使用的python版本,
你可以選擇使用一個Python解釋器(比如``python2.7``):
$ virtualenv -p /usr/bin/python2.7 my_project
如果想查看全局的python位置,輸入
whereis python
第二節 在PYCHARM中進行配置:
1. 點擊左上角的Pycharm Edu ==> Preferences ==> Project Interpreter,如圖

選擇解釋器為虛擬環境中安裝的那個(圖中沒選對,下面也是,當時安裝的是python3.6但是發現不好用)。
2. 點擊左下角的“+”加號,進入PYTHON的包管理界面

搜索Opencv,選中opencv-python,點擊Install Package;同理再安裝opencv-contrib
3. 完成最后的配置,還是在Preferences內,選擇Build, Execution, Deployment ==> Console ==> Python Console,
選擇Python Interpreter為虛擬環境中安裝的那款,如圖

然后點擊OK進行確定
4. 測試:在文本框內輸入如下命令:
import cv2
print(cv2.__version__)
運行后得到當前Opencv版本為3.3.0,如圖

至此就安裝好了!
--- THE END ---
參考文獻:
[1] http://www.jianshu.com/p/6e345b3aa988
[2] http://www.jianshu.com/p/b5424e9cb7ad
[3] http://blog.csdn.net/willduan1/article/details/53898440
[4] http://nooverfit.com/wp/手把手教你,在ubuntu上安裝opencv-3-0-和-python-2-7/
[5] http://elf8848.iteye.com/blog/1582137
[6] http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html
