グレイサーチ(正規化相関サーチ)¶
グレイサーチを行い、サーチ結果を画像として保存するサンプルコードです。 Matplotlib を使用します。
import pyfie
import matplotlib.pyplot as plt
# 利便のため PyFIE 関数が返すエラーコードに応じて例外を発生させる機能を有効化
pyfie.ctrl.enable_f_err_exception(True)
# 入力画像読み込み
hpat = pyfie.imread("gs_pattern.png")
htar = pyfie.imread("gs_target.png")
# グレイサーチオブジェクトの作成
hgs_pat = pyfie.fnFIE_gs2_pattern_alloc(
hpat, None, hpat.width / 2 * 100, hpat.height /
2 * 100, pyfie.F_COMP_MODE_SMOOTH, None
)
hgs = pyfie.fnFIE_gs2_alloc(0, 0, 0)
# サーチウインドウの設定
win = pyfie.BOX_T((0, 0), (htar.width - 1, htar.height - 1))
max_nresults = 10
# データ受け取りのための PyFIE データ型インスタンスを作成
results = pyfie.F_GS_RESULT.ARRAY(max_nresults)
nresults = pyfie.INT()
# サーチを実行
pyfie.fnFIE_gs2_search_enforce2(
hgs,
hgs_pat,
htar,
win,
5000,
6000,
False,
False,
0,
0,
2, # サーチ開始圧縮度
0, # サーチ終了圧縮度
pyfie.F_GS2_SUBPXL_NEIB_8,
results,
max_nresults,
nresults
)
# サーチ結果を表示
print("num results:", nresults)
for i in range(nresults):
result = results[i]
print("#{}: x = {}, y = {}, score = {}".format(
i, result.x / 100, result.y / 100, result.score
))
# 結果をプロットしてファイルに保存
htar.imshow()
pyfie.mpltools.plot_gs2_results(hgs_pat, results, nresults)
plt.savefig("gs_result.png")
処理結果例¶
$ python sample_gray_search.py
num results: 1
#0: x = 223.0, y = 218.0, score = 9999
パタン画像

サーチ対象画像

結果画像
