OpenCV + Python 2.6 (on OS X)

So, I was trying to get OpenCV and NumPy to work together on my Mac (10.6). Problem was, NumPy wanted to live on Python 2.6, and OpenCV wanted to live on Python 2.7. Much misery was had.

Solution I found (finally!) was:

  1. Install NumPy. (I can’t quite remember how to do this. Just do what the instructions say.)
  2. Install OpenCV. Per the instructions, the steps are as follows:
    cd ~/<my_working_directory>
    svn co https://code.ros.org/svn/opencv/trunk
    cd ~/<my_working_directory>/trunk/opencv # directory should contain INSTALL, CMakeLists.txt etc.
    mkdir release
    cd release
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D PYTHON_EXECUTABLE=/usr/bin/python2.6 ..
    sudo make install

    Note the BUILD_PYTHON_SUPPORT and PYTHON_EXECUTABLE flags to cmake. In the output to cmake, you should see:

    Python: ON
    Python interpreter: /usr/bin/python2.6
    Python numpy: YES

    under “Interfaces:”.

  3. Test the install. Launch Python, and run:
    import numpy
    import cv

    Nothing bad should happen!

(Make sure the “release” directory is nice and clean before you run cmake & make. At one point, my release directory was in some weird state so that after I ran make, “import cv” gave me a segfault. Clearing out the directory and rerunning the install process made everything work!)