Вы находитесь на странице: 1из 7

Mac OS X OpenCV Port

This page is devoted to information specific to the OS X build of OpenCV. Things are
changing quickly due to recent work by Willow Garage, and the transition to the CMake
build system.

Also see the general InstallGuide.

1. Easy build via MacPorts

For Snow Leopard users, you can build OpenCV 2.1.0 and above via MacPorts as a 64-
bit library with support of QTKit video I/O and Cocoa GUI.

Assuming MacPorts installed, first use:

sudo port selfupdate

to update the MacPorts base and the port tree. Then, simply:

sudo port install opencv

to build OpenCV 2.1.0 in 64-bit.

If you encounter errors, try installing its dependents with +universal whenever
possible.

sudo port -v install opencv +python26

to use with Python 2.6 bindings

By default OpenCV is installed to /opt/local/ according to MacPorts conventions.


The opencv_xxxx utilities are located in /opt/local/bin/, the dynamic libraries in
/opt/local/lib/ and the header files in /opt/local/include/opencv/.

To use the libraries, include the required files with standard includes. For example:

#include <cv.h> #include <highgui.h>

If you use Xcode, the instructions are similar to the ones in method 2. It only differs that
/usr/local becomes /opt/local.
2. Build from source using the CMake build system
(new)

The GNU autotools methods (configure, make, make install) are being obsoleted. OpenCV
has now switched to the CMake build system.

Getting the source code

Make sure you have subversion installed (built-in since Snow Leopard). If MacPorts is
your primary package manager, use:

sudo port install subversion

if not using MacPorts or it gives an error, try:

sudo fink install svn-ssl

Make sure you have cmake installed. This can also be installed with MacPorts:

sudo port install cmake

Check out the source code into a clean directory and cd into the source directory after the
download is complete:

svn co https://code.ros.org/svn/opencv/trunk/opencv cd opencv

Note: There's a "atomic bomb commit" at r3060 which largely changed the
OpenCV directory structure. To be safe, you can specify an earlier checkout revision
to work with the instructions in this section:

svn co https://code.ros.org/svn/opencv/trunk/opencv -r 3059 cd opencv

Use CMake to build

To use CMake to generate Unix Makefiles, type:

sudo cmake -G "Unix Makefiles" .

You can specify many configuration options in the above command. For examples:

-D BUILD_TESTS=OFF -> if you don't want to build tests.


-D BUILD_NEW_PYTHON_SUPPORT=OFF -> if you don't want the Python
interface support.
-D WITH_CARBON=ON -> (mainly for Leopard and below) if you want to use
Carbon for UI instead of Cocoa (since r2909).
-D WITH_QUICKTIME=ON -> (mainly for Leopard and below) if you want to
use QuickTime for Video I/O insted of QTKit (since r2924). If you're using Snow
Leopard and do need Carbon and QuickTime, you must also specify -D
CMAKE_OSX_ARCHITECTURES=i386 and -D CMAKE_C/CXX_FLAGS=-
m32.
and many others (???)

For the case you need the CMake GUI for other options, replace cmake with ccmake. If the
arrow keys fail, you can use the following commands instead:

ctrl + N : next option ctrl + P : previous options ctrl + D :


down one page ctrl + U : up one page

To compile with Intel Threading Building blocks, use ccmake (the Cmake GUI) to specify
the lib and include paths to your installation of TBB.

After setting the build options, hit 'c' to configure. Hit 'g' to generate the config file and exit
ccmake.

Finally, follow the usual "make, make install" method:

sudo make -j8 sudo make install

Using the OpenCV libraries in an Xcode OS X project (opencv 2.1


since SVN r3060)

These instructions were written for Xcode 3.1.x and C++

Create a new Xcode project using the Command Line Utility/Standard Tool template
Select Project -> Edit Project Settings
Select the Build tab
Set Configuration to All Configurations
In the Architectures section, double-click Valid Architectures and remove all the PPC
architectures (If you built the libraries as 64-bit only, remove i386 as well)
In the Search Paths section set Header Search Paths to /usr/local/include
(/opt/local/include if you used MacPorts) and check Recursive
Close the Project Info window
Select Project -> New Group and create a group called OpenCV Frameworks
With the new group selected, select Project -> Add to Project…
Press the "/" key to get the Go to the folder prompt
Enter /usr/local/lib (/opt/local/lib if you used MacPorts)
Select libopencv_core.dylib, libopencv_highgui.dylib, and
libopencv_imgproc.dylib (perhaps more, not sure yet).
Click Add
Uncheck Copy Items… and click Add

Now you should be able to include the OpenCV libraries, compile, and run your project.
The only #include directive you should need is #include <opencv.hpp>. It, in turn, includes
the rest of the opencv header files.

Using the OpenCV libraries in an Xcode OS X project (opencv 2.0, and


2.1 via MacPorts)

These instructions were written for Xcode 3.1.x

Create a new XCode project using the Command Line Utility/Standard Tool template
Select Project -> Edit Project Settings
Select the Build tab
Set Configuration to All Configurations
In the Architectures section, double-click Valid Architectures and remove all the PPC
architectures
In the Search Paths section set Header Search Paths to /usr/local/include/opencv
Close the Project Info window
Select Project -> New Group and create a group called OpenCV Frameworks
With the new group selected, select Project -> Add to Project…
Press the "/" key to get the Go to the folder prompt
Enter /usr/local/lib
Select libcxcore.dylib, libcvaux.dylib, libcv.dylib, libhighgui.dylib, and
libml.dylib.
Click Add
Uncheck Copy Items… and click Add

Now you should be able to include the OpenCV libraries, compile, and run your project

3. Building as an Xcode Framework using the


make_frameworks.sh script (obsoleted)

Note: the CMake build system should ultimately make this obsolete, one may remove this
section when the CMake building is mature on the Mac. By the way, the
make_framework.sh script has been removed since revision 2528. The last revision
where make_framework.sh successfully builds, as I tested, is r2492.

OpenCV can now be built as a Mac OS X Framework useable with XCode. Just invoke the
'./make_frameworks.sh' script and you will obtain a Universal Framework named
"OpenCV.framework" meant as a Private Framework.

Additionally, a pre-built version (universal binary Private Framework, again together with a
demo application) is available. See the PrivateFramework wiki page or directly download it
from http://www.ient.rwth-aachen.de/~asbach/OpenCV-Private-Framework-1.1.dmg .

A more recent version of the framework can be found here http://www.ient.rwth-


aachen.de/cms/software/opencv/ or downloaded directly from http://www.ient.rwth-
aachen.de/~asbach/OpenCV-Private-Framework-1.2.dmg

OpenCV 2.0 Universal Framework (tested on ppc and i386 Leopard and Snow Leopard):
Here is an universal Framework for Mac ppc/i386 built from the OpenCV 2.0 released in
September09. By the way, I also fixed some bugs and add some improvements:

-> a bug on Mac (carbon) that prevented cvCloseWindow/cvCloseAllWindow


from working. -> a bug when 2 cameras capture images simultaneously
(black bands) (see picture bellow) -> The trackbar now displays its
current value (see picture bellow) and is resized according to its
name.

Those fix/improvements are added to this framework.

http://vislab.cs.vt.edu/~vislab/wiki/images/4/44/OpenCV2.0.dmg

4. Building the old UNIX way

The following shows you how to compile OpenCV CVS for Mac OS X, using QuickTime
video I/O and Carbon windowing. Image I/O is currently done via external libraries.

External dependencies

The code relied on external libraries for image loading and saving as well as video reading /
writing. You can still enable reading JPEG, PNG and TIFF images 'the traditional way' with
external libraries installed via Fink or DarwinPorts. However, this is no longer required
nor recommended.

Unfortunately, we still have no working (i.e. native) video writer for Mac OS X. If you need
video writing, you should currently configure for ffmpeg or xine and disable QuickTime.
The downside of this is that you will lose QuickTime video input at the same time. And still
ffmpeg video writing support is as buggy on Mac OS X as it is on Linux (for OpenCV).
Volunteers?

Configuring

To configure the code for your environment, go into the source directory. It’s a good idea to
first make a special build directory:

mkdir build; cd build

Then run the provided configure script, giving it the location of the external libraries. For
Fink, use:

../configure CPPFLAGS="-I/sw/include" LDFLAGS="-L/sw/lib"

For ! MacPorts users, the line reads:

../configure CPPFLAGS="-I/opt/local/include" LDFLAGS="-L/opt/local/lib"

You can add additional tuning parameters as well, e.g. CXXFLAGS="-mcpu=G5 -


mtune=G5". See also the output of ../configure --help for additional
arguments.

Compiling and Installing

Finally, compile and install the code:

make sudo make install

The install goes into /usr/local by default; of course you can change that with the
usual --prefix argument to configure. You might also want to run the tests with
make check.

Other Resources

Links:

Creating a universal static OpenCV library with iPhone support


http://www.ient.rwth-aachen.de/~asbach/OpenCV-Private-Framework-1.1.dmg
KirstenJones - March 2, 2009 - Has notes on building OpenCV using CMake on her
blog at http://www.princesspolymath.com
http://www.opencv.org.cn/forum/viewtopic.php?f=1&t=7524&start=0%20Tutorial
of method 3 in Chinese, although it suggests a pre-built Framework instead of
building by yourself.
Compile OpenCV projects using shell scripts on MacOSX
Mac How

Attachments:

[get | view] (2006-05-30 14:17:41, 6.2 KB) [[attachment:export.cpp]]

Collected Information

Compiling Universal Binaries

The --enable-universal-build switch has been removed again since it doesn't


allow building on intel machines ... You can still try out what it did by configuring with:

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


ppc" --disable-dependency-tracking

As an alternative, we now have a small build script that configures and builds twice (once
for ppc, once for intel) and then merges the binaries with lipo. We can and will extend this to
more architectures whenever Mac OS X 10.5 Leopard is released, since it finally comes
with 64bit compilers for intel. In addition to those then natural 4 platforms (intel/ppc *
32/64), it might be nice to provide specially optimized variants for G5, G4, G3 (-mcpu=...)
to maximize speed on older hardware. This however requires additional testing (and
performance testing).

See also: Compiling for Multiple Architectures in


http://developer.apple.com/documentation/Porting/Conceptual/PortingUnix/

A (very old) list of supported quicktime codecs can be found at QuickTimeCodecs

OpenCV OS

OpenCVWiki: Mac_OS_X_OpenCV_Port (last edited 2010-09-20 00:11:39 by Norman Khine)

Вам также может понравиться