Tuesday, September 22, 2009

Installing OpenCV on Mac OS X, with Python Bindings

It's my first time developing on OS X, so I thought I'd document the process to get started using vision algorithms on a Mac.

Platform: OS X Version 10.5.8

1. Install XCode from the Mac OS X install CD.

2. Download and install MacPorts, a command-line tool to install software and deal with dependencies for you. (For some reason I received a "Could not find specified message for index 16" error when trying to install the version for Snow Leopard, so I installed the version for Leopard instead.)

Don't forget to run sudo port selfupdate once it's installed.

3. Install OpenCV by running the command sudo port install open cv . This installs OpenCV 1.0, which is fine for me, because that's what the Python bindings (next) are tested on.

4. Download ctypes-opencv source and demo files. Once that's done, navigate to the ctypes-opencv directory and install using sudo python setup.py install

5. Open your ~/.profile (created when installing MacPorts) and add the following line:
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib

Save and close it, then restart your terminal window for the .profile to be loaded.

6. Try one of the ctypes-opencv demos to check if it works: python houghlines.py

Running the camshift demo, using the integrated MacBook iSight webcam:



That's me thinking "Oh, OpenCV installation. Can start my research now?"

21 comments:

Oscar said...

Thank you!!

It was straight fast!!

Anonymous said...

Hi Angelica,

I'm currently go through the same progress to install opencv. How long did it take to install using macports?

It's been well over an hour since I ran "port install opencv", and its still fetching dependencies :-(

Thanks,
Josh

Anonymous said...

Just a heads up to people going through the install process. It took me around 2 hours to obtain all the dependencies.

Josh

Unknown said...

Thanks for your comments :) Sorry I didn't see your message until now, Josh. Glad it worked out in the end.

Dave said...

Hi!

I'm working through this set up now and I've got everything install (through your step 5). I'm trying to run some of the Python demos, but I'm getting errors regarding the PATH variable:

ImportError("OpenCV's shared library '%s' is not found. Make sure you have its path included in your PATH variable." % name)
ImportError: OpenCV's shared library 'cxcore' is not found. Make sure you have its path included in your PATH variable.

Is it possible I need to change the PATH variable in .profile to something different? (I'm on OSX 10.5.8, XCode 3.1.4, things seemed normal with my opencv install).

Thanks!
-Dave

Unknown said...

Here's what's in my .profile. Anything look different? Also, what do you get when you do echo $PATH?

# MacPorts Installer addition on 2009-09-22_at_16:40:31: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.


# MacPorts Installer addition on 2009-09-22_at_16:40:31: adding an appropriate MANPATH variable for use with MacPorts.
export MANPATH=/opt/local/share/man:$MANPATH
# Finished adapting your MANPATH environment variable for use with MacPorts.

# For OpenCV
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
export DYLD_LIBRARY_PATH=/Users/angelica/local/lib

Dave said...

Here's what my .profile looks like:


# MacPorts Installer addition on 2009-11-06_at_16:34:29: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.


# MacPorts Installer addition on 2009-11-06_at_16:34:29: adding an appropriate MANPATH variable for use with MacPorts.
export MANPATH=/opt/local/share/man:$MANPATH
# Finished adapting your MANPATH environment variable for use with MacPorts.

# For OpenCV
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
export DYLD_LIBRARY_PATH=/Users/Dave/local/lib


and when I echo $PATH I get:

/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

Unknown said...

Have you tried restarting your terminal?

Also, what do you get when you do:
ls /opt/local/lib | grep cxcore

I get this:
libcxcore.1.0.0.dylib
libcxcore.1.dylib
libcxcore.dylib
libcxcore.la

Dave said...

Ok, cool. It looks like restarting the terminal got the PATH variable to take (I don't do much command line work on my Mac, this is cool to learn). I'm now getting another ImportError, though:

File "camshiftdemo.py", line 6, in < module >
from ctypes_opencv import *
ImportError: No module named ctypes_opencv

Unknown said...

Yep, the .profile is only run upon startup.

Hm, I just checked my instructions again and maybe I wasn't clear.

Did you run
sudo python setup.py install
from within the ctypes-opencv src folder?

Were there any errors?

Unknown said...

Hi Angelica,

(nice post!)
stil..
i am a bit confused with regard to the python version to be used. i recently switched to the EDP version of python and changed my pythonpath etc accordingly. Do i have to use the original python distribution shipped with the mac if i want to use opencv from within the ctypes_opencv module? That is, more specific: do i have to install these modules within the original python version, or are there other options that you know of, so i could keep using my EDP ditribution?

thanks in advance!
best regards,
)a((o

Unknown said...

)a((o,

I'm not familiar with EDP Python, but if it's mostly similar to Python 2.5 (what I'm on) then I don't think there should be any problems. Also, my PYTHONPATH is empty, so don't worry about having deleted/modified something important. Give it a try and see how it goes?

manel camacho said...

ive installed opencv 1.0 nice tutorial, but i'm using now 2.0 version, compiled from 0 it's nice and better, you need the same dependencies :)

Unknown said...

I'd love to try upgrading to 2.0 when I get a chance. Thanks for letting me know!

Anonymous said...

You can download opencv 2.0 from the page and tell configure to build it in 32 bit mode if you want to use it on snow leopard:

CXXFLAGS="-arch i386" CFLAGS="-arch i386" LDFLAGS="-arch i386" ./configure

Chuck

limey said...

Hi, when I ls and grep cxcore on the specified places you describe I get nothing.
And my DYLD_FALLBACK is in my profile.

sudo ports install opencv gives me nothing new as far as dependencies go.

Am struggling with this and I can't understand why also I'm on a mac. :)

I have done the sudo python setup.py install aswell!

Thank you! :D

Unknown said...

Limey,
These instructions are old, and I think the macport install might be installing the newest version of OpenCV now. (2.2.0)

Maybe that's the problem, because the ctypes-opencv library only works with OpenCV 1.x...

If you have OpenCV 2.x installed already, I think they should have nice Python bindings already.

The Hyphenator said...

If you're installing OpenCV with MacPorts, make sure to include "+python27" or "+python26" as a command-line argument, or you cannot use it with Python. More info: http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port#A1.BuildviaMacPorts

Anonymous said...

ImportError: OpenCV's shared library 'cxcore' is not found. Make sure you have its path included in your PATH variable.

$PATH: /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/texbin:/usr/X11/bin:/Users/katharinairrgang/.rvm/bin


I dont know how to fix?

Anonymous said...

plz help :'(

hi angelica,

have you or anyone out here tried to opencv 2.4.1 with python2.7 on a mac os x?

i have no idea how i had managed to make it work two days ago by using macports with xcode 4.2.1 already installed (that was 2 weeks of struggling).

but then i messed it up today because i was trying to install gamepy and my mac finally realized that i did not have any system python installed. when i installed one by downloading a source it messed my entire setup.

i don't even know how many places python is installed in my comp anymore.

now i can't run anything because of
"ImportError: No module named cv"

i have tried everything on this blog and so many others.

any thoughts? any help? please?

i haven't slept in a day because of this, i'm hungry... i've started to hate programming and now i think my mind is slipping... hmmm i think i should just stop working and spend the rest of my life watching all the episodes of gintama again and again.

*things taken individually seem like a perfect package... but then... when brought together... the interaction amongst them merely produces perfect errors.

Unknown said...

Thank you a lot!

It works everything perfectly (specially the information about the system variable PATH).

Best regards from Colombia.