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:

You might need to install Python first:

Add OpenCV using the following commands:

Install OpenCV:

Your OpenCV installation will be located at:

Python setup

The final part is to setup Python, use the following:

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:

Demo

Here is a simple Python sample to test out that everything works fine:

Check out the new guide on installing Python and OpenCV 3 using Anaconda: here

More information & sources:

  • OpenCV: http://www.opencv.org/
  • Homebrew package manager: http://brew.sh/
  • Install OpenCV for Python on Mac: https://jjyap.wordpress.com/2014/05/24/installing-opencv-2-4-9-on-mac-osx-with-python-support/
  • Calibrate camera with OpenCV & Python: http://www.janeriksolem.net/2014/05/how-to-calibrate-camera-with-opencv-and.html
  • OpenCV Python samples: https://github.com/Itseez/opencv/tree/master/samples/python2
    • Hello,

      thank you for the article, with your help I’ve installed OpenCV with Homebrew.

      In particular, I’ve installed OpenCV 3.0.0-beta because I need some new functions.

      Stuff like SIFT, SURF, BRIEF and FREAK with OpenCV 2.4.10.1 are installed by default in the “nonfree” folder, but with OpenCV 3.0.0 they have been moved to a separate repository called “opencv_contrib” that Homebrew doesn’t install by default.

      So, I would to know if it is possible to install an external repository, like “opencv_contrib”, with Homebrew, and how to do it; or if it is possible to add a repository in another (simple?) way without messing up everything installed with Homebrew.

      https://github.com/itseez/opencv_contrib

      Thank you

      • Hi, I also face the problem. Have you solved it?

        • No, at the end I had to reinstall everything without Homebrew and it has worked.
          However I have a blurred memory of the thing, this was 4 month ago, sorry 🙁

    • I couldn’t get this working with OpenCV2.4.10 or OpenCV2.4.11. However seemed to work OK as soon as OpenCV2.4.11_1 became available and I installed it with Homebrew

    • Will Huang

      Hello,

      it is a great tutorial. But I have the following problems:

      cat ~/.bash_profile | grep PYTHONPATH

      I don’t have .bash_profile in my home directory, is there anything wrong?

      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
      I got “ln: cv2.so: File exists” and “ln: cv.py: File exists”.

      Is it because of the missing bash_profile?

      Thank you!

      • Razvan

        Open terminal, type “cd ~/” and then: “touch .bash_profile” to create the file.

    • DaveGao

      hi!.

      I have installed OpenCV successfully using brew on my mac with OS X 10.10.3. Created the symbolic link as well to the cv.py and cv.so files. when I use the import command in python I receive the following error

      >>> import cv2

      Traceback (most recent call last):

      File “”, line 1, in

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

      Referenced from: /usr/local/Cellar/opencv/2.4.11_1/lib/libopencv_highgui.2.4.dylib

      Reason: image not found

    • rickoshay

      What is the purpose of cat’ing ~/.bash_profile and grep’ing for PYTHONPATH?

    • Lin

      Any suggestions? Thanks!

      • Alex Sobrino

        having the same issue :S

      • Daniela Florit

        I am having this same problem. were you able to get a solution? thanks

    • cgrinaldi

      Thanks for the post! Helped keep me from bashing my head in 🙂

    • lunabus

      Thank you so much for this!

    • where do you want to link the cv.py and cv2.so

      • I mv the cv2.so and cv.py into /usr/local/lib/python2.7/site-packages can solve the link error problem

        • cp /usr/local/Cellar/opencv/2.4.12_2/lib/python2.7/site-packages/* /usr/local/lib/python2.7/site-packages/

          • Ilhom Saidjafar

            Could you explain it step by step, please?

    • S Charlesworth

      What if I don’t want to jack with my existing python setup? Does installing python w/ homebrew pretty much wipe that out?

    • Jamie

      Hey,

      Thanks for the step by step guide.

      It looks like this was written about a year ago – Ive followed your instructions with updated the links for python 3.5 and opencv 2.4.12_2, but when I “import cv2” I get this error:

      ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type

      Any idea how to fix this please?

      Thanks

    • Frank

      Hi, thanks for the convenient tutorial! Unfortunately I got stuck while installing OpenCV using the ‘brew install opencv’ command. Everything seemed to work fine, until the last part of the installation process. I got this error message:

      Linking /usr/local/Cellar/opencv/2.4.13…
      Error: Could not symlink include/opencv2/calib3d/calib3d.hpp
      /usr/local/include/opencv2/calib3d is not writable.

      Does anyone know how to solve this problem?

      Thank you!

      • Brihi

        Hey! You can always change the permissions using sudo chmod 777 /usr/local/include/opencv2/calib3d to make it writeable!

    • I’m guessing that when your instructions show “cat ~/.bash_profile | grep PYTHONPATH”, you actually mean that you should cd into that directory before creating the symlinks. If so, the following would work better: “cd python -c 'import site; site.getsitepackages()'“.

      • Brad Gibson

        Thanks for this Ken.
        I have to install Python 2 (2.7.13) to run a Python2 script, and needed to run the following to get it to work:
        cd python -c "import site; print(site.getsitepackages()[0])"

    • Ron

      thanks for the tutorial 🙂 could you please tell me how to test the code? where am I supposed to execute the sample code?