モジュール

			
/*********************************************
 template_matching.cc
 テンプレートマッチング(拡張Reduction使用)
*********************************************/
// 結果を挿入する1次元配列 result は分割無しで
// 各計算機が保有していることが前提

vs_module
template_matching(iImage data, iImage temp, ivalue result)
{
    int o_width = orig_width(temp)/2;
    int o_heigh = orig_heigh(temp)/2;

    // 全比較が可能な領域以外はマッチングを行わない
    vsSetBoundary2D(max(o_width, o_heigh));

    parallel<2,2>(x,y) {
	int sum = 0;
	for(int j= -o_heigh; j < o_heigh; j+=2)
	    for(int i= -o_width; i < o_width; i+=2)
		sum += abs(data[+i][+j] - temp[[i+o_width]][[j+o_heigh]]);

	if(result[[0]] > sum) {
	    result[[0]] = sum;
	    result[[1]] = l2g_x(data, x); // local to global (アドレス変換)
	    result[[2]] = l2g_y(data, y); // local to global (アドレス変換)
	}
    }
    // 各計算機の結果を統一する
    vsUnion(result, vsMIN_DEPEND_ON_FIRST);
}


実行フロープログラム

/****************************************
  template_matching.vpe
  テンプレートマッチング用
 ****************************************/

// 通信処理初期化 -------------

#pragma host_name	host1
#pragma host_name	host2
#pragma host_name	host3
#pragma host_name	host4

// データの初期化 -------------

imgLoad("all.pgm", data);
imgLoad("template.pgm", temp);

int result[3];
result[0] = 999999;	// 適当な大きい数値を.

int c;
c = width(temp) / 2;

set(data, CACHE, c);
set(temp, DIVISION, ALL);
set(result,DIVISION,ALL);
module("template_matching", data, temp, result);

// 結果の表示   ----------------
put("matching point is (");
put(result[1]);
put(",");
put(result[2]);
put(")");
put(" at ");
put(result[0]);
put("\n");


実行方法

  1. 全体画像、テンプレートの用意
  2. モジュールのコンパイル
    % vs_makemodule template_matching.cc
  3. プログラムの実行
    % vios_run < template_matching.vpe


実行フロー設定項目


モジュールプログラム説明

実行結果

テンプレートと全体画像がもっともマッチした時のテンプレートの中心がある座標を出力します。