OpenCVインストールメモ




$ sudo apt-get install cmake python-opencv


$ wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.0/OpenCV-2.0.0.tar.bz2/download


$ tar xjvf OpenCV-2.0.0.tar.bz2


$ cd OpenCV-2.0.0


$ mkdir release


$ cd release


$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..


$ make


$ sudo make install/


$ export LD_LIBRARY_PATH=~/home/ziruchi/src/opencv/release/lib:$LD_LIBRARY_PATH


$ sudo ldconfig




#!/usr/bin/env python


# -*- coding: utf-8 -*-


import sys


from opencv.cv import *


from opencv.highgui import *



def faceDetect(imgfile):


# 画像を読み込む


src_img = cvLoadImage(imgfile, CV_LOAD_IMAGE_COLOR)


src_gray = cvCreateImage(cvSize(src_img.width, src_img.height),


IPL_DEPTH_8U, 1)



# ブーストされた分類器のカスケードを読み込む


cascade_name = "/usr/local/share/opencv/haarcascades/haarcascade_frontalface_default.xml"


cascade = cvLoadHaarClassifierCascade(cascade_name, cvSize(1,1))



# メモリを確保し,読み込んだ画像のグレースケール化,ヒストグラムの均一化を行う


storage = cvCreateMemStorage(0)


cvClearMemStorage(storage)


cvCvtColor(src_img, src_gray, CV_BGR2GRAY)


cvEqualizeHist(src_gray, src_gray)



# 顔検出


faces = cvHaarDetectObjects(src_gray, cascade, storage,


1.11, 4, 0, cvSize(40, 40))



# 検出された全ての顔位置に枠を描画する


for c, i in enumerate(faces):


pt1 = cvPoint(int(i.x), int(i.y))


pt2 = cvPoint(int(i.x + i.width), int(i.y + i.height))


cvRectangle(src_img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0);



return src_img



if __name__ == '__main__':


if len(sys.argv) < 2:


print "Usage: facedetect <filename>\n" ;


sys.exit(-1)


img = faceDetect(sys.argv[1])



# 画像を出力


cvSaveImage("output.jpg", img)




参考URL


http://keepkeptkept.seesaa.net/article/129699993.html


http://yamashita.dyndns.org/blog/face-detection-with-opencv/