2017-07-30 75 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值。

相关问题