Install OpenCV for Python on Mac OS X
In our previous post we described how to develop driver assistance systems on Android devices. We are using OpenCV SDK for Android, but during the app development we had to test & calibrate the cameras using a computer. Out platform for now is Mac OS X (Yosemite). Therefore, we needed to install & configure OpenCV for Python on Mac.
Install OpenCV
The “traditional” way didn’t work out: downloading OpenCV zip from their website and then compile it & link it with Python. There is a simpler way to do it using Homebrew.
Homerew is a package manager for Mac OS X, more info can be found on their website.
Installing brew is simpler than you think, just run this in terminal:
1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
You might need to install Python first:
1
brew install python
Add OpenCV using the following commands:
1
brew tap homebrew/science
Install OpenCV:
1
brew install opencv
Your OpenCV installation will be located at:
1
/usr/local/Cellar/opencv/2.4.10/
Python setup
The final part is to setup Python, use the following:
1
2
3
cat ~/.bash_profile | grep PYTHONPATH
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
That’s it! If you have issue with Python when running some OpenCV samples, you need to make sure that you have numpy & matplotlib installed. You can do this using brew, in terminal enter:
1
2
pip install numpy
pip install matplotlib
Demo
Here is a simple Python sample to test out that everything works fine:
1
2
3
4
5
6
7
8
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('road.png', 0)
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.show()
More information & sources: