2017-01-27 106 views
1

我在目标图像中的每种RGB颜色的key: 'Color'list字典中都有一堆独特的RGB颜色。Python - 独特元素的计算频率

我想:

  • 遍历目标RGB颜色列表
  • 检查,如果该元素匹配任何在key: 'Color'
  • 的颜色如果匹配我想改变key: frequency增加它由一个(+ = 1)

最后,我希望实现这样的过程dict: Frequency WIL的末端更新frequency['Frequency'] l包含一串(Color,Frequency)。然后,我想从低频到高频进行排序,并打印每对RGB颜色+外观数量。

这里是我到目前为止的代码:

from PIL import Image 

im = Image.open('test.png').convert('RGB') 
im2 = Image.open('test2.png').convert('RGB') 

unique_colors = set() 

def get_unique_colors(img): 
    for i in range(0,img.size[0]): 
     for j in range(0,img.size[1]): 
      r,g,b = img.getpixel((i,j)) 
      unique_colors.add((r,g,b)) 
    return(unique_colors) 

unique_colors = get_unique_colors(im) 

all_colors = [] 

def get_all_colors(img): 
    for i in range(0,img.size[0]): 
     for j in range(0,img.size[1]): 
      r,g,b = rgb_im.getpixel((i,j)) 
      all_colors.append((r,g,b)) 
    return(all_colors) 

all_colors = get_all_colors(im2) 

frequency = {'Color': list(unique_colors), 'Frequency': [0 for x in range(0,len(unique_colors))]} 

我面临许多问题与我缺乏操作能力的字典,是不是真的适合使用字典这样的数据存储在这种情况下?

回答

0

我认为你的字典创建不正确。

frequency = dict(zip(list(unique_colors), [0 for x in range(0,len(unique_colors))])) 

zip函数使两个列表一起为键和值:如果您创建dict像下面你可以有一个字典(颜色,频率)结构。如果unique_colors={'red','green','blue'},这将创建一个字典,如:

frequency = {'red': 0, 'green': 0, 'blue': 0} 

之后,您可以更新字典为:

frequency['red']+=1 

而且字典变得{'red': 1, 'green': 0, 'blue': 0}

+0

谢谢,它帮助了很多。 之后我只不得不做: '为电子在all_colors: 对于i在unique_colors: 如果E == I: 频率[E] + = 1' 并且比每个(R,G,B)元组将会更新其计数器。 – EduGord

+0

很高兴能够帮助! – ilke444

0

使用字典是一个伟大的想法,事实证明标准lib已经为你做了一些工作,用collections.Counter来计算你放入它的东西。添加itertools.product通过所有像素位置运行,折腾的是到一个自定义像素迭代器,你会得到

from PIL import Image 
import collections 
import itertools 

def iter_colors(img): 
    coordinates = itertools.product(range(img.size[0]), range(img.size[1])) 
    return map(img.getpixel, coordinates) 

im = Image.open('test.png').convert('RGB') 
im2 = Image.open('test2.png').convert('RGB') 

unique_colors = set(iter_colors(im)) 
print("unique", unique_colors) 

frequencies = collections.Counter((rgb for rgb in iter_colors(im2) 
    if rgb in unique_colors)) 
print("frequencies", frequencies) 

# counter keys are rgb tuples and velue is number of times seen 
rgbs_sorted = list(sorted(frequencies)) 
print("im2 rgb values sorted by value:", ", ".join(
    str(rgb) for rgb in rgbs_sorted)) 
print("im2 rgb values sorted by most common:", ", ".join(
    str(rgb) for rgb in frequencies.most_common())) 
print("one rgb value", frequencies[rgbs_sorted[0]]) 

在测试图像,这回

unique {(0, 0, 255), (191, 191, 191), (0, 255, 0)} 
frequencies Counter({(191, 191, 191): 45, (0, 0, 255): 44, (0, 255, 0): 32}) 
im2 rgb values sorted by value: (0, 0, 255), (0, 255, 0), (191, 191, 191) 
im2 rgb values sorted by most common: ((191, 191, 191), 45), ((0, 0, 255), 44), ((0, 255, 0), 32) 
one rgb value 44 
+0

感谢您的回应,非常简短的脚本,它的工作原理。唯一的问题是我不确定如何从这个数据结构中检索特定的RGB颜色及其值。 – EduGord

+0

它们的工作方式与dicts非常相似(请参阅https://docs.python.org/3.3/library/collections.html#collections.Counter中的文档)。我已经添加了一些样本。 – tdelaney