Install OpenCV on your Mac OSX with Python

In this post, I am going to tell you how to install OpenCV on your Mac OSX, so that you can import cv in your python code. In my case, I’ll be installing OpenCV 2.4.12 and running python 2.7 on my Mac OSX. The version of OpenCV will keep updating, so in the following procedure you might need to change the version number. Otherwise, there might be some errors installing.

Let’s get started.

Step 1. Install Homebrew.
Homebrew includes some packages that Mac OSX doesn’t include, so it is better to install Homebrew and get the extra packages. This is the link to install Homebrew:

http://brew.sh/index.html

In this website, the only thing that you’ll need to do is to copy the code below:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Go to your terminal, then type “bash” and press enter.You’ll be now using bash shell to install Homebrew.

Now paste the code that you just copy and press enter. It should start to install Homebrew. You’ll need to type your password while installing.

 

 

Step 2. Install OpenCV on your Mac OSX.

The following procedure is followed by the website here:

https://jjyap.wordpress.com/2014/05/24/installing-opencv-2-4-9-on-mac-osx-with-python-support/

In case you have any problem installing, remember to go to the website and check the details.

Under the bash shell, copy and paste the code below and enter:

brew tap homebrew/science

Then copy and paste the code below and enter:

brew install opencv

You can find your OpenCV at the code below. OpenCV will keep updating its version, so you might need to change the number of version rather that “2.4.12”.

cd /usr/local/Cellar/opencv/2.4.12/

 

 

Step 3. Setting up python
Go to your python path by typing the code below:

cat ~/.bash_profile | grep PYTHONPATH

Go to the package folder of python: (If you are not using python 2.7, you might also need to change the number of version)

cd /Library/Python/2.7/site-packages/

Create a symlink by typing the code below, and you’ll need to type “sudo” beforehand to run the code as a administrator. Later on type you’ll password to run. You might also change the number of version in the code below.

sudo ln -s /usr/local/Cellar/opencv/2.4.12/lib/python2.7/site-packages/cv.py cv.py

Type and run the code below:

sudo ln -s /usr/local/Cellar/opencv/2.4.12/lib/python2.7/site-packages/cv2.so cv2.so

Congratulations! You are done!

 

 

Step 4. Check if your Mac OSX can import OpenCV

Run the python by merely typing python:

python

You’ll see something similar like this.

Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Now, you are running python on your terminal. Run the code below and see if OpenCV successfully be installed.

import cv

or

import cv2

If there is no error pop out, then you had completely installed.

 

 

Appendix:

There is an error when I am installing OpenCV. The terminal says that one of my packages is not up to date, so I’ll need to first upgrade the package. In my case, the numpy is not the latest version. You should change “numpy” to other package name.

pip install numpy --upgrade

After that, you should be able to install OpenCV on your Mac OSX.

 

 

 

Ting-Hao Chen
Feb 23, 2016

Advertisements

One thought on “Install OpenCV on your Mac OSX with Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s