【发布时间】:2018-09-16 18:12:21
【问题描述】:
我正在尝试计算颜色(或与 17 种颜色中的一种颜色最接近的颜色)出现在图像像素中的次数(以 300x300x3 np 数组的形式给出,浮点值 [0,1])。我已经写了这个,但它似乎效率极低:
for w in range(300):
for h in range(300):
colordistance = float('inf')
colorindex = 0
for c in range(17):
r1 = color[c, 0]
g1 = color[c, 1]
b1 = color[c, 2]
r2 = img[w, h, 0]
g2 = img[w, h, 1]
b2 = img[w, h, 2]
distance = math.sqrt(
((r2-r1)*0.3)**2 + ((g2-g1)*0.59)**2 + ((b2-b1)*0.11)**2)
if distance < colordistance:
colordistance = distance
colorindex = c
colorcounters[colorindex] = colorcounters[colorindex] + 1
有什么方法可以提高这个位的效率吗?我已经在外部循环中使用多处理。
【问题讨论】:
-
在您的第 5 行中,您缺少范围的值。我猜是3?另外,您是否考虑过使用生成器?如果您以前没有听说过,我可以详细说明
-
numba 可能会大大加快这个循环,看看吧。
-
@HarisNadeem 是 17 - 检查每种颜色。图片是从 h5 文件中获取的——只是这个循环需要很长时间。
-
您可以通过首先检查是否完全匹配
if all([a==b for a, b in [(r1, r2), (g1, g2), (b1, b2)]]): colorcounters[c] += 1; continue来短路内部for循环 -
@thebjorn 这些图像是真实世界的照片,我只检查 17 种网络颜色,因此我要计算距离。它们将完全相等的情况非常有限。
标签: python performance for-loop processing-efficiency