サンプルコード
[照度差ステレオ]

基本的な使用例

コード

#include "fie.h"
#include "oal_aloc.h"

// --- リング照明使用の照度差ステレオ法サンプル ---
static INT do_ring_woodham()
{   
    INT status = F_ERR_UNKNOWN;
    INT img_num;//撮像枚数

    // 画像のパス
    
    const CHAR* img_fnames[] = {
        
        "input_img_files/00.png",
        "input_img_files/01.png",
        "input_img_files/02.png",
        "input_img_files/03.png",
        "input_img_files/04.png",
        "input_img_files/05.png",
        "input_img_files/06.png",
        "input_img_files/07.png"
    };


    // 読み込む用画像
    FHANDLE himg_temp = NULL;

    // himg_tempをまとめる画像
    FHANDLE himg_all = NULL;
    FHANDLE himg_all_child = NULL;

    FHANDLE himg_normal_double_after = NULL;

    //法線画像用
    FHANDLE himg_normal_double = NULL;
    FHANDLE himg_normal_uc8 = NULL;

    //曲率画像用
    FHANDLE hdst_curvature_mean_double = NULL;
    FHANDLE hdst_curvature_mean_uc8 = NULL;

    //高さ画像(frankot_chellappa)用
    FHANDLE hdst_depth_frankot_chellappa_double = NULL;
    FHANDLE hdst_depth_frankot_chellappa_uc8 = NULL;

    //高さ画像(horn_brooks)用
    FHANDLE hdst_depth_horn_brooks_double = NULL;
    FHANDLE hdst_depth_horn_brooks_uc8 = NULL;

    INT i;

    INT ch_temp,type_temp, width_temp, height_temp;
    INT img_all_ch, type, width, height;
    DOUBLE light_height, light_radius;

    DOUBLE* slant=NULL;
    DOUBLE* tilt=NULL;
    INT light_order = 0;//時計回り
    DOUBLE initial_angle;

    DOUBLE normal_thresh = 0.10;
    INT curvature_type = F_CURVATURE_MEAN;

    INT iter;

    DOUBLE low_thresh = 5;

    //ファイル数から撮像枚数を計算することにしています。

    img_num = sizeof(img_fnames) / sizeof(CHAR*);
    
    //最初の一枚目の画像読み込み(これを基準にする)
    {
        status = fnFIE_load_img_file(img_fnames[0], &himg_temp, F_COLOR_IMG_TYPE_UC8);
        if (status != F_ERR_NONE) { goto exit; }

        status = fnFIE_img_get_params(himg_temp,&ch_temp,&type_temp,NULL,&width_temp,&height_temp);
        if (status != F_ERR_NONE) { goto exit; }
        
    }

    //まとめる用の画像オブジェクトを確保

    img_all_ch = img_num;
    width = width_temp;
    height = height_temp;
    type = type_temp;

    himg_all = fnFIE_img_root_alloc(F_IMG_UC8, img_all_ch, width, height);
    if (himg_all == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    //1枚目の処理

    himg_all_child = fnFIE_img_child_alloc_single_ch(himg_all, 0, 0, 0, width, height);
    if (himg_all_child == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    status = fnFIE_img_copy(himg_temp, himg_all_child);
    if (status != F_ERR_NONE) { goto exit; }

    fnFIE_free_object(himg_all_child); himg_all_child = NULL;
    fnFIE_free_object(himg_temp); himg_temp = NULL;

    //2枚目以降の処理

    for (i = 1; i<img_num; i++) {
    
        status = fnFIE_load_img_file(img_fnames[i], &himg_temp, F_COLOR_IMG_TYPE_UC8);
        if (status != F_ERR_NONE) { goto exit; }

        status = fnFIE_img_get_params(himg_temp, &ch_temp, NULL,NULL, &width_temp, &height_temp);
        if (status != F_ERR_NONE) { goto exit; }

        if (ch_temp != 1 || width_temp != width || height_temp != height) {
            status = F_ERR_INVALID_IMAGE;
            goto exit;
        }

        himg_all_child = fnFIE_img_child_alloc_single_ch(himg_all, i, 0, 0, width, height);
        if (himg_all_child == NULL) { status = F_ERR_NOMEMORY; goto exit; }

        status = fnFIE_img_copy(himg_temp,himg_all_child);
        if (status != F_ERR_NONE) { goto exit; }

        fnFIE_free_object(himg_all_child); 
        himg_all_child = NULL;
        fnFIE_free_object(himg_temp);
        himg_temp = NULL;
    }

    //前処理
    //黒背景の微妙な画素値の違いによる法線推定の誤りを和らげます

    status=fnFIE_img_max_const(himg_all, low_thresh, himg_all);
    if (status != F_ERR_NONE) { goto exit; }

    //リング照明用で光源情報を計算する
    slant = (DOUBLE*)fnOAL_malloc(img_num * sizeof(DOUBLE));
    if (slant == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    tilt = (DOUBLE*)fnOAL_malloc(img_num * sizeof(DOUBLE));
    if (tilt == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    light_radius = 1;
    light_height = 2;
    initial_angle = 170;

    status= fnFIE_ps_woodham_calc_light_ring(light_height, light_radius, initial_angle, light_order,slant,tilt, img_num);
    if (status != F_ERR_NONE) { goto exit; }

    //----------------------------------------------
    //-----woodhamによる法線推定実行----------------
    //----------------------------------------------

    himg_normal_double = fnFIE_img_root_alloc(F_IMG_DOUBLE, 3, width, height);
    if (himg_normal_double == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    status=fnFIE_ps_woodham(himg_all, slant, tilt, himg_normal_double,NULL);
    if (status != F_ERR_NONE) { goto exit; }
    
    //結果画像保存(可視化用)
    {
        himg_normal_uc8 = fnFIE_img_root_alloc(F_IMG_UC8, 3, width, height);
        if (himg_normal_uc8 == NULL) { status = F_ERR_NOMEMORY; goto exit; }

        status = fnFIE_img_copy_ex(himg_normal_double,himg_normal_uc8, 0, -1, 255.0/(1-(-1)));
        if (status != F_ERR_NONE) { goto exit; }

        status = fnFIE_save_png("himg_normal_uc8.png", himg_normal_uc8 ,-1);
        if (status != F_ERR_NONE) { goto exit; }

    }

    //----------------------------------------------
    //-----曲率画像計算-----------------------------
    //----------------------------------------------

    hdst_curvature_mean_double = fnFIE_img_root_alloc(F_IMG_DOUBLE, 1, width, height);
    if (himg_normal_double == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    status=fnFIE_ps_curvature(himg_normal_double, hdst_curvature_mean_double, normal_thresh, curvature_type);
    if (status != F_ERR_NONE) { goto exit; }
    
    //結果画像保存(可視化用)
    {
        hdst_curvature_mean_uc8 = fnFIE_img_root_alloc(F_IMG_UC8, 1, width, height);
        if (himg_normal_uc8 == NULL) { status = F_ERR_NOMEMORY; goto exit; }

        status = fnFIE_img_copy_ex(hdst_curvature_mean_double, hdst_curvature_mean_uc8, 1, 0, 0);
        if (status != F_ERR_NONE) { goto exit; }

        status = fnFIE_save_png("hdst_curvature_mean_uc8.png", hdst_curvature_mean_uc8, -1);
        if (status != F_ERR_NONE) { goto exit; }

    }

    //----------------------------------------------
    //-----高さ画像計算(frankot_chellappa)----------
    //----------------------------------------------

    hdst_depth_frankot_chellappa_double = fnFIE_img_root_alloc(F_IMG_DOUBLE, 1, width, height);
    if (hdst_depth_frankot_chellappa_double == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    status = fnFIE_ps_frankot_chellappa(himg_normal_double, hdst_depth_frankot_chellappa_double, normal_thresh);
    if (status != F_ERR_NONE) { goto exit; }

    //結果画像保存(可視化用)
    {
        hdst_depth_frankot_chellappa_uc8 = fnFIE_img_root_alloc(F_IMG_UC8, 1, width, height);
        if (hdst_depth_frankot_chellappa_uc8 == NULL) { status = F_ERR_NOMEMORY; goto exit; }

        status = fnFIE_img_copy_ex(hdst_depth_frankot_chellappa_double, hdst_depth_frankot_chellappa_uc8, 1, 0, 0);
        if (status != F_ERR_NONE) { goto exit; }

        status = fnFIE_save_png("hdst_depth_frankot_chellappa_uc8.png", hdst_depth_frankot_chellappa_uc8, -1);
        if (status != F_ERR_NONE) { goto exit; }

    }

    //----------------------------------------------
    //-----高さ画像計算(horn_brooks)----------------
    //----------------------------------------------

    iter = 100;

    hdst_depth_horn_brooks_double = fnFIE_img_root_alloc(F_IMG_DOUBLE, 1, width, height);
    if (hdst_depth_horn_brooks_double == NULL) { status = F_ERR_NOMEMORY; goto exit; }

    status = fnFIE_ps_horn_brooks(himg_normal_double, hdst_depth_horn_brooks_double, normal_thresh,iter);
    if (status != F_ERR_NONE) { goto exit; }

    //結果画像保存(可視化用)
    {
        hdst_depth_horn_brooks_uc8 = fnFIE_img_root_alloc(F_IMG_UC8, 1, width, height);
        if (hdst_depth_horn_brooks_uc8 == NULL) { status = F_ERR_NOMEMORY; goto exit; }

        status = fnFIE_img_copy_ex(hdst_depth_horn_brooks_double, hdst_depth_horn_brooks_uc8, 1, 0, 0);
        if (status != F_ERR_NONE) { goto exit; }

        status = fnFIE_save_png("hdst_depth_horn_brooks_uc8.png", hdst_depth_horn_brooks_uc8, -1);
        if (status != F_ERR_NONE) { goto exit; }

    }

exit:
    // 各種解放
    fnFIE_free_object(himg_all);
    fnFIE_free_object(himg_all_child);
    fnFIE_free_object(himg_temp);

    fnFIE_free_object(himg_normal_double);
    fnFIE_free_object(himg_normal_double_after);
    fnFIE_free_object(himg_normal_uc8);

    fnFIE_free_object(hdst_curvature_mean_double);
    fnFIE_free_object(hdst_curvature_mean_uc8);

    fnFIE_free_object(hdst_depth_frankot_chellappa_double);
    fnFIE_free_object(hdst_depth_frankot_chellappa_uc8);

    fnFIE_free_object(hdst_depth_horn_brooks_double);
    fnFIE_free_object(hdst_depth_horn_brooks_uc8);

    fnOAL_free(slant);
    fnOAL_free(tilt);
    
    return status;
}

INT main()
{
    INT status=F_ERR_UNKNOWN;

    // FIEライブラリのセットアップ
    fnFIE_setup();

    // リング照明でwoodhamの手法による法線推定
    status = do_ring_woodham();

    // ライブラリの終了処理
    fnFIE_teardown();

    return status;
}

入力画像

fie_ps_sample_code_input_00.png

入力画像0チャネル

fie_ps_sample_code_input_01.png

入力画像1チャネル

fie_ps_sample_code_input_02.png

入力画像2チャネル

fie_ps_sample_code_input_03.png

入力画像3チャネル

fie_ps_sample_code_input_04.png

入力画像4チャネル

fie_ps_sample_code_input_05.png

入力画像5チャネル

fie_ps_sample_code_input_06.png

入力画像6チャネル

fie_ps_sample_code_input_07.png

入力画像7チャネル

fie_ps_sample_code_normal.png

出力法線画像

fie_ps_sample_code_curvature_mean.png

出力曲率画像(平均曲率)

fie_ps_sample_code_frankot_chellappa.png

出力高さ画像(Frankot-Chellappa法)

fie_ps_sample_code_horn_brooks.png

出力高さ画像(Horn-Brooks法)


Documentation copyright © 2009-2024 FAST Corporation.
Generated on Fri Aug 9 16:38:48 2024 for FIEライブラリ by doxygen 1.5.6-FASTSP-p2