This post addresses how to get the OpenCV vision package with Python bindings to compile on OS X, such that you can use a version of Python other than the one that is supplied with the original operating system installation. It's very hacky and not for casual reading, but my help some person questing for help.
The problem is that OpenCV insists on being linked with the standard default installation of Python on OS X. On the other hand, many (most?) techie people using Python will have installed an alternative newer release (for instance via "fink" or "macports").
When you compile OpenCV and try to use it with your custom-installed Python (e.g. version 2.5.2 instead of the stock 2.5.1), you get an error as follows:
% python
>>> import opencv
Fatal Python error: Interpreter not initialized (version mismatch?)
Abort
Using the standard Python is a workaround, but for me it wasn't acceptable. Unfortunately, since I had no prior experience with cmake or opencv, this was problematic. Here's how to fix it. Despite many only-semi-correct discussions of this on the net, it's fairly easy to fix, once you realize the source of the problem.
On OS X (or, in fact, the core operating system kernel, Darwin), many packages are installed using Frameworks. They group together libraries and headers. The C compiler on Darwin has been extended to take a -framework PATH flag, and by default this causes the linker to look in the system directories, but not user-installed directories. You need to provide an extra -F DIRECTORY flag to specific the location of your user-installed frameworks (e.g. "-F /opt/local/Library/Frameworks/").
I never quite figureed out the "right" place to insert the flag before starting a build, but after a failed build you can just insert it into
interfaces/swig/python/CMakeFiles/_highgui.dir/link.txt
The right place is probably interfaces/swig/python/CMakeLists.txt
using something like
set(CMAKE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -F/opt/local/Library/Frameworks")
but once I got it working I didn't want to waste more time on a really clean fix (instead, I wasted time writing it up here).
When you run make, in order to assure it's working you can set the system frameworks to be temporarily inaccessible to verify the right places are being searched. Do this with the chmod command. Afterwards, you can put things back the way they were (mode 755).
I suggest you test your patch as follows:
sudo chmod 000 /System/Library/Frameworks/Python.framework/
cmake .....
sudo make install
sudo chmod 755 /System/Library/Frameworks/Python.framework/
As an aside, I was also confronted by the error:
opencv/src/highgui/cvcap_ffmpeg.cpp:70:28: error: ffmpeg/swscale.h: No such file or directory
this can be fixed by the hack:
cp -r /opt/local/include/libswscale/* /opt/local/include/ffmpeg