Skip to content

Instantly share code, notes, and snippets.

@tooshitaka
Created December 15, 2016 09:49
Show Gist options
  • Save tooshitaka/1e642942d9dfa0a4465b4139e4211654 to your computer and use it in GitHub Desktop.
Save tooshitaka/1e642942d9dfa0a4465b4139e4211654 to your computer and use it in GitHub Desktop.
#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(void)
{
try
{
// 画像を格納する2次元配列
// カラー画像として読み込むのでrgb_pixelを指定
array2d<rgb_pixel> image;
// ivy.bmpをimageに読み込む
load_image(image, "ivy.bmp");
// ガウシアンフィルタをかける
// unsigned charなのでグレイスケール画像
array2d<unsigned char> blurred_img;
rectangle rect;
// 分散シグマ
double sigma = 1.0;
gaussian_blur(image, blurred_img, sigma);
// ウィンドウを作ってガウシアンフィルタの結果を表示
image_window win_blurred(blurred_img, "Blurred image");
// エッジ抽出
// まず水平方向と垂直方向の勾配画像を計算
array2d<short> horz_gradient, vert_gradient;
array2d<unsigned char> edge_image;
sobel_edge_detector(blurred_img, horz_gradient, vert_gradient);
image_window win_hoz(horz_gradient, "Horizontal gradient");
image_window win_vert(vert_gradient, "Vertical gradient");
// NMSを使ってエッジの精度を上げる
suppress_non_maximum_edges(horz_gradient, vert_gradient, edge_image);
// エッジ画像を表示
image_window my_window(edge_image, "Edge image");
// エッジ画像をヒートマップとして表示
image_window win_hot(heatmap(edge_image), "Edge image as heat map");
// エッジ画像をjet color mapをもとに表示
image_window win_jet(jet(edge_image), "Edge image based jet map color");
// オリジナル画像を表示
image_window my_window2(image, "Original Image");
// エッジ画像上でダブルクリックされた座標の値を表示
point p;
while (my_window.get_next_double_click(p))
{
cout << "クリックされた座標: " << p << endl;
cout << "ピクセル値: " << (int)edge_image[p.y()][p.x()] << endl;
}
// オリジナル画像を表示しているウィンドウが閉じられるまで待つ
//my_window2.wait_until_closed();
// 領域分割
array2d<unsigned char> region;
segment_image(image, region,500, 500);
image_window win_seg(region, "Region segmented image");
win_seg.wait_until_closed();
}
catch (exception& e)
{
cout << "exception thrown: " << e.what() << endl;
}
}
// ----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment