Installing OpenCV 2.4 on Mac OS X Lion

Tuesday April 16, 2013

Some quick help on installing OpenCV on Mac Lion. I had a lot of trouble for a long time, and the default installation instructions had me keep on getting Segmentation Fault: 11 when I would try to import cv2. I discovered the default installation instructions create a reference to the incorrect version of Python. Here's how to fix that, playing off the regular build instructions.

mkdir build
cd build
cmake -D PYTHON_EXECUTABLE=/Library/Frameworks/Python.framework/Versions/2.7/bin/python \
 -D PYTHON_PACKAGES_PATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ \
 # this is of course optional
 -D INSTALL_PYTHON_EXAMPLES=ON \
 -D PYTHON_INCLUDE_DIR=/Library/Frameworks/Python.framework/Versions/2.7/Headers/ \
 -D PYTHON_LIBRARY=/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib \
..
make -j8 # use all your cores. I have 8, so that's my number. Use the appropriate number here.
sudo make install

and then you can

import cv2

to your hearts content!

If you're curious, here's how I figured it out. cmake is a system that builds a bunch of Makefiles. I looked in the build directory, where cmake puts all of its output. I grepped around a bit and eventually looked at this file, build/cmakecache. If you grep that for dylib or python, you'll see it's referencing the wrong thing. In my case, it referenced the default Apple install of Python, not what I desired, since I installed Python 2.7 from Python.org. So, fixing this incorrect reference then fixes the problem. A mystery solved!


Want to receive similar articles? (No spam, promise!)