Installing and testing OpenCV/Python (Spring 2017 edition)

First, select your platform:


Mac OS X

I recommend using MacPorts to install its own versions of Python and OpenCV, but students have also reported success with HomeBrew as well.

MacPorts
  1. Follow the directions to install MacPorts on your system. Please note this requires downloading and installing XCode, which may take a long time on your computer!
  2. After the installation completes, open the terminal and run the commands
    sudo port install py27-numpy
    sudo port install opencv +python27
    sudo port select --set python python27
Homebrew

If you're already a Homebrew user, you probably know what you're doing, but feel free to drop me a line if it's not working for you. Otherwise, I'd suggest MacPorts.

Verifying the installation

Either way, test the install by running python in the Terminal. In the interactive Python session, type

import cv2
The statement should complete with no errors. If it works, you're all set!


Windows (64-bit only)

If you have a 32-bit Windows install, see me.

  1. Download and run the Windows 64-bit Python 2.7 installer for the Anaconda distribution from this page. It should be fine to accept the default options, unless you already have a different version of Python installed, in which case you may not want to update the path and register Anaconda as the default version of Python.
  2. Download the latest version (3.1) of OpenCV for Windows from this page. Run the installer and choose to extract to the location C:\. It should create an C:\opencv directory for you and stuff it with lots of goodies.
  3. Go to the Start Menu and find the newly installed Anaconda Prompt and then type or paste (by right-clicking) the following set of commands, one line at a time:
    cd %CONDA_DEFAULT_ENV%
    copy C:\opencv\build\x64\vc12\bin\opencv_ffmpeg310_64.dll .
    cd Lib\site-packages
    copy C:\opencv\build\python\2.7\x64\cv2.pyd .
    All of these commands should complete without error messages.
  4. Finally, test the install by running python in the Anaconda command prompt window. In the interactive Python session, type
    import cv2
    The statement should complete with no errors. If it works, you're all set!

Ubuntu Linux

This will work for Ubuntu 12.04 or 14.04. For older versions, see me.

  1. Install the libopencv-dev and python-numpy packages using apt-get or your favorite package manager.
  2. Run python in the terminal. In the Python session, type
    import cv2
    The statement should complete with no errors. If it works, you're all set!

Please note: your distibution's OpenCV version might be slightly out of date compared to the Mac and Windows users, so you may have to make slight modifications to the sample code to get it to run correctly. Ask me if you have questions/issues.

Back to E27 course page.