Dlib C++ Library に付属のサンプルプログラムで,カラー画像のエッジ抽出(Ubuntu 上)

本記事では,dlib C++ Library を用いてカラー画像から Sobel エッジ検出を行う方法を説明する.Sobel エッジ検出は,画像中の輝度変化が大きい箇所(輪郭や境界)を抽出する手法である.抽出したエッジを濃淡画像,ヒートマップ,Jet カラーマップの3種類の方法で可視化する例を示す.

サイト内の関連ページ

用語説明

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 のダウンロードは, 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 を使用する(謝辞:画像の作者に感謝します)

Sobel エッジ抽出の例

dlib C++ Library を用いて, Sobel エッジを得る. エッジを濃淡画像(グレースケール)で表示.

#include<dlib/gui_widgets.h>
#include<dlib/image_io.h>
#include<dlib/image_transforms.h>
#include<fstream>

using namespace std;
using namespace dlib;

int main(int argc, char** argv)
{
        array2d<rgb_pixel> img;
        load_image(img, argv[1]);
        array2d<unsigned char> blurred_img;
        gaussian_blur(img, blurred_img);

        // gradient images.
        array2d<short> horz_gradient, vert_gradient;
        array2d<unsigned char> edge_image;
        sobel_edge_detector(blurred_img, horz_gradient, vert_gradient);

        // now we do the non-maximum edge suppression step so that our edges are nice and thin
        suppress_non_maximum_edges(horz_gradient, vert_gradient, edge_image); 
        image_window my_window(edge_image, "Normal Edge Image"); 
	my_window.wait_until_closed();
}

上のソースコードを,a.cppのようなファイル名で保存し, 次の手順でビルドして実行

g++ -I/usr/local/include a.cpp -L/usr/local/lib -ldlib -lpthread -lX11
./a.out home.jpg
./a.out fruits.jpg

Sobel エッジ (ヒートマップで表示)

dlib C++ Library を用いて, Sobel エッジをヒートマップで表示.エッジの強度を暖色系のグラデーションで可視化する.

#include<dlib/gui_widgets.h>
#include<dlib/image_io.h>
#include<dlib/image_transforms.h>
#include<fstream>

using namespace std;
using namespace dlib;

int main(int argc, char** argv)
{
        array2d<rgb_pixel> img;
        load_image(img, argv[1]);
        array2d<unsigned char> blurred_img;
        gaussian_blur(img, blurred_img);

        // gradient images.
        array2d<short> horz_gradient, vert_gradient;
        array2d<unsigned char> edge_image;
        sobel_edge_detector(blurred_img, horz_gradient, vert_gradient);

        // now we do the non-maximum edge suppression step so that our edges are nice and thin
        suppress_non_maximum_edges(horz_gradient, vert_gradient, edge_image); 
        image_window my_window(heatmap(edge_image)); 
	my_window.wait_until_closed();
}

上のソースコードを,a.cppのようなファイル名で保存し, 次の手順でビルドして実行

g++ -I/usr/local/include a.cpp -L/usr/local/lib -ldlib -lpthread -lX11
./a.out home.jpg
./a.out fruits.jpg

Sobel エッジ (Jet カラーマップで表示)

dlib C++ Library を用いて, Sobel エッジを Jet カラーマップで表示.エッジの強度を青から赤へのグラデーション(虹色)で可視化する.

#include<dlib/gui_widgets.h>
#include<dlib/image_io.h>
#include<dlib/image_transforms.h>
#include<fstream>

using namespace std;
using namespace dlib;

int main(int argc, char** argv)
{
        array2d<rgb_pixel> img;
        load_image(img, argv[1]);
        array2d<unsigned char> blurred_img;
        gaussian_blur(img, blurred_img);

        // gradient images.
        array2d<short> horz_gradient, vert_gradient;
        array2d<unsigned char> edge_image;
        sobel_edge_detector(blurred_img, horz_gradient, vert_gradient);

        // now we do the non-maximum edge suppression step so that our edges are nice and thin
        suppress_non_maximum_edges(horz_gradient, vert_gradient, edge_image); 
        image_window my_window(jet(edge_image)); 
	my_window.wait_until_closed();
}

上のソースコードを,a.cppのようなファイル名で保存し, 次の手順でビルドして実行

g++ -I/usr/local/include a.cpp -L/usr/local/lib -ldlib -lpthread -lX11
./a.out home.jpg
./a.out fruits.jpg