Dlib C++ Library に付属のサンプルプログラムで SURF を求める(Ubuntu 上)
SURF(Speeded Up Robust Features)は、画像から特徴点を高速に検出・記述するアルゴリズムである。物体認識、画像マッチング、パノラマ合成などに広く利用されている。
【サイト内の関連ページ】
- 説明資料: Dlib の機能概要 [PDF], [パワーポイント]
- 顔情報処理の Python プログラム(Dlib,face_recognition を使用) について: 別ページ »にまとめ
- Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア): 別ページ »にまとめている.
【用語説明】
- Dlib
Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
Ubuntu を使うとして手順を説明する.
前準備
Ubuntu のシステム更新
Ubuntu で OS のシステム更新を行うときは, 次のコマンドを実行.
# パッケージリストの情報を更新
sudo apt update
# インストール済みのパッケージを包括的に更新 (依存関係も考慮)
sudo apt full-upgrade
# 変更をシステム全体に確実に反映させるために再起動
sudo shutdown -r now
いくつかのパッケージのインストール
sudo apt install libx11-dev
DLib のインストール(Ubuntu 上)
DLib のインストール(Ubuntu 上): 別ページ »で説明
画像ファイル fruits.jpg, home.jpg のダウンロード
画像ファイル fruits.jpg, home.jpg のダウンロードは, Ubuntu上で次のコマンドを実行する.
curl -L https://github.com/opencv/opencv/blob/master/samples/data/fruits.jpg?raw=true -o fruits.jpg
curl -L https://github.com/opencv/opencv/blob/master/samples/data/home.jpg?raw=true -o home.jpg
Windows を使用する場合は,コマンドプロンプトを管理者として開き 次のコマンドを実行する.
curl -L https://github.com/opencv/opencv/blob/master/samples/data/fruits.jpg?raw=true -o fruits.jpg
curl -L https://github.com/opencv/opencv/blob/master/samples/data/home.jpg?raw=true -o home.jpg
上のコマンドがうまく実行できないときは, 別ページを参考にダウンロードを行う.
https://github.com/opencv/opencv/tree/master/samples/data
で公開されている fruits.jpg, home.jpg を使用する(謝辞:画像の作者に感謝します)
SURF を標準出力に表示
dlib C++ Library を用いて SURF を求める。
#include<dlib/image_keypoint/draw_surf_points.h>
#include<dlib/image_io.h>
#include<dlib/image_keypoint.h>
#include<fstream>
using namespace std;
using namespace dlib;
int main(int argc, char** argv)
{
array2d<rgb_pixel> img;
load_image(img, argv[2]);
std::vector<surf_point> sp = get_surf_points(img);
cout << "number of SURF points found: "<< sp.size() << endl;
std::vector<surf_point>::iterator it;
for( it = sp.begin(); it != sp.end(); it++ )
{
cout << "center of first SURF point: "<< it->p.center << endl;
cout << "pyramid scale: " << it->p.scale << endl;
cout << "SURF descriptor: \n" << it->des << endl;
}
if ( atoi(argv[1]) != 0 ) {
image_window my_window(img);
draw_surf_points(my_window, sp);
my_window.wait_until_closed();
}
}
上のソースコードを,a.cppのようなファイル名で保存し, 次の手順でビルドして実行
g++ -I/usr/local/include a.cpp -L/usr/local/lib -ldlib -lpthread -lX11
./a.out 0 home.jpg
./a.out 0 fruits.jpg
第1引数は表示モードを指定する。0 を指定すると SURF 特徴点の情報を標準出力に表示する。1 を指定すると、標準出力への表示に加えて、画像ウィンドウに特徴点を描画して表示する。第2引数には処理対象の画像ファイル名を指定する。
./a.out 1 home.jpg
./a.out 1 fruits.jpg