UPDATE: Mavericks users were likely frustrated for the last few weeks trying to install OpenCV. It appears the problem has been fixed, and these instructions should work without issue.
My previous post on installing OpenCV for Mac users is one of the most popular on this site (which is simultaneously surprising and fantastic). However, I recently switched from using MacPorts to Homebrew – users that need to migrate existing installs can check out this guide.
Still complicated but thoroughly do-able, here is how to install OpenCV on a Mac running Mountain Lion:
- Open Applications > Utilities > Terminal…
- Install Homebrew by typing the following into Terminal
1ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) - Run the command brew doctor to check for any errors
- While normal installations using Homebrew are very straightforward, for some reason OpenCV requires an extra step – you need to install an add-on called
science:
1brew tap homebrew/science - Then you can run the usual Homebrew command to install OpenCV:
1brew install opencv
If this fails, it may be that you have ffmpeg installed – if so, try this instead:
1brew install opencv --env=std - This may take several minutes, so be patient!
- Homebrew may tell you that you have to update PYTHONPATH – if so, open your favorite text editor, click File > Open… and hit command+shift+. to see hidden files
- Navigate up to your home folder and open the .bash_profile file – if it doesn’t exist, create it yourself
- Add the line suggested by Homebrew, probably something like:
1export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH - Save the file and close it – you may also have to exit Terminal and open it again
- Test by opening Python:
1python
Then try to import OpenCV:
1import cv - If you don’t get any errors, you’re all set! Now go enjoy a real beer!
Hope this helps! Problems, suggestions – leave them in the comments.
On Mac Mountain Lion, I had to do two things.
Tuomas Rasila says correctly to:
brew install eigen
This eliminated the error:
make[2]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/src/rgbdodometry.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs….
make[1]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/all] Error 2
make: *** [all] Error 2
Then Nis Sarup says:
$ brew unlink opencv
$ brew uninstall opencv
$ brew install opencv
This eliminated the error:
Segmentation fault: 11
when running the program with an ‘import cv2’
Thank you very much for your post!
When I try to import cv it gives “Segmentation fault: 11” error. I couldn’t find any solution for that :/
Did you update the
PYTHONPATH
? That could be it, or that you set a different version of Python when building OpenCV than is installed on your computer. See this post for some details and fixes.I tried all of them however i couldn’t solve it. after recovery from time machine I followed same steps and it worked. I still don’t know the problem :/
I followed all the instructions but am still getting the no module named cv error. Please help!
thanks for ur post, it works on me.
one more question if u dont mind
i would like to modify the opence_traincascade source n rebuild it
how to do so ? any instructions on how to rebuild the opencv with my modify source ?
thanks
I don’t really work in C/C++, so I don’t know. I’m sure there’s tutorials on building OpenCV from source, but I’m guessing that build might conflict with the one you just installed.
I am embarassed at asking this trivial question.
How do I fix this problem?
$ brew install opencv
==> Installing dependencies for opencv: cmake, eigen, jpeg, jasper, libpng, libtiff, ilmbase, openexr, pkg-config
==> Installing opencv dependency: cmake
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/cmake-3.0.1.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring cmake-3.0.1.mavericks.bottle.tar.gz
Error: An unexpected error occurred during the
brew link
stepThe formula built, but is not symlinked into /usr/local
No such file or directory – /usr/local/Cellar/cmake/2.8.12.2/share/cmake
Error: No such file or directory – /usr/local/Cellar/cmake/2.8.12.2/share/cmake
Not sure! Looks like a problem installing
cmake
. Maybe just trybrew install cmake
alone?When I ran brew install opencv or brew install opencv –env=std, this was the output:
==> Installing opencv from homebrew/homebrew-science
==> Downloading https://github.com/Itseez/opencv/archive/2.4.9.tar.gz
######################################################################## 100.0%
curl: (33) HTTP server doesn’t seem to support byte ranges. Cannot resume.
==> Trying a full download
What is the curl error and how do I fix it?
Thank you!
Not sure, that sounds like either a curl or Homebrew error, neither of which I know a lot about. Did you run
brew doctor
after installing Homebrew?Hi, I am following your steps to install OpenCV and it appears that the homebrew installer has moved. They supply the correct command when installing but thought you may want to change it on the post. This is command that I homebrew asked me to run instead:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Updated, thanks!
Thanks a lot Jeffrey. After about 4 hours of web browsing, unsuccessful installs, I tried your solution… and it works on Mac OS maverick (don’t want of Yosemite…) with Pycharm. Just remember to force your scripts to run with Python 2.7, if I am not wrong.
Thanks again.
Eric
I run into following error, while doing import cv
Traceback (most recent call last):
File “”, line 1, in
File “cv.py”, line 1, in
from cv2.cv import *
ImportError: dlopen(./cv2.so, 2): Library not loaded: /usr/local/lib/libpng16.16.dylib
Referenced from: /usr/local/Cellar/opencv/2.4.10.1/lib/libopencv_highgui.2.4.dylib
Reason: Incompatible library version: libopencv_highgui.2.4.dylib requires version 33.0.0 or later, but libpng16.16.dylib provides version 30.0.0
Hmm, not sure. It looks like the issue is with
libpng
which might have been installed by Homebrew when installing OpenCV. Try running this command in the Terminal:brew update
.Jeff – Tried running brew doctor, still running into the same issue. I’ll search some more. Error is clear though not sure how to fix it.
Hi! I have a macbook. I’m trying to install opencv and use Netbeans IDE, but i can’t, it’s say, for example: “”main.cpp:23:10: fatal error: ‘highgui.h’ file not found””
but I added the files. Hope you can help me, waiting for your answer, thanks!
Macarena
I think this might answer your question: http://stackoverflow.com/questions/2694994/opencv-dll-missing-but-its-not
I am not being able to install any ideas?
$ brew install opencv
==> Installing opencv from homebrew/homebrew-science
==> Installing dependencies for homebrew/science/opencv: gcc, eigen, jpeg
==> Installing homebrew/science/opencv dependency: gcc
==> Downloading http://ftpmirror.gnu.org/gcc/gcc-5.1.0/gcc-5.1.0.tar.bz2
Already downloaded: /Library/Caches/Homebrew/gcc-5.1.0.tar.bz2
==> Patching
patching file gcc/jit/Make-lang.in
==> ../configure –build=x86_64-apple-darwin14.3.0 –prefix=/usr/local/Cellar/gc
==> make bootstrap
configure: error: cannot compute suffix of object files: cannot compile
See
config.log' for more details.
dump’: can’t dump anonymous class #::Gcc (TypeError)make[2]: *** [configure-stage1-target-libgcc] Error 1
make[1]: *** [stage1-bubble] Error 2
make: *** [bootstrap] Error 2
/usr/local/Library/Homebrew/build.rb:181:in
from /usr/local/Library/Homebrew/build.rb:181:in
rescue in '
‘from /usr/local/Library/Homebrew/build.rb:167:in
Error: Suspicious failure
$
Hmm, I really don’t know! Looks like an install/Homebrew/GCC error, not OpenCV. Did you try this version of the command instead:
brew install opencv --env=std
?I have somehow installed two different versions of python on my Mac (OsX Yosemite) i.e. 3.4.2 and 2.7.9. From command line my python 2.7.9 is configured but from IDLE it is configured to 3.4.2. I need to re-configure IDLE to 2.7.9. How do I do it?
Sorry, I don’t use IDLE! Maybe try Stack Overflow?
Thanks for this tutorial.
Can I code in C++ using openCV after I install openCV this way? I have the impression that brew install the Python OpenCV?
I don’t know for sure (I don’t do much C++) but you’re right, this is targeted at using Python with OpenCV. This post suggests that the base OpenCV gets installed regardless, it then is bound to Python, so I think it might work. Please let me know if it does!
Works excelent, thanks!
Pd: OSX El Capitan 10.11.3. Macbook pro.
I just followed your tutorial but i got this same orrer as here (http://stackoverflow.com/questions/19724083/linking-python-with-open-cv-on-mac) I just can’t fix it, I have the .bash_profile but is doesn’t change anything!
Any suggestion?
@Renaud: if you’re getting the same error and it’s still not working, I think it’s a Python issue. Maybe post to a forum for help?
Hi,
I followed you steps above and cv seems to be installed – if I now try ‘brew install cv’ I get ‘homebrew/science/opencv-2.4.13.2 already installed’.
However when I run python or python3 and try ‘import cv’ I get the following error:
Jonass-MacBook-Pro:~ Jonas$ python3
Error in sitecustomize; set PYTHONVERBOSE for traceback:
KeyError: ‘PYTHONPATH’
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul 2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/lib/python2.7/site-packages/cv.py”, line 1, in
from cv2.cv import *
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type
Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
Expected in: flat namespace
in /usr/local/lib/python2.7/site-packages/cv2.so
Any idea how to solve this?
Thanks
@Jonas – not sure, but the
KeyError: 'PYTHONPATH'
error I think is a hint. Did you update yourPYTHONPATH
variable?