Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save welch/6468594 to your computer and use it in GitHub Desktop.
Save welch/6468594 to your computer and use it in GitHub Desktop.
How to to build OpenCV 2.4.5 from the distribution tarball using cmake, on Mac OSX 10.8.4, linked to an anaconda installation.
It is a rite of passage to post one's successful build instructions for OpenCV on a Mac
after you've tried all the other conflicting instructions out there and still failed.
brew failed for me (was this because I could never get a happy brew doctor situation?
I'll never know). macports? nope. build-from-source recipes? I didn't find one that
worked for me.
Here's what did work to build OpenCV 2.4.5 from the distribution tarball using cmake,
on Mac OSX 10.8.4, linked to an anaconda installation rather than the system python.
It is a mashup of various bits of advice out there. If you're already comfortable with
build/install from source, all you need to read is the cmake invocation in step 3 and
the library path hack in step 6. For the rest of us, here are the details on what to type:
0: get yourself an updated compiler/linker by installing Xcode (google that), launching it,
and choosing Xcode/Preferences. Install/Update the Commandline tools. I
Verify your setup:
$ c++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.1
Thread model: posix
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
1: If cmake is not installed on your machine, get it. brew install cmake worked for me, but you can google
yourself to success there.
Verify your setup:
$ cmake --version
cmake version 2.8.11.2
2: Download the distribution tarball from here and unpack it where you please:
http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.5/opencv-2.4.5.tar.gz/download
$ tar xvfz opencv-2.4.5.tar.gz
3: Set up a release/build directory inside the unpacked distribution and run cmake:
$ cd opencv-2.4.5
$ mkdir release
$ cd release
$ cmake -DPYTHON_INCLUDE_DIR=/Users/smallberries/anaconda/include/python2.7/ -DPYTHON_LIBRARY=/Users/smallberries/anaconda/lib/libpython2.7.dylib -DPYTHON_EXECUTABLE=/Users/smallberries/anaconda/bin/python -DPYTHON_PACKAGES_PATH=/Users/smallberries/anaconda/lib/python2.7/site-packages/ ..
Some notes about that cmake line:
* it's one long line, a bunch of "-D" options which will probably be displayed in this page with
deceiving linebreaks.
* It assumes your username is smallberries and you have installed your own anaconda distribution
in your toplevel user directory. If either of these is not true, change the paths in the cmake accordingly.
* The '..' at the end is real important, not a typo.
Verify output:
There will be many lines of "Looking for ...", occasional "not found", but watch closely for this line:
-- Found PythonInterp: /Users/smallberries/anaconda/bin/python (found suitable version "2.7.5", minimum required is "2.0")
-- Found PythonLibs: /Users/smallberries/anaconda/lib/libpython2.7.dylib (found suitable exact version "2.7.5")
If instead an error complains that no compatible version was found, rats, this recipe cannot help you further.
4: let's build it!
$ make
Expected output:
It will go on for days, with many successes and a few warnings (ranlib warnings about 0-length libraries
you don't care about, cap_qtkit.mm warnings, surf.ocl.cpp warnings, a NumPY decprecated API warning),
but NO ERRORS. If you are getting errors, rats. I'm no further help.
My final line of output looked like this:
[100%] Built target opencv_traincascade
5: Install it!
$ sudo make install
Expected output:
A long list of lines like this:
-- Installing: /usr/local/bin/opencv_traincascade
No errors or warnings though.
Verify successful installation:
$ ipython
Python 2.7.5 |Anaconda 1.6.0 (x86_64)| (default, May 31 2013, 10:42:42)
In [1]: import cv2
Segmentation fault: 11
Ok, that wasn't much fun. But we have verified that Important Stuff is in the right place
(if you instead see ImportError: No module named cv2, then Important Stuff is not in
the right place). Try this too and verify you got the same stuff in your cv2.so:
$ otool -L /Users/smallberries/anaconda/lib/python2.7/site-packages/cv2.so
/Users/smallberries/anaconda/lib/python2.7/site-packages/cv2.so:
cv2.so (compatibility version 0.0.0, current version 0.0.0)
libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
lib/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_flann.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_imgproc.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_video.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_ml.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_highgui.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_features2d.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_calib3d.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_photo.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_objdetect.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_contrib.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_legacy.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_gpu.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_ocl.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_nonfree.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
/System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
6. Fix the anaconda library reference in the cv2.so library (If you know how to do this via the build
system rather than by directly hacking the .so, do tell, as this is patently awful):
$ sudo install_name_tool -change libpython2.7.dylib /Users/smallberries/anaconda/lib/libpython2.7.dylib /Users/smallberries/anaconda/lib/python2.7/site-packages/cv2.so
Verify successful library hackery:
$ otool -L /Users/smallberries/anaconda/lib/python2.7/site-packages/cv2.so
/Users/smallberries/anaconda/lib/python2.7/site-packages/cv2.so:
cv2.so (compatibility version 0.0.0, current version 0.0.0)
/Users/smallberries/anaconda/lib/libpython2.7.dylib (compatibility version 2.7.0, current version 2.7.0)
lib/libopencv_core.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_flann.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_imgproc.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_video.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_ml.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_highgui.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_features2d.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_calib3d.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_photo.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_objdetect.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_contrib.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_legacy.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_gpu.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_ocl.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
lib/libopencv_nonfree.2.4.dylib (compatibility version 2.4.0, current version 2.4.5)
/System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
Verify ultimate success!
$ ipython
Python 2.7.5 |Anaconda 1.6.0 (x86_64)| (default, May 31 2013, 10:42:42)
In [1]: import cv2
In [2]: help(cv2)
Help on module cv2:
NAME
cv2
FILE
/Users/smallberries/anaconda/lib/python2.7/site-packages/cv2.so
... etc ...
Yay! All done! Now go install SimpleCV on top of that (another post for another day)
@somada141
Copy link

Hands down the only solution I've found to building SimpleITK with Anaconda on the whole bloody net!!! You are my hero! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment