【发布时间】:2015-05-26 18:54:12
【问题描述】:
目前我正在迭代一个数组,对于这个数组中的每个值,我正在寻找另一个数组中对应点的最接近值,该数组位于对应点周围的区域内。
综上所述:对于一个数组中的任意一点,距离另一个数组中的对应点需要走多远才能得到相同的值。
代码似乎适用于小型数组,但我现在使用 1024x768 数组,导致我每次运行都要等待很长时间......
任何帮助或建议将不胜感激,因为我已经有一段时间了!
我使用的格式示例矩阵:np.array[[1,2],[3,4]]
#Distance to agreement
#Used later to define a region of pixels around a corresponding point
#to iterate over:
DTA = 26
#To account for noise in pixels - doesnt have to find the exact value,
#just one within +/-130 of it.
limit = 130
#Containers for all pixel value matches and also the smallest distance
#to pixel match
Dist = []
Dist_min = []
#Continer matrix for gamma pass/fail values
Dist_to_agree = np.zeros((i_size,j_size))
#i,j indexes the reference matrix (x), ii,jj indexes the measured
#matrix(y). Finds a match within the limits,
#appends the distance to the match into Dist.
#Then find the minimum distance to a match for that pixel and append it
#to dist_min
for i, k in enumerate(x):
for j, l in enumerate(k):
#added 10 packing to y matrix, so need to shift it by 10 in i&j
for ii in range((i+10)-DTA,(i+10)+DTA):
for jj in range((j+10)-DTA,(j+10)+DTA):
#If the pixel value is within a range to account for noise,
#let it be "found"
if (y[ii,jj]-limit) <= x[i,j] <= (y[ii,jj]+limit):
#Calculating distance
dist_eu = sqrt(((i)-(ii))**2 + ((j) - (jj))**2)
Dist.append(dist_eu)
#If a value cannot be found within the noise range,
#append 10 = instant fail.
else:
Dist.append(10)
try:
Dist_min.append(min(Dist))
Dist_to_agree[i,j] = min(Dist)
except ValueError:
pass
#Need to reset container or previous values will also be
#accounted for when finding minimum
Dist = []
print Dist_to_agree
【问题讨论】:
-
您似乎在
for jj in range(...)循环中有缩进错误。在 Stackoverflow 上复制/粘贴代码时通常会发生这种情况,有时,复制/粘贴会以这种方式呈现,因为您在源代码中混合了空格和制表符 - 您可能需要检查 :-) -
首先,尝试使用 numpy 数组操作而不是逐点评估整个
ii,jj窗口。将ii视为值向量。