概要 Abstract
In this page, explain how to use OpenCV briefly. OpenCV is a collection of C/C++ libraries of the open source, and various features necessary for computer vision are packaged. Using OpenCV, for example, advanced image processing features regarding computer vision, such as recognition of the object, pattern recognition, and recognition of the action are available easily. In addition, it is very convenient library so as to perform matrix calculation.
目次 Table of contents

OpenCV のインストール Installation of OpenCV


Here, Introduce a method to put OpenCV in by apt-get and a method to compile from a source in person.

apt-getでインストール Installation with apt-get


It is for the person who want to install OpenCV briefly. By this method, OpenCV2.0 may be installed. (as of 2011/05/20)

ただ,最新版のほうがバグが無くなっていたり,機能が増えているので,ソースからインストールすることをおすすめします. 研究で使う可能性のある人は,ソースからインストールしたほうがいいと思います.

But, because a bug is disappearing, and features are increasing in the latest version rather than old version, you are recommended to install it from a source. The person, who may use it in a research, should install it from a source.

  $ sudo aptitude install libcv4 libhighgui4 libcvaux4 libcv-dev libhighgui-dev libcvaux-dev python-opencv opencv-doc


The sample included in: /usr/share/doc/opencv-doc/examples/


After copying a sample to a suitable Folder, execute it and confirm whether installation is done.

  $ cp -r /usr/share/doc/opencv-doc/examples コピー先
  $ cd examples/c
  $ sh
  $ cp -r /usr/share/doc/opencv-doc/examples COPY_TO
  $ cd examples/c
  $ sh

自分でソースからインストール Installation in person by a source


Here, Introduce an installation method of OpenCV2.2 from a source to Linux by yourself.


Also in this time, when installing it in other distribution using Ubuntu10.04 for Linux, please read it depending on the situation.

必要なパッケージなどをインストール Install rquired packages


Install cmake・g++ necessary to compile, ffmpeg・libjpeg・libpng necessary to use an animated image and static image, and libdc1394 necessary to use 1394 cameras

  $ sudo apt-get install cheese coriander
  $ sudo apt-get install cmake pkg-config g++ build-essential
  $ sudo apt-get install mencoder mplayer
  $ sudo apt-get install libgtk2.0-0 libgtk2.0-dev
  $ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libjpeg62 libjpeg62-dev libjpeg62-dbg
  $ sudo apt-get install ffmpeg libavcodec-dev libavcodec52 libavformat-dev libavformat52 libswscale-dev libavfilter-dev
  $ sudo apt-get install libdc1394-22 libdc1394-22-dev
  $ sudo apt-get install python2.6 python2.6-dev python-numpy
  $ sudo apt-get install libtbb2 libtbb-dev
  $ sudo apt-get install libgstreamer0.10-0 libgstreamer0.10-dev libgstreamer-plugins-base0.10-0 libgstreamer-plugins-base0.10-dev

OpenCV本体を入手して,コンパイル Obtain the main body of OpenCV to compile

OpenCV公式ページ からLinux用の最新版パッケージをダウンロードして,ダウンロードしたパッケージを解凍する

Download the latest package for Linux from OpenCV official page, then decompress downloaded package.

 $ tar jxvf OpenCV-2.2.0.tar.bz2


From decompressed folder, perform cmake for OpenCV.

  $ cd OpenCV-2.2.0


Here, confirm whether features and the setting that you want to use in OpenCV are correct and effective. If the message which is displayed by cmake is such as the bottom, then it should be basically all right. For the person who wants to install invalid features, please try it in various ways.

-- General configuration for opencv 2.2.0 =====================================
--     Built as dynamic libs?:    ON
--     Compiler:
--     C++ flags (Release):         -Wall -pthread -march=i686 -ffunction-sections    -fomit-frame-pointer -O3 -ffast-math -msse -msse2 -mfpmath=387 -DNDEBUG
--     C++ flags (Debug):           -Wall -pthread -march=i686 -ffunction-sections    -O0 -ggdb3 -DDEBUG -D_DEBUG
--     Linker flags (Release):
--     Linker flags (Debug):
--   GUI:
--     GTK+ 2.x:                  1
--     GThread:                   1
--   Image I/O:
--     JPEG:                      TRUE
--     PNG:                       TRUE
--     TIFF:                      TRUE
--     JPEG 2000:                 build
--     OpenEXR:                   NO
--   Video I/O:
--     DC1394 1.x:
--     DC1394 2.x:                1
--     FFMPEG:                    1
--       codec:                   1
--       format:                  1
--       util:                    1
--       swscale:                 1
--       gentoo-style:            1
--     GStreamer:                 1
--     UniCap:                    FALSE
--     PvAPI:
--     V4L/V4L2:                  1/1
--     Xine:                      FALSE
--   Interfaces:
--     Python:                    ON
--     Python interpreter:        /usr/bin/python2.6
--     Python numpy:              YES
--     Use IPP:                   NO
--     Use TBB:                   YES
--     Use Cuda:                  NO
--     Use Eigen2:                NO
--   Documentation:
--     Build PDF                  NO
--     Doxygen HTMLs              NO
--     Install path:              /usr/local
--     cvconfig.h is in:          /home/adhoc/Downloads/OpenCV-2.2.0
-- -----------------------------------------------------------------


If setting has no problem, execute make to install.

  $ make -j2
  $ sudo make install
  $ sudo ldconfig


Confirm whether it is installed safely. Execute the following and compile safely to execute a sample, then it is all right.

  $ cd OpenCV-2.2.0/samples/c
  $ sh

CUDAを有効にさせたい人へ To the person who wants to validate CUDA

OpenCV2.2から実験的にCUDAがサポートされました. GPUを使って計算を行うため,画像処理の計算が非常に高速化されると思われます.

Since OpenCV2.2, CUDA is supported experimentally. As calculating using GPU, it seems that calculation of the image processing is speeded up very much.


In addition of the CUDA, various things, such as Qt, are added experimentally. Please try it if you like.

必要となるパッケージのダウンロード The download of a required package

NVIDIA から,以下のパッケージのOSに適切なバージョンをダウンロード

From NVIDIA, download an appropriate version for an OS of the following packages.

  CUDA Toolkit for Ubuntu Linux 10.04
  NVIDIA Performance Primitives library

CUDAインストール Installation of CUDA


Installation of toolkit


Execute the following command and follow the instruction.

  sudo sh ./


It is installed to /usr/local/cuda by default.


Installation of SDK


Move the SDK Folder, which is decompressed by the following command, to the suitable place.

  tar xzvf npp_3.2.16_linux_32.tar.gz

OpenCVのコンパイル A compile of OpenCV


Install it while confirming an output message.


By a message after cmake, confirm that CUDA is valid.

  $ make -j2
  $ sudo make install
  $ sudo ldconfig

FULL_PATH_TO_SDK_FOLDER: SDKのフォルダへの絶対パスで置き換える.

FULL_PATH_TO_SDK_FOLDER: Replace by a full path to the SDK directory.

Macへのインストール Installation to Mac

常用iMac(leopard)へのインストールは,ここ を参照

Refer to here for installation to common use iMac (leopard).


For installation to your Mac, make full use of Port to install.

OpenCV 使い方 How to use OpenCV

カメラの接続確認 Connection confirm of the camera

OpenCVでカメラを使ったプログラミングを行う前に,カメラが接続されているかを確認する. プログラムで画像が表示されないっと言った事態に陥ったときに,これを行っていないとカメラが問題なのか?プログラムなのか?の判別ができない

Before programming using the camera of OpenCV, confirm whether a camera is connected. If it is not performed, judgment can not be done about that problem is caused by camera or program when Image is not displayed in a program.

  USBカメラの場合:  $ cheese
  1394カメラの場合: $ coriander
  In the case of a USB camera:  $ cheese
  In the case of 1394 cameras: $ coriander

ヘッダとコンパイル実行 Header and compile execution


To program with OpenCV, include the following basically.

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


Here, Indicate a sample to acquire image by a camera.

  // opencv-sample.cpp
  #include <iostream>
  #include <cv.h>
  #include <highgui.h>

  int main(int argc, char **argv){

    // Define an image array
    cv::Mat raw;

    // Camera setting 
    cv::VideoCapture camera(0);

    // Connection confirm of the camera
    if( !camera.isOpened() ){
        std::cerr << "ERROR : camera is not opened !!" << std::endl;
        return 1;

        // Capture from a camera
        camera >> raw;
        // Display image
        cv::imshow("raw image", raw);
        // Finishe when pushing q
        if( cv::waitKey(3) == 'q' ){
            // Release cv::Mat and camera
    return 0;


To compile this,

  $ g++ -O2 -g -Wall `pkg-config opencv --cflags` `pkg-config opencv --libs` opencv-sample.cpp

OpenCV2系(c++)でプログラムを書くように心がける. Keep it in mind to write a program with OpenCV2 system (c++).

OpenCVはc++への移行がほぼ完了しており,今後新しく導入される関数などはすべてc++で書かれていると考えられる. また,2系ではcv::Matを筆頭に利便性の向上と計算速度の向上が図られている. そのため,cで書き続けることのメリットが無いので,c++でOpenCVをプログラミングしてください.

For OpenCV, a shift to c++ is almost completed, so all of introduced function newly in future is expected to be written with c++. Also, in the 2 system, cv:: Mat for the first on the list, improvement of the convenience and the improvement of the computing speed are attempted. Therefore, because there is no merit in what you continue to write with c, program OpenCV with c++.


In brief:

  画像配列: IplImagecvMat
  関数  : cvほげほげ(例:cvCreateCameraCapture)
  画像配列: cv::Mat
  関数  : cv::ほげほげ(例:cv::VideoCapture)
  Do not use
  Image arrayIplImagecvMat
  Function  : cvXXX (e.g. cvCreateCameraCapture)
  Image arraycv::Mat
  Function  : cv::XXX (e.g.: cv:: VideoCapture)

メモリのリークを確認する. Confirm memory leakage.

OpenCVは,画像という大きなデータを取り扱う. そのため,cv::Matなどの定義や開放のタイミングを間違えるとメモリがリークして,実行中にプログラムが落ちることになる. なので,メモリのリークが無いかをちゃんと確認する.

OpenCV handles big data called the image. Therefore, when making a mistake about definitions such as cv:: Mat or release timing, memory leak may occurs and program shut down during execution. So, confirm whether there is not the leakage of the memory properly.

Using top command, confirm the memory percentage that your program is using.
If sharply increasing, it is wrong.

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2013-02-18 (月) 20:10:25 (1977d)