I'm trying to count the number of times a colour (or a closest match to one of 17 colours) appears in the pixels of an image (given as 300x300x3 np array, float values [0,1]). I've written this but it seems to be extremely inefficient:
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
Are there any ways I can improve the efficiency of this bit? I'm already using multiprocessing for an outer loop.