我正在尝试解决运动跟踪的对应问题:
在帧n中从帧n-1中找到给定的维数m * n块。我正在使用opencv和python(这两个都是第一次),我正在计算归一化的平方差,但它太慢了。我想在某种程度上我可以使用离散傅里叶变换,但我无法弄清楚我是如何做到的!
def match(img, block):
# img is the frame n, block is from frame n-1
w, h = img.shape[:2]
output = np.zeros( (w,h) ) + 255
for x in range( w ):
for y in range( h ):
output[x, y] = evaluate(img, (x,y) , block)
# the minimum value is the position of the block into the frame n
return output
def evaluate( img, point, block):
m, n = block.shape[:2]
w, h = img.shape[:2]
a = (m-1)/2
b = (n-1)/2
x, y = point
response = 0
for s in range( -a, a+1 ):
for t in range(-b, b+1 ):
if x+s >= w or x+s < 0 or y+t >= h or y+t < 0:
pixel = 0
else:
pixel = img[x+s, y+t]
# normalized squared difference
response = response + (pow((block[ 1+s, 1+t] - pixel), 2) / (m*n))
return response