<分区>
我希望在以较大二维阵列的 (x,y) 位置为中心的 WxW 窗口中找到最接近匹配的 NxN block 。下面的代码工作正常,但对于我的需要来说速度很慢,因为我需要多次运行此操作。有一个更好的方法吗?? 这里N=3,W=15,x=15,y=15并且(bestx,besty)是最佳匹配 block 的中心
import numpy as np
## Generate some test data
CurPatch = np.random.randint(20, size=(3, 3))
Data = np.random.randint(20,size=(30,30))
# Current Location
x,y = 15,15
# Initialise Best Match
bestcost = 999.0
bestx = 0;besty=0
for Wy in xrange(-7,8):
for Wx in xrange(-7,8):
Ywj,Ywi = y+Wy,x+Wx
cost = 0.0
for py in xrange(3):
for px in xrange(3):
cost += abs(Data[Ywj+py-1,Ywi+px-1] - CurPatch[py,px])
if cost < bestcost:
bestcost = cost
besty,bestx = Ywj,Ywi
print besty,bestx