Installing OpenCV 2.4.9 on Mac OSX with Python Support
**OpenCV is now at 3.0.0! :), I will write up a guide on that when I get around to installing it
*Here is the updated version of my previous post since Brew finally added the most recent version of OpenCV!
1. First of all I’ll be using the package manager Homebrew to simplify things, you can get it here: http://brew.sh/
The reason I’ve decided to use Homebrew was because it installs packages into their own directory which makes everything nice and easy to find. I tried macports but didn’t like it.
2. Once you have brew installed you can go ahead and and add homebrew/science which is where OpenCV is located using:
brew tap homebrew/science
3. If you want to see other possible options such as when compiling (such as using tbb) you can use info
brew info opencv
Go ahead and install OpenCV now
brew install opencv
4. You’re done! You can find OpenCV at
cd /usr/local/Cellar/opencv/2.4.9/
Setting up Python
1. Navigate to your python path, if you don’t know where it is when you installed Python you can find it in your .bash_profile or using
cat ~/.bash_profile | grep PYTHONPATH
in my case we’ll be using
cd /Library/Python/2.7/site-packages/
2. Once there we need to link our compiled OpenCV files, create a symlink using
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so
3. You’re done! You can make sure it works by running python and typing
import cv
70 responses to “Installing OpenCV 2.4.9 on Mac OSX with Python Support”
Trackbacks / Pingbacks
- May 24, 2014 -
- February 14, 2015 -
- April 12, 2015 -
- June 9, 2015 -
- August 10, 2015 -
- September 24, 2015 -
- February 23, 2016 -
- July 11, 2016 -
I am stuck on the last step. When I try to make the symlink using that command, I get a message which says: ln: cv.py: Permission denied
I followed all the steps exactly so I’m not really sure whats going on. Thank you!
use sudo
maybe you could elaborate? use sudo for what?
You mean sudo when start python from terminal right?
use “sudo ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so” . it will work.
make sure you have the same version of cv as the tutorial.
what’s annoying is that if the file you are creating a symbolic link to doesn’t exist, it displays ‘permission denied’ which seems odd.
I think in this command you face trouble
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
because 2.4.9 folder might changed with other name .. just change with your file name..
Hi,
I’m having trouble with the final part of importing cv. Initially I thought it was because I kept getting “import: command not found” and I tried everything. Now I found your blog, I tried it all out and when I try to do part 2 of the Python section, I get ” Permission denied”.
Is there anyway you could help me? It is installed successfully it’s just part 2 and 3!
Thanks,
Paula
Ignore this I figured it out! Sorry and thank you – this helped a lot!
What solution did you find? I am having the same error.
Can you explain how you did it? I’m having the same problem… Thanks!!
solution is to type ‘sudo ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py’, which enables you to issue a command as the system administrator.
how did you fix the import :command not found problem?thanks a lot
What solution did you find? I am having import: command not found
Pretty helpful! THX!!
dylib file missing error.
This is what my bash looks like –
hogwarts:MapNetwork Akrita$ python sampleOCV.py
Traceback (most recent call last):
File “sampleOCV.py”, line 1, in
import cv
File “/Volumes/PrivetDrive/Copy/UCincy/cchmc/MapNetwork/cv.py”, line 1, in
from cv2.cv import *
ImportError: dlopen(/Volumes/PrivetDrive/Copy/UCincy/cchmc/MapNetwork/cv2.so, 2): Library not loaded: /opt/local/lib/libjasper.1.dylib
Referenced from: /usr/local/Cellar/opencv/2.4.9/lib/libopencv_highgui.2.4.dylib
Reason: image not found
Python file – sampleOCV.py
location – /Volumes/PrivetDrive/Copy/UCincy/cchmc/MapNetwork/
python and opencv were installed using macports but now switched to homebrew using this tutorial.
mac OS X version 10.9.4
python 2.7
Can you help me?
Thanks
I had this issue as well. I ran brew uninstall opencv and then did the following:
# brew doctor
# Note: Resolve ALL issues present in brew doctor.
#
# Brew doctor may ask you to change the perms on /usr/local/lib/pkgconfig:
# Do sudo chown -R [YOUR COMPUTER USERNAME]:admin /usr/local
# (http://stackoverflow.com/questions/16462949/homebrew-brew-doctor-throwing-up-a-lot-of-errors-how-can-i-fix-them)
#
# You may have to link python (brew link python) to make sure the python shell evokes the brew version of python
# If you haven’t even installed brew’s version of python, run brew install python
#
# As the doctor suggests, you may have to run echo export PATH=’/usr/local/bin:$PATH’ >> ~/.bash_profile
# Note, whenever you change ~/.bash_profile, you should start working in a new terminal window.
#
# You may have to remove unnecessary .dylib, .pc, and .a files
# (http://stackoverflow.com/questions/10580813/how-do-i-fix-brew-doctor-errors)
#
# brew tap homebrew/science
# brew install opencv
# brew link opencv
# brew doctor
# Note: Resolve all issues present in brew doctor (AGAIN).
#
# You may have to link a bunch of dependencies of opencv including libpng (brew link libpng)
#
# Open the python shell and try import cv2
# (from http://www.jeffreythompson.org/blog/2013/08/22/update-installing-opencv-on-mac-mountain-lion/)
Hope that helps.
Are you sure this actually works? Every time I install opencv using homebrew, turns out there is no python packages at nowhere. I mean, this directory has never created /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/
It is exactly how I installed it. I installed python using homebrew not the installer off the website so maybe you could give that a try.
I actually had the same problem. To fix it, I removed Python (see http://stackoverflow.com/questions/3819449/how-to-uninstall-python-2-7-on-a-mac-os-x-10-6-4). Then, I installed Python and opencv as follows :
brew install python
brew install opencv
and it worked fine.
Had been trying hard to install and setup opencv all day, all my efforts went in vain. saw this and tried, cannot believe that just 2 lines made it work, now i can sleep peacefully. thanks 🙂
Thanks! It really works well(:
Awesome, I’m glad!
Hi,
I followed the instructions here and when I tried to import cv, I got a segfault: 11. Any idea what might have gone wrong?
Thanks!
If I can recall segfault: 11 was due to compiling with a version of python different from the one that is running OpenCV. Remove all versions of python installed on your system and then remove OpenCV, install python through brew followed by OpenCV.
try
brew install python
brew link python
but first brew rm python
to remove your computer’s own python
Thanks man! This save me a lot of time 😉
OMG a tutorial that is not out of date!!! You rock.
This is a good tutorial, but I’m rather disappointed to find that the Python bindings a) aren’t separate from the OpenCV software itself and b) can’t be simply installed from PyPI with pip or similar. This would make it possible to support different versions in different Python virtual environments, as well as setting the stage for a Python 3 version.
Further notes on my attempt to use the instructions given:
1. The article suggests using the command “cat ~/.bash_profile | grep PYTHONPATH” to locate the installed Python. Ignoring the fact that virtual environments can mean there are many Pythons installed, the PYTHONPATH environment variable does not in any way locate the installed Python. If you want to know where your current Python is, “which python” will do that.
2. There is no /usr/local/Cellar/opencv/2.4.9/lib/python2.7/ directory on my system after installation of OpenCV using brew. I do see usr/local/Cellar/opencv/2.4.9/lib/ but it has no “python*” subdirectory.
Please let me know if you’d like me to raise these issues against the project – I an guessing things may have moved on a little since you wrote the article.
What @holdenweb said. As of `brew install opencv` there is Cellar/opencv/2.4.9/lib/ and but no Cellar/opencv/2.4.9/lib/python.
What does one pick up the Python bindings these days?
Reblogged this on Repository of my past writings and commented:
Opencv tool for python
Don’t think the reblogging contains the “code”. Also I’m having an interesting issue where the console is unresponsive when using interactive samples from http://sourceforge.net/projects/opencvlibrary/files/
Thanks for your reply. At the moment I reblogged your post at my blog, I had trouble while installing opencv on my laptop. . I had to keep your post for my future that may needed for confirming my whole past installation process. Code was secondary subject for me!
Thanks a lot!
I did all of this, but I keep getting the error:
Raise ImportError(“Cannot load OpenCV library which is required by SimpleCV”)
ImportError: Cannot load OpenCV library which is required by SimpleCV
Hi there, thanks for the tutorial!
I followed everything through, but when I follow up and run python on terminal and try to import cv, it tells me that the module doesn’t exist.
Any tips?
You haven’t linked your files correctly. Go back and try step 2 of the Python section again and hopefully that solves it
When I run “brew install opencv”, it gets stuck on “make bootstrap” for a REALLY long time. My computer gets really hot/loud too. Seems to be crashing. Any idea what might be causing this?
Your computer is getting hot because you’re compiling opencv, it takes quite a long time so just leave your laptop and go watch a movie and come back in a few hours. You could also use your hot laptop as an electric blanket, or as a warming for plate when you make too much pizza but know you’ll get hungry in an hour from all this installing, put your coffee on the palm rest to keep it warm while you play tower defense games on miniclip, read a book while you wait but make sure it doesn’t touch your laptop and catch fire, call Tim Cook and tell him what you think about the iWatch, maybe go camping and bring your laptop as a portable always-lit fire starter.
Hope this helps if you need more ideas please let me know.
Ok got the Pizza hint , great post terminated openCV as i thought it’s crashed .
The step brew install opencv failed for me. Any thoughts?
==> Installing opencv dependency: homebrew/python/numpy
Error: undefined method `homepage’ for Numpy:Class
Please report this bug:
https://github.com/Homebrew/homebrew/wiki/troubleshooting
/usr/local/Library/Taps/homebrew/homebrew-python/numpy.rb:2:in `’
/usr/local/Library/Taps/homebrew/homebrew-python/numpy.rb:1:in `’
Maybe you don’t have numpy installed? Try install it using homebrew and make sure it’s linked to the same version of Python you’re using to compile OpenCV.
Hi. I followed all your steps but can only read certain video files. For some if I run “import cv2; cap = cv2.VideoCapture(‘video.avi”);cap.read()” it just shows False,None, while trying this on a different computer it shows a numpy array. I have tried to fix it for hours but don’t succeed. Do you have any ideas why it only reads certain video files?
Ya it’s likely because you don’t have the required codecs installed. Download and install ffmpeg and it should work, if not you may need to recompile OpenCV after you’ve installed ffmpeg for it to work. Look closely at the output of make when compiling OpenCV and it should tell you if it can find ffmpeg on your system.
What could be simpler than that?
I was running into the following error following the above steps
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: /usr/local/lib/libImath-2_2.12.dylib
had to brew unlink openexr than brew install openexr and than i was good to go.
hopefully this is helpful for others…
If you are using macports, the complete procedure is:
% sudo port install opencv +python27
Unfortunately, python3 is not supported yet by macports, even though opencv is supposed to support it.
I prefer macports to home-brew because it keeps everything in the macports /opt directory, instead of stomping on /usr/local, so opinions differ.
Thank you muchly!
Reblogged this on Metaprime Studio.
Reblogged this on 正我.
Thank you very much for the help!
Thank a lot for your help, really appreciated! It worked fine for me, following both your instructions and the tips in the comment! Keep on the good work! Cheers from Italy!
Awesome, Glad to hear!
thank you so much there…:) helped me a lot…..:D you are a life saver 😀
Excellent! Worked like a charm!
Hi there, I follow each step for my mac but when I run import cv the python2.7 output is this error messagge
>>> import cv
Traceback (most recent call last):
File “”, line 1, in
import cv
ImportError: dynamic module does not define init function (initcv)
could please somebody help me.
Thanks for the tutorial!!
You, my friend, rock! I’ve been trying to install openCV on my Mac for a week now. I tried following the instructions on the openCV site and they didn’t work. I even tried compiling openCV following their instructions, no love. I searched google and tried a couple of different recipes from who knows who – didn’t work. Your instructions, however, worked the first time I followed them correctly. 😉
Thank you so much
Thank you! This is exactly the reason I made this guide and I’m glad you found it useful! I do have to update it for OpenCV 3.1 in my lack of spare time but the gist of it should still be the same.
Thank you for this! Just make sure when run the following lines:
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/2.4.9/lib/python2.7/site-packages/cv2.so cv2.so
to change the number 2.4.9 to what your current version is (mine was 2.4.13.2)
Other than that, this was a great tutorial and I finally got cv and cv2 to work in python2.7.