テンプレートを画像に沿ってシフトすることにより、テンプレートをバイナリ画像 (白黒のみ) と一致させようとしています。テンプレートと画像の間の最小距離を、この最小距離が発生した対応する位置とともに返します。例えば:
画像:
0 1 0
0 0 1
0 1 1
テンプレート:
0 1
1 1
このテンプレートは、位置 (1,1) で画像に最もよく一致し、距離は 0 になります。これまでのところ、それほど難しくはなく、トリックを実行するコードを既に取得しています。
def match_template(img, template):
mindist = float('inf')
idx = (-1,-1)
for y in xrange(img.shape[1]-template.shape[1]+1):
for x in xrange(img.shape[0]-template.shape[0]+1):
#calculate Euclidean distance
dist = np.sqrt(np.sum(np.square(template - img[x:x+template.shape[0],y:y+template.shape[1]])))
if dist < mindist:
mindist = dist
idx = (x,y)
return [mindist, idx]
しかし、必要なサイズの画像 (500 x 200 ピクセルの画像と 250 x 100 のテンプレート) の場合、これにはすでに約 4.5 秒かかり、遅すぎます。そして、行列の乗算を使用して同じことをより迅速に実行できることを知っています(matlabでは、im2colとrepmatを使用してこれを実行できると信じています)。python/numpyでそれを行う方法を誰かに説明してもらえますか?
ところで。必要なことを正確に行うopencv matchTemplate関数があることは知っていますが、後でコードを変更する必要があるかもしれないので、完全に理解して変更できるソリューションを好むでしょう。
ありがとう!
編集:opencvがこれを0.2秒未満で行う方法を誰かが説明できれば、それも素晴らしいでしょう。ソース コードをざっと見てみましたが、これらのことは常に非常に複雑に見えます。
edit2: Cython コード
import numpy as np
cimport numpy as np
DTYPE = np.int
ctypedef np.int_t DTYPE_t
def match_template(np.ndarray img, np.ndarray template):
cdef float mindist = float('inf')
cdef int x_coord = -1
cdef int y_coord = -1
cdef float dist
cdef unsigned int x, y
cdef int img_width = img.shape[0]
cdef int img_height = img.shape[1]
cdef int template_width = template.shape[0]
cdef int template_height = template.shape[1]
cdef int range_x = img_width-template_width+1
cdef int range_y = img_height-template_height+1
for y from 0 <= y < range_y:
for x from 0 <= x < range_x:
dist = np.sqrt(np.sum(np.square(template - img[ x:<unsigned int>(x+template_width), y:<unsigned int>(y+template_height) ]))) #calculate euclidean distance
if dist < mindist:
mindist = dist
x_coord = x
y_coord = y
return [mindist, (x_coord,y_coord)]
img = np.asarray(img, dtype=DTYPE)
template = np.asarray(template, dtype=DTYPE)
match_template(img, template)