Installing OpenCV 2.4.8 on Mac OSX with Python Support

This is now outdated as Brew finally added opencv 2.4.9, See here

1. First of all I’ll be using the package manager Homebrew to simplify things, you can get it here:

The reason I’ve decided to use Homebrew was because it installs packages into their own directory which makes everything nice and easy to find.  I tried macports but didn’t like it.

2. Once you have brew installed you can go ahead and and add homebrew/science which is where OpenCV is located using:

brew tap homebrew/science

3. As of the time of writing Homebrew only supports OpenCV 2.7.1 so we need to make a few changes:

brew edit opencv

The ‘formula’ for OpenCV should now be open in whatever you set the default editor to be, it’s vim by default but if you want to change it you can run the following replacing ’emacs’ with whatever your favorite text editor is.

echo 'export EDITOR=emacs' >> ~/.bash_profile
source ~/.bash_profile

4. Look for the following lines

require 'formula'

class Opencv < Formula
  homepage ''
  url ''
  sha1 'b6b0dd72356822a482ca3a27a7a88145aca6f34c'

  option '32-bit'

Go ahead and replace the highlighted lines with

url ''
sha1 '61b95974bf9689b94b169c39aed6c85a2e124837'

5. If you want to see other possible options such as when compiling (such as using tbb) you can use info

brew info opencv

Go ahead and install OpenCV now

brew install opencv

6. You’re done! You can find OpenCV at

cd /usr/local/Cellar/opencv/2.4.8/

Setting up Python

1. Navigate to your python path, if you don’t know where it is when you installed Python you can find it in your .bash_profile or using

cat ~/.bash_profile | grep PYTHONPATH

in my case we’ll be using

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

2. Once there we need to link our compiled OpenCV files, create a symlink using

ln -s /usr/local/Cellar/opencv/2.4.8/lib/python2.7/site-packages/
ln -s /usr/local/Cellar/opencv/2.4.8/lib/python2.7/site-packages/

3. You’re done! You can make sure it works by running python and typing

import cv

Tags: , , , ,

About jjyap

I'm an undergraduate student at Simon Fraser University. A lot of the fun projects and research that I work on are Computer Vision and Machine Learning related.

14 responses to “Installing OpenCV 2.4.8 on Mac OSX with Python Support”

  1. selectersky says :

    brew install current version is 2.4.9

  2. iUwej says :

    Awesome article. Though I didn’t have to edit the opencv formula as it currently support 2.4.9

  3. Glyn Jackson (@glynjackson) says :

    You saved me hours of work, thanks for this post!

  4. Jim says :

    You are awesome. I will make you a peanut butter sandwich.

  5. freerefillsohyeah says :

    damn, you’re dope. thanks for this careful writeup.

  6. Aron Hjartarson says :

    hey there! thanks for a great writeup, I get the following error message, was wondering if you could help :

    bash-3.2$ brew install opencv
    opencv: Unsatisfied dependency: numpy
    Homebrew does not provide Python dependencies; install with:
    pip install numpy
    Error: An unsatisfied requirement failed this build.

    already ran pip install numpy and seems ok. On yosemite on a 2011 macbook pro

    • Joseph Gimenez says :

      I ran into this issue as well and after running “brew edit opencv” I changed line #26 to read: #depends_on “numpy” => :python (essentially commenting out the line)

      this appears to have allowed the library to build successfully.

  7. mikeill says :

    Thank you for this! I’m having an interesting issue where the OSX console is unresponsive when using interactive samples from (for example python2/

    • mikeill says :

      the reason for this was simply that the “window” displaying the images was not “in focus”, which needs to be the case for the “waitKey” buttons to receive input.

  8. mikeill says :

    ignore please – just for notify me signup

  9. Vipul says :

    /Users/apple/.bash_profile: No such file or directory
    Cant link ocv with python

    • jjyap says :

      Maybe it’s in your .bashrc or you’re using Anaconda? If you already know the path to your python installation just change directories there then continue adding the symlinks

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: