2017-07-30 72 views
-1

我想要的圖像檢索的平均RGB值元組索引必須是整數,而不是元組

def DetectColour((x ,y) ,n, image): 
    r, g, b = 0, 0, 0 
    count = 0 
     for s in range(x, x+n+1): 
      for t in range(y, y+n+1): 
       pixlr, pixlg, pixlb = image[s, t] 
       r += pixlr 
       g += pixlg 
       b += pixlb 
       count += 1 
    return((r/count), (g/count), (b/count)) 

我估計,在東西此代碼的問題,但我不知道該怎麼修復

有問題的錯誤:

Traceback (most recent call last): 
    File "C:\Python27\Sound-o-Colour.py", line 74, in <module> 
    r, g, b = DetectColour((25, 25) ,5 ,image) #finds the average colour in the frame 
    File "C:\Python27\Sound-o-Colour.py", line 19, in DetectColour 
    pixlr, pixlg, pixlb = image[s, t] #Counts the pixels of each colour, red, green and blue 
TypeError: tuple indices must be integers, not tuple 
+0

'image'似乎是一個元組,而不是你所期望的。檢查如何調用此函數。 – user2357112

+0

你期望索引'[s,t]'代表什麼? – deceze

+0

該消息告訴你問題發生在第19行。哪行代碼是這個,你想在這裏做什麼? –

回答

1

當您嘗試訪問列表或元組中的一員,我想形象是你,象這樣sqare括號內的整數做到這一點:

image[0] 

我想你也許試圖做到這一點:

image[s][t] 

這將訪問image列表/元組的INT(S)成員。 如果此成員恰好也是一個列表,您可以通過添加另一個方括號,並在其中指定此成員的索引來訪問它。 如果你的循環帶你通過圖像中的像素矩陣,這也是有意義的,因爲在第一個循環中,您可能會經歷像素行,並通過第二個列並嘗試檢索RGB值。

相關問題