Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
@rouseguy

This comment has been minimized.

Copy link

commented Jan 21, 2014

Thanks for a detailed post. I am stuck in step 6 now. I followed the exact same commands as mentioned above -and the cv2.so ended up in the folder: /Users/rouseguy/Documents/Python/OpenCV/opencv-2.4.7.1/release/lib. I am not confident with Terminal commands and am unable to figure out how to modify the command in step 6.

Could you suggest me how I should proceed next? (No issues in installation. Everything fine. Just unable to import cv2 in python -because the file is not present in site-packages)

EDIT: Figured out. Did two things. Copied cv2.so to anaconda's site-package folder. And ran the command in line 6. And cv2 works.

Great article !

@slavasav

This comment has been minimized.

Copy link

commented Jan 27, 2014

Thank you so much for this!

@scollis

This comment has been minimized.

Copy link

commented Jan 28, 2014

Hey! Great howto.. any idea with this error?

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../lib/libIlmImf.a(IlmThreadSemaphorePosixCompat.cpp.o) has no symbols
[ 22%] Built target IlmImf
[ 23%] Building NVCC (Device) object modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_matrix_operations.cu.o
clang: error: unsupported option '-dumpspecs'
clang: error: no input files

CMake Error at cuda_compile_generated_matrix_operations.cu.o.cmake:206 (message):
Error generating
/Users/scollis/src/opencv-2.4.5/release/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_matrix_operations.cu.o

make[2]: *** [modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_matrix_operations.cu.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_core.dir/all] Error 2
make: *** [all] Error 2

@meznom

This comment has been minimized.

Copy link

commented Jan 28, 2014

On point 6:

I ran into the same issue with a different project and, asking on the Anaconda mailing list, got the impression that "fixing" the paths with install_name_tool is the recommended approach. As I understand it, Anaconda using relative-path install names (which seems to be very unusal on OS X) is the price you pay for it's flexible environments system.

conda build does some magic to get the libraries' paths right and at least for my project it was pretty straightforward to put together a conda recipe. There seems to be a recipe for opencv in the conda-recipes repository which you might be able to adapt (haven't used it, though).

Still I feel like Anaconda should somehow warn about this linking issue. It's very non-obvious to figure out (a seg fault --- where do you go from there), especially if you are not doing linking gymnastics on OS X every day.

@jjhelmus

This comment has been minimized.

Copy link

commented Jan 29, 2014

If anyone is having @scollis is having try adding

-DWITH_CUDA=OFF

To your cmake command. It turns off CUDA support which is causing the problem.

@chohlasa

This comment has been minimized.

Copy link

commented Feb 2, 2014

Thank you for this, I'm sure it was a huge timesaver!
I was also able to get this working on Mavericks with Anaconda 1.9.1 without changing any of your instructions.

@robertDouglass

This comment has been minimized.

Copy link

commented Feb 6, 2014

Thanks! It worked.

@dafukua

This comment has been minimized.

Copy link

commented Mar 17, 2014

Thanks very much! Works quite well under opencv-2.4.8-macosx-10.9.2-anaconda-1.9.0

@rogerg

This comment has been minimized.

Copy link

commented Apr 2, 2014

Thanks for this code. I tried to install opencv with a corrupt 2.4.3 version this afternoon with which I got stuck at step 4 all the time... after trying with 2.4.8, step 4 worked fine
I finally got to point 5 - I had my anaconda folder at the source - copy it to my user folder. now I do not have a cv2.so in there. I have another cv2.so from the first try with 2.4.3 - copy that and seems to work so far - now I can go to step 6.

@rogerg

This comment has been minimized.

Copy link

commented Apr 2, 2014

in the end when trying to import cv2, ipython tells me No module named cv2

@rogerg

This comment has been minimized.

Copy link

commented Apr 2, 2014

ok, I started iPython from the site-packages folder and now I get

In [1]: import cv2
Segmentation fault: 11

however, I have everything in the cv2.so except the /System/Library/Frameworks/OpenCL.framework/Versions/A/OpenCL (compatibility version 1.0.0, current version 1.0.0)

I copied this in the site-packages folder
and
I have older versions of
/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)

I'm using ipython 1.1.0

@myazdani

This comment has been minimized.

Copy link

commented Apr 3, 2014

This is the place where I get stuck...

[ 35%] Building CXX object modules/core/CMakeFiles/opencv_perf_core.dir/perf/perf_main.cpp.o
/Applications/OpenCV/opencv-2.4.5/modules/core/perf/perf_main.cpp:3:1: error:
multiple unsequenced modifications to 'argc' [-Werror,-Wunsequenced]
CV_PERF_TEST_MAIN(core)
^~~~~~~~~~~~~~~~~~~~~~~
/Applications/OpenCV/opencv-2.4.5/modules/ts/include/opencv2/ts/ts_perf.hpp:481:12: note:
expanded from macro 'CV_PERF_TEST_MAIN'
while (++argc >= (--argc,-1)) {VA_ARGS; break;} /this ugly cons...
^ ~~
1 error generated.
make[2]: *
* [modules/core/CMakeFiles/opencv_perf_core.dir/perf/perf_main.cpp.o] Error 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] Error 2
make: *** [all] Error 2

@rogerg

This comment has been minimized.

Copy link

commented Apr 3, 2014

Hello Myazdani, I had the same problem as you at a point with this error
I found out that it is due to a problem with that OpenCV version 2.4.5
download another version of OpenCV and this step will work

for my part, I finally decided to uninstall all OpenCV and Anaconda instances and to install it with brew
and finally after 7hrs yesterday and 2 hrs today, I managed to install OpenCV with brew

@rogerg

This comment has been minimized.

Copy link

commented Apr 3, 2014

but cannot open it with brew, either :-( - back to the start

@myazdani

This comment has been minimized.

Copy link

commented Apr 3, 2014

@rogerg, indeed upgrading to the latest version of OpenCV ( found here: ) fixed everything for me. Sorry about your troubles though.

I suggest to the author of the tutorial to point readers to this link for getting OpenCV:
http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/

@ytkim4558

This comment has been minimized.

Copy link

commented Apr 27, 2014

I installed opencv with this(with anaconda). it was very useful to me :D thank you very much

@greentreeys

This comment has been minimized.

Copy link

commented May 31, 2014

Thanks a lot!!! This really works!!!

@jbencook

This comment has been minimized.

Copy link

commented Sep 4, 2014

So good.

@somada141

This comment has been minimized.

Copy link

commented Oct 16, 2014

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
You can’t perform that action at this time.