金子邦彦研究室プログラミングDlib C++ ライブラリDlib C++ Library に付属のサンプルプログラムで,カラー画像のエッジ抽出(Ubuntu 上)

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

Ubuntu を使うとして手順を説明する.

前準備

Ubuntu のシステム更新

UbuntuUbuntu で OS のシステム更新を行うときは, 次のコマンドを実行.

sudo apt -y update
sudo apt -yV upgrade
sudo /sbin/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.cのようなファイル名で保存し, 次の手順でビルドして実行

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

[image]

[image]

[image]

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.cのようなファイル名で保存し, 次の手順でビルドして実行

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

[image]

[image]

[image]

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(jet(edge_image));  
	my_window.wait_until_closed();
}

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

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

[image]

[image]

[image]