Tuesday, October 30, 2012

Installing OpenCV on MacOS X via MacPorts

Installing OpenCV on MacOS X is simple.

1) Installing MacPorts
i) via pkg

For me, I tried downloading the MacPorts pkg files (the claimed easiest way) from here:
http://www.macports.org/install.php

After installation you should be able to use:

sudo port -v selfupdate

to update the MacPorts. However for unknown reasons it didnt work.

ii) via source

Then I tried downloading the source, i.e. MacPorts-2.1.2.tar.bz2, extract it.
then cd to MacPorts-2.1.2 and type:

./configure && make && sudo make install

After all these things you have to edit the PATH variable to allow the terminal access the command "port". Go to /etc/, copy the "paths" file out, append the following to the end of file:

/opt/local/bin

and put it back. (This saves you from using the troublesome vim editor)
thats where the MacPorts installed. Then you should be able to access the following update command:

sudo port -v selfupdate

2) Installing OpenCV

- Problems: After setting up the MacPorts, I tried installing openCV by this:

sudo port install opencv +python27

But after installation I find

import cv

in python did not work.

I figured out that it is the problem of having many different versions of python install on Mac (Both my installation and MacOS original version)
The easiest way to fix this is to remove all the versions of Python, and install MacPorts-version of Python. The OpenCV from MacPorts only works for Python from MacPorts too.
if you have an user-installed version of python, remove it by:


rm -rf /Library/Frameworks/Python.Framework


because the OpenCV will not be configured to your original version of python nor the come-with-the-mac version of python.
Then try to type:

python

If you are still able to open python, it is possibly due to the old version that comes with the macOS. Go to /usr/bin/ , rename "python" to other names
Now you shouldnt be able to call python in terminal.
Then install python27 on MacPorts, type:

sudo port install python27

After that, type:

sudo port install opencv +python27

This will install the python bindings of openCV

Then it automatically calculates and install opencv for you. It takes about 2 hours to compile and install all necessary dependencies.

When it is done, try enter python in terminal, and type:

import cv

If everything goes right, it will do nothing.

You can now go to this github to get hello world applications.

https://github.com/jessicaaustin/robotics-projects/tree/master/opencv-tutorial