當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python skimage.feature.match_template用法及代碼示例

用法:

skimage.feature.match_template(image, template, pad_input=False, mode='constant', constant_values=0)

使用歸一化相關性將模板與 2-D 或 3-D 圖像匹配。

輸出是一個值在 -1.0 和 1.0 之間的數組。給定位置的值對應於圖像和模板之間的相關係數。

對於 pad_input=True 匹配對應於模板的中心,否則對應於模板的左上角。要找到最佳匹配,您必須在響應(輸出)圖像中搜索峰值。

參數

image(M, N[, D]) 數組

2-D 或 3-D 輸入圖像。

template(m, n[, d]) 數組

要定位的模板。它必須是 (m <= M, n <= N[, d <= D])。

pad_inputbool

如果為 True,則填充圖像以使輸出與圖像大小相同,並且輸出值對應於模板中心。否則,輸出是一個形狀為 (M - m + 1, N - n + 1) 的數組,用於 (M, N) 圖像和 (m, n) 模板,並且匹配對應於原點(左上角)的模板。

mode numpy.pad ,可選

填充模式。

constant_values numpy.pad ,可選

mode='constant' 結合使用的常量值。

返回

output數組

具有相關係數的響應圖像。

注意

[1] 中提供了有關互相關的詳細信息。此實現使用圖像和模板的 FFT 卷積。參考文獻[2] 提供了類似的推導,但本參考文獻中提供的近似值未在我們的實現中使用。

參考

1

J. P. Lewis, “Fast Normalized Cross-Correlation”, Industrial Light and Magic.

2

Briechle and Hanebeck, “Template Matching using Fast Normalized Cross Correlation”, Proceedings of the SPIE (2001). DOI:10.1117/12.421129

例子

>>> template = np.zeros((3, 3))
>>> template[1, 1] = 1
>>> template
array([[0., 0., 0.],
       [0., 1., 0.],
       [0., 0., 0.]])
>>> image = np.zeros((6, 6))
>>> image[1, 1] = 1
>>> image[4, 4] = -1
>>> image
array([[ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -1.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.]])
>>> result = match_template(image, template)
>>> np.round(result, 3)
array([[ 1.   , -0.125,  0.   ,  0.   ],
       [-0.125, -0.125,  0.   ,  0.   ],
       [ 0.   ,  0.   ,  0.125,  0.125],
       [ 0.   ,  0.   ,  0.125, -1.   ]])
>>> result = match_template(image, template, pad_input=True)
>>> np.round(result, 3)
array([[-0.125, -0.125, -0.125,  0.   ,  0.   ,  0.   ],
       [-0.125,  1.   , -0.125,  0.   ,  0.   ,  0.   ],
       [-0.125, -0.125, -0.125,  0.   ,  0.   ,  0.   ],
       [ 0.   ,  0.   ,  0.   ,  0.125,  0.125,  0.125],
       [ 0.   ,  0.   ,  0.   ,  0.125, -1.   ,  0.125],
       [ 0.   ,  0.   ,  0.   ,  0.125,  0.125,  0.125]])

相關用法


注:本文由純淨天空篩選整理自scikit-image.org大神的英文原創作品 skimage.feature.match_template。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。