我已经设法用numpy操作来计算NUBN,所以速度很快,但是我找不到一种方法来逃避python计算DRD部分的缓慢循环。以下是我目前对DRD的计算:
def drd(im, im_gt):
height, width = im.shape
W = np.array([[1/math.sqrt(x**2+y**2) if x != 0 or y != 0 else 0 for x in range(-2, 3)] for y in range(-2, 3)])
W /= W.sum()
drd = 0
s = []
for y, x in zip(*np.where(im_gt != im)):
if x > 1 and y > 1 and x + 2 < width and y + 2 < height:
s.append(im_gt[y-2:y+3, x-2:x+3] == im_gt[y, x])
else:
for yy in range(y-2, y+3):
for xx in range(x-2, x+3):
if xx > 1 and yy > 1 and xx < width - 1 and yy < height - 1:
drd += abs(im_gt[yy, xx] - im[y, x]) * W[yy-y+2, xx-x+2]
return drd + np.sum(s * W)
drd(np.random.choice([0, 1], size=(100, 100)), np.random.choice([0, 1], size=(100, 100)))
有人能想出一个更快的方法吗?1000x1000上的计时:
使用numpy加快速度的第一步是将操作序列分解为可应用于整个阵列的内容。让我们从一个简单的问题开始:删除
W
计算中的理解:下一件事(上面用
where=W.astype(bool)
暗示)是在适当的地方使用掩蔽将条件应用于整个数组。您的算法如下所示:你可以用} 中实现。通过使用
W
的卷积来计算。只需丢弃im == im_gt
的位置。需要通过从W.sum()
中减去来翻转im_gt == 1
的位置,因为需要对零求和,而不是对这些元素求和。卷积在^{mode='same'
并仔细调整边缘像素,可以获得相同的边缘效果。您可以通过使用一个数组进行卷积来欺骗和获取边和:相关问题 更多 >
编程相关推荐