2017-01-27 147 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