I have a function that eat a couple of seconds. The function should return the Top(n) colors in a given Image. The return must be sorted, so i could work with the rgb values from the first, second, third top most color.
On the fist hand i had an PIL.Image Object that i looped over x,y coords and counted it in an defaultdict. I've replaced the PIL Object in my project with a Numpy array what gived me a big Boost but i don't know how to replace the defaultdict in this situation.
My current solution:
import numpy as np
from scipy import misc # for example Image
from collections import defaultdict
def count_colors(img, n):
img = img.reshape(-1, img.shape[-1])
color = defaultdict(int)
for pixel in img:
rgb = (pixel[0], pixel[1], pixel[2])
color[rgb] += 1
sorted_color = sorted(color.items(), key=lambda k_v: k_v[1], reverse=True)
sorted_color = sorted_color[:n]
return sorted_color
img = misc.face() # example Numpy Image array
top_colors = count_colors(img, n=5)
display(top_colors)
Current Output:
[((9, 9, 9), 1062),
((10, 10, 10), 700),
((8, 8, 8), 668),
((9, 7, 8), 586),
((9, 7, 10), 579)]
Is there a real Numpy way to solve this?