OpenCVインストール手順〜Mac編(10.5.8Leopard)〜

MacPortsというMacOSX上で動作するパッケージシステムでOpenCVについて調査する

$ port search opencv
opencv @1.0.0 (graphics, science)
    Intel(R) Open Source Computer Vision Library

OpenCVと依存関係にあるパッケージを「info」コマンドを用いて調査する

$ port info opencv @1.0.0
opencv @1.0.0 (graphics, science)
Variants:             universal

Description:          opencv is a library that is mainly aimed at real time computer vision. Some example areas would be Human-Computer Interaction (HCI),
                      Object Identification, Segmentation and Recognition, Face Recognition, Gesture Recognition, Motion Tracking, Ego Motion, Motion
                      Understanding, Structure From Motion (SFM), and Mobile Robotics.
Homepage:             http://www.intel.com/technology/computing/opencv/

Library Dependencies: gtk2, zlib, jpeg, libpng, tiff
Platforms:            darwin
License:              unknown
Maintainers:          stante@gmail.com

OpenCVと依存関係にあるパッケージは「gtk2, zlib, jpeg, libpng, tiff」であることがわかる
OpenCVをインストールする前にこれらのパッケージをインストールする必要がある
これらのパッケージに関しても同様に依存関係の調査を行う

$ port info gtk2
gtk2 @2.18.2 (x11)
Variants:             darwin_7, darwin_8, no_x11, quartz, universal, [+]x11

Description:          This is GTK+ version 2.x. GTK+, which stands for Gimp ToolKit, is a library for creating GUIs for the X Windows System.
Homepage:             http://www.gtk.org/

Build Dependencies:   pkgconfig
Library Dependencies: cairo, fontconfig, freetype, glib2, jpeg, tiff, libiconv, libpng, jasper, atk, pango, gettext, zlib, xorg-libXi, xorg-libXrandr,
                      xorg-libXcursor, xorg-libXinerama, xorg-libXdamage, xorg-libXcomposite, xorg-libXfixes
Runtime Dependencies: shared-mime-info
Platforms:            darwin
License:              unknown
Maintainers:          nox@macports.org, openmaintainer@macports.org

どうやらpkgconfigも入れる必要があるようだ。
しかしgtk2はX11が入ってたら必要無いようなので、今回はgtk2自体はインストールしないがpkgconfigは必要なのでインストールする。

$ port install pkgconfig
$ port install zlib jpeg libpng tiff

これで準備オッケー
本命のOpenCVを入れるが、今回MacPortからは1.0しか見つからなかったけど、1.1preバージョンを入れたいので
http://sourceforge.net/projects/opencvlibrary/files/
から1.1preのソースコードを落としてくる

そして解凍した後、configureの設定をする為にhelp参照

$ ./configure --help
`configure' configures opencv 1.1.0 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking...' messages
      --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR           user executables [EPREFIX/bin]
  --sbindir=DIR          system admin executables [EPREFIX/sbin]
  --libexecdir=DIR       program executables [EPREFIX/libexec]
  --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR   modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR    modifiable single-machine data [PREFIX/var]
  --libdir=DIR           object code libraries [EPREFIX/lib]
  --includedir=DIR       C header files [PREFIX/include]
  --oldincludedir=DIR    C header files for non-gcc [/usr/include]
  --datarootdir=DIR      read-only arch.-independent data root [PREFIX/share]
  --datadir=DIR          read-only architecture-independent data [DATAROOTDIR]
  --infodir=DIR          info documentation [DATAROOTDIR/info]
  --localedir=DIR        locale-dependent data [DATAROOTDIR/locale]
  --mandir=DIR           man documentation [DATAROOTDIR/man]
  --docdir=DIR           documentation root [DATAROOTDIR/doc/opencv]
  --htmldir=DIR          html documentation [DOCDIR]
  --dvidir=DIR           dvi documentation [DOCDIR]
  --pdfdir=DIR           pdf documentation [DOCDIR]
  --psdir=DIR            ps documentation [DOCDIR]

Program names:
  --program-prefix=PREFIX            prepend PREFIX to installed program names
  --program-suffix=SUFFIX            append SUFFIX to installed program names
  --program-transform-name=PROGRAM   run sed PROGRAM on installed program names

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET   configure for building compilers for TARGET [HOST]

Optional Features:
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-static[=PKGS]  build static libraries [default=no]
  --disable-dependency-tracking  speeds up one-time build
  --enable-dependency-tracking   do not reject slow dependency extractors
  --enable-shared[=PKGS]  build shared libraries [default=yes]
  --enable-fast-install[=PKGS]
                          optimize for fast installation [default=yes]
  --disable-libtool-lock  avoid locking (might break parallel builds)
  --enable-debug          build debug version without optimization [no]
  --enable-apps           build demo applications [yes]
  --enable-sse2           enable SSE2 intrinsics [yes]
  --enable-openmp         enable OpenMP threading [automatic]

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-gnu-ld           assume the C compiler uses GNU ld [default=no]
  --with-pic              try to use only PIC/non-PIC objects [default=use
                          both]
  --with-tags[=TAGS]      include additional configurations [automatic]
  --with-swig             use swig wrapper generator [no]
  --with-python           use python for wrapping [yes]
  --with-octave           use octave for wrapping [no]
  --with-xine             use xine libraries (see LICENSE) [no]
  --with-gstreamer        use gstreamer multimedia framework (see LICENSE)
                          [no]
  --with-ffmpeg           use ffmpeg libraries (see LICENSE) [automatic]
  --with-1394libs         use libraw1394/libdc1394 [automatic]
  --with-v4l              use video4linux [automatic]
  --with-unicap           use unicap (see LICENSE) [no]
  --with-imageio          use ImageIO libraries [automatic]
  --with-quicktime        use QuickTime libraries [automatic]
  --with-carbon           use Carbon windows [automatic]
  --with-gtk              use gtk+ 2.0 windows [automatic]
  --with-gthread          use gthread in addition to gtk+ [automatic]

Some influential environment variables:
  CXX         C++ compiler command
  CXXFLAGS    C++ compiler flags
  LDFLAGS     linker flags, e.g. -L<lib dir> if you have libraries in a
              nonstandard directory <lib dir>
  LIBS        libraries to pass to the linker, e.g. -l<library>
  CPPFLAGS    C/C++/Objective C preprocessor flags, e.g. -I<include dir> if
              you have headers in a nonstandard directory <include dir>
  CC          C compiler command
  CFLAGS      C compiler flags
  CPP         C preprocessor
  CXXCPP      C++ preprocessor
  F77         Fortran 77 compiler command
  FFLAGS      Fortran 77 compiler flags
  PYTHON      Python Executable Path
  SWIG        Simplified Wrapper and Interface Generator, used for Python
              bindings

Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.

Report bugs to <opencvlibrary-devel@lists.sourceforge.net>.

Pythonからも使いたいので追加しておく

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

General configuration ================================================
    Compiler:                 g++
    CXXFLAGS:                 -Wall -fno-rtti -pipe -O3 -g -march=prescott -ffast-math -fomit-frame-pointer 

    Install path:             /usr/local

HighGUI configuration ================================================

    Windowing system --------------
    Use Carbon / Mac OS X:    yes
    Use gtk+ 2.x:             no
    Use gthread:              no

    Image I/O ---------------------
    Use libjpeg:              yes
    Use zlib:                 yes
    Use libpng:               yes
    Use libtiff:              yes
    Use libjasper:            no
    Use libIlmImf:            no

    Video I/O ---------------------
    Use QuickTime / Mac OS X: yes
    Use xine:                 no
    Use ffmpeg:               no
    Use dc1394 & raw1394:     no
    Use v4l:                  no
    Use v4l2:                 no

Wrappers for other languages =========================================
    SWIG                      
    Python                    yes

Additional build settings ============================================
    Build demo apps           yes

エラーが出ていないのを確認して、次はmake

$ make

そんでもって

$ sudo make install

インストールが終わったらpkg-configのパスを通しておく

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

そしたらサンプル動かしてみよう
どうやらbuild_all.shってのでサンプル全部コンパイルしてくれるみたいなので実行

$ sh build_all.sh

無事コンパイルが終わったらサンプル実行

$ ./facedetect

iSightが起動して顔認識アプリで遊べたらOK