IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

はじめてのOpenCVで結果をファイル出力してみる

OpenCVって盛り上がっているのでしょうか?Twitterで検索すると継続的にツイートが出てきますね。

OpenCVAWS EC2のubuntuにインストールして試してみました。サンプルを動かしたところ、GUI前提のようでしたので、結果をファイルに出力するように変更してみました。

facedetect.cpp の変更点

112     {
113         image = imread( "lena.jpg", 1 );
114         if(image.empty()) cout << "Couldn't read lena.jpg" << endl;
115     }
116 //    cvNamedWindow( "result", 1 );     GUI処理なのでここをコメントアウト
117 
118     if( capture )
119     {

271             circle( img, center, radius, color, 3, 8, 0 );
272         }
273     }
274 //    cv::imshow( "result", img );  GUI処理なのでここをコメントアウト
275 cv::imwrite("./test.jpg" , img);  //ファイル出力処理を加える
276 }

実行します。

root@test:/home/ubuntu/download/opencv-2.4.6.1/samples/c# g++ -ggdb `pkg-config --cflags opencv` -o `basename ./facedetect.cpp .cpp` ./facedetect.cpp `pkg-config --libs opencv`
root@test:/home/ubuntu/download/opencv-2.4.6.1/samples/c# ./facedetect ./lena.jpg 
libdc1394 error: Failed to initialize libdc1394

This program demonstrates the cascade recognizer. Now you can use Haar or LBP features.
This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.
It’s most known use is for faces.
Usage:
./facedetect [--cascade=<cascade_path> this is the primary trained classifier such as frontal face]
   [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]
   [--scale=<image scale greater or equal to 1, try 1.3 for example>]
   [--try-flip]
   [filename|camera_index]

see facedetect.cmd for one call:
./facedetect --cascade="../../data/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="../../data/haarcascades/haarcascade_eye.xml" --scale=1.3

During execution:
	Hit any key to quit.
	Using OpenCV version 2.4.6.1

Processing 1 ./lena.jpg
In image read
detection time = 273.735 ms
root@test:/home/ubuntu/download/opencv-2.4.6.1/samples/c# ls ./test.jpg 
./test.jpg
root@test:/home/ubuntu/download/opencv-2.4.6.1/samples/c# 

*別ターミナルより
docs:aws hoge$ scp -i ./秘密鍵ファイル ubuntu@XXX.XX.XX.XXX:/home/ubuntu/download/opencv-2.4.6.1/samples/c/test.jpg ./test.jpg
test.jpg                                              100%  106KB 106.2KB/s   00:00   

出力ファイルはこうなりました。

f:id:naotoogawa:20140409195637j:plain

参考情報