Installing OpenCV 2.4.9 on Mac OSX with Python Support

**OpenCV is now at 3.0.0! :), I will write up a guide on that when I get around to installing it

*Here is the updated version of my previous post since Brew finally added the most recent version of OpenCV!

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

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. 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

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

cd /usr/local/Cellar/opencv/2.4.9/

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.9/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so

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

import cv

70 responses to “Installing OpenCV 2.4.9 on Mac OSX with Python Support”

  1. Aurora says :

    I am stuck on the last step. When I try to make the symlink using that command, I get a message which says: ln: cv.py: Permission denied

    I followed all the steps exactly so I’m not really sure whats going on. Thank you!

  2. Paula Clerkin says :

    Hi,
    I’m having trouble with the final part of importing cv. Initially I thought it was because I kept getting “import: command not found” and I tried everything. Now I found your blog, I tried it all out and when I try to do part 2 of the Python section, I get ” Permission denied”.
    Is there anyway you could help me? It is installed successfully it’s just part 2 and 3!
    Thanks,
    Paula

  3. 黄东 says :

    Pretty helpful! THX!!

  4. akrita agarwal says :

    dylib file missing error.

    This is what my bash looks like –

    hogwarts:MapNetwork Akrita$ python sampleOCV.py
    Traceback (most recent call last):
    File “sampleOCV.py”, line 1, in
    import cv
    File “/Volumes/PrivetDrive/Copy/UCincy/cchmc/MapNetwork/cv.py”, line 1, in
    from cv2.cv import *
    ImportError: dlopen(/Volumes/PrivetDrive/Copy/UCincy/cchmc/MapNetwork/cv2.so, 2): Library not loaded: /opt/local/lib/libjasper.1.dylib
    Referenced from: /usr/local/Cellar/opencv/2.4.9/lib/libopencv_highgui.2.4.dylib
    Reason: image not found

    Python file – sampleOCV.py
    location – /Volumes/PrivetDrive/Copy/UCincy/cchmc/MapNetwork/
    python and opencv were installed using macports but now switched to homebrew using this tutorial.
    mac OS X version 10.9.4
    python 2.7

    Can you help me?
    Thanks

  5. diegoram says :

    Are you sure this actually works? Every time I install opencv using homebrew, turns out there is no python packages at nowhere. I mean, this directory has never created /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/

  6. Siling says :

    Thanks! It really works well(:

  7. Colleen says :

    Hi,
    I followed the instructions here and when I tried to import cv, I got a segfault: 11. Any idea what might have gone wrong?
    Thanks!

  8. Sara Sanna says :

    Thanks man! This save me a lot of time 😉

  9. Andrew says :

    OMG a tutorial that is not out of date!!! You rock.

  10. holdenweb says :

    This is a good tutorial, but I’m rather disappointed to find that the Python bindings a) aren’t separate from the OpenCV software itself and b) can’t be simply installed from PyPI with pip or similar. This would make it possible to support different versions in different Python virtual environments, as well as setting the stage for a Python 3 version.

  11. holdenweb says :

    Further notes on my attempt to use the instructions given:

    1. The article suggests using the command “cat ~/.bash_profile | grep PYTHONPATH” to locate the installed Python. Ignoring the fact that virtual environments can mean there are many Pythons installed, the PYTHONPATH environment variable does not in any way locate the installed Python. If you want to know where your current Python is, “which python” will do that.

    2. There is no /usr/local/Cellar/opencv/2.4.9/lib/python2.7/ directory on my system after installation of OpenCV using brew. I do see usr/local/Cellar/opencv/2.4.9/lib/ but it has no “python*” subdirectory.

    Please let me know if you’d like me to raise these issues against the project – I an guessing things may have moved on a little since you wrote the article.

  12. peterbengtsson says :

    What @holdenweb said. As of `brew install opencv` there is Cellar/opencv/2.4.9/lib/ and but no Cellar/opencv/2.4.9/lib/python.

    What does one pick up the Python bindings these days?

  13. bowonc says :

    Reblogged this on Repository of my past writings and commented:
    Opencv tool for python

    • mikeill says :

      Don’t think the reblogging contains the “code”. Also I’m having an interesting issue where the console is unresponsive when using interactive samples from http://sourceforge.net/projects/opencvlibrary/files/

      • bowonc says :

        Thanks for your reply. At the moment I reblogged your post at my blog, I had trouble while installing opencv on my laptop. . I had to keep your post for my future that may needed for confirming my whole past installation process. Code was secondary subject for me!

  14. Nishant Singh says :

    Thanks a lot!

  15. sdv says :

    I did all of this, but I keep getting the error:

    Raise ImportError(“Cannot load OpenCV library which is required by SimpleCV”)
    ImportError: Cannot load OpenCV library which is required by SimpleCV

  16. yw1hbmk says :

    Hi there, thanks for the tutorial!
    I followed everything through, but when I follow up and run python on terminal and try to import cv, it tells me that the module doesn’t exist.
    Any tips?

  17. Phil says :

    When I run “brew install opencv”, it gets stuck on “make bootstrap” for a REALLY long time. My computer gets really hot/loud too. Seems to be crashing. Any idea what might be causing this?

    • jjyap says :

      Your computer is getting hot because you’re compiling opencv, it takes quite a long time so just leave your laptop and go watch a movie and come back in a few hours. You could also use your hot laptop as an electric blanket, or as a warming for plate when you make too much pizza but know you’ll get hungry in an hour from all this installing, put your coffee on the palm rest to keep it warm while you play tower defense games on miniclip, read a book while you wait but make sure it doesn’t touch your laptop and catch fire, call Tim Cook and tell him what you think about the iWatch, maybe go camping and bring your laptop as a portable always-lit fire starter.

      Hope this helps if you need more ideas please let me know.

  18. Joseph says :

    The step brew install opencv failed for me. Any thoughts?

    ==> Installing opencv dependency: homebrew/python/numpy
    Error: undefined method `homepage’ for Numpy:Class
    Please report this bug:
    https://github.com/Homebrew/homebrew/wiki/troubleshooting
    /usr/local/Library/Taps/homebrew/homebrew-python/numpy.rb:2:in `’
    /usr/local/Library/Taps/homebrew/homebrew-python/numpy.rb:1:in `’

    • jjyap says :

      Maybe you don’t have numpy installed? Try install it using homebrew and make sure it’s linked to the same version of Python you’re using to compile OpenCV.

  19. Jolle Jolles (@Mudfooted) says :

    Hi. I followed all your steps but can only read certain video files. For some if I run “import cv2; cap = cv2.VideoCapture(‘video.avi”);cap.read()” it just shows False,None, while trying this on a different computer it shows a numpy array. I have tried to fix it for hours but don’t succeed. Do you have any ideas why it only reads certain video files?

    • jjyap says :

      Ya it’s likely because you don’t have the required codecs installed. Download and install ffmpeg and it should work, if not you may need to recompile OpenCV after you’ve installed ffmpeg for it to work. Look closely at the output of make when compiling OpenCV and it should tell you if it can find ffmpeg on your system.

  20. jrdnshw says :

    I was running into the following error following the above steps

    ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: /usr/local/lib/libImath-2_2.12.dylib

    had to brew unlink openexr than brew install openexr and than i was good to go.

    hopefully this is helpful for others…

  21. David Palmer says :

    If you are using macports, the complete procedure is:

    % sudo port install opencv +python27

    Unfortunately, python3 is not supported yet by macports, even though opencv is supposed to support it.

    I prefer macports to home-brew because it keeps everything in the macports /opt directory, instead of stomping on /usr/local, so opinions differ.

  22. offbooks says :

    Thank you muchly!

  23. metacowboy says :

    Reblogged this on Metaprime Studio.

  24. szsy1985 says :

    Reblogged this on 正我.

  25. Soon Chye says :

    Thank you very much for the help!

  26. giacomoalberiniomo says :

    Thank a lot for your help, really appreciated! It worked fine for me, following both your instructions and the tips in the comment! Keep on the good work! Cheers from Italy!

  27. Srinidhi Cillanki says :

    thank you so much there…:) helped me a lot…..:D you are a life saver 😀

  28. Thomas Kilian says :

    Excellent! Worked like a charm!

  29. ab1983m says :

    Hi there, I follow each step for my mac but when I run import cv the python2.7 output is this error messagge
    >>> import cv

    Traceback (most recent call last):
    File “”, line 1, in
    import cv
    ImportError: dynamic module does not define init function (initcv)

    could please somebody help me.

  30. Arjun Ahuja says :

    Thanks for the tutorial!!

  31. Jim Schrempp says :

    You, my friend, rock! I’ve been trying to install openCV on my Mac for a week now. I tried following the instructions on the openCV site and they didn’t work. I even tried compiling openCV following their instructions, no love. I searched google and tried a couple of different recipes from who knows who – didn’t work. Your instructions, however, worked the first time I followed them correctly. 😉

    Thank you so much

    • jjyap says :

      Thank you! This is exactly the reason I made this guide and I’m glad you found it useful! I do have to update it for OpenCV 3.1 in my lack of spare time but the gist of it should still be the same.

  32. umarqattan says :

    Thank you for this! Just make sure when run the following lines:
    ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
    ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so

    to change the number 2.4.9 to what your current version is (mine was 2.4.13.2)

    Other than that, this was a great tutorial and I finally got cv and cv2 to work in python2.7.

Leave a reply to mikeill Cancel reply