简体   繁体   English

Python 类型错误:列表索引必须是整数或切片,而不是元组

[英]Python TypeError: list indices must be integers or slices, not tuple

I am trying to extract features from an image img using convolution.我正在尝试使用卷积从图像img中提取特征。

img_copy = np.copy(img)
x = img_copy.shape[0]
y = img_copy.shape[1]

matrix = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]] # convolution matrix
weight = 1

def conv(x, y):
    val = 0.0
    for row,i in enumerate([-1, 0, 1]):
        for col,j in enumerate([-1, 0, 1]):
            val = val + img[x+j, y+i]*matrix[row, col]
    val = val*weight

    return val

for i in range(1, x-1):
    for j in range(1, y-1):
        pixel = conv(i, j)
        if(pixel<0):
            pixel = 0
        if(pixel>255):
            pixel = 255

Executing this code block throws the following error:执行此代码块会引发以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-66-84eb09b3a0b7> in <module>
      1 for i in range(1, x-1):
      2     for j in range(1, y-1):
----> 3         pixel = conv(i, j)
      4         if(pixel<0):
      5             pixel = 0

<ipython-input-65-88737f90ffac> in conv(x, y)
      6     for row,i in enumerate([-1, 0, 1]):
      7         for col,j in enumerate([-1, 0, 1]):
----> 8             val = val + img[x+j, y+i]*matrix[row, col]
      9     val = val*weight
     10 

TypeError: list indices must be integers or slices, not tuple

Any help to resolve this issue is appreciated.任何解决此问题的帮助表示赞赏。

Your matrix is not a numpy array, but a Python list, thus matrix[row, col] can not be performed.您的matrix不是numpy 数组,而是 Python 列表,因此无法执行matrix[row, col]

You thus should convert this to a numpy array:因此,您应该将其转换为 numpy 数组:

matrix = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]])
val = val + img[x+j, y+i]*matrix[row, col]

Two values separated by a comma is usually interpreted as a tuple.用逗号分隔的两个值通常被解释为一个元组。 Try this instead试试这个

val = val + img[x+j][y+i]*matrix[row][col]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:列表索引必须是整数或切片,而不是元组? - TypeError: list indices must be integers or slices, not tuple? TypeError:列表索引必须是整数或切片,而不是元组 - TypeError: list indices must be integers or slices, not tuple 类型错误:列表索引必须是整数或切片,而不是在 python 中使用 sys 导入的元组 - TypeError: list indices must be integers or slices, not tuple with sys import in python Python棋盘游戏-“类型错误:列表索引必须是整数或切片,而不是元组” - Python Board Game - "TypeError: list indices must be integers or slices, not tuple" Python:“列表索引必须是整数或切片,而不是元组” - Python:'list indices must be integers or slices, not tuple' Python TypeError:元组索引必须是整数或切片,而不是元组? - Python TypeError: Tuple indices must be integers or slices, not tuple? 列表类型错误:列表索引必须是整数或切片,而不是元组 - List of lists TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是元组列表的元组 - TypeError: list indices must be integers or slices, not tuple for list of tuples 新编码器:TypeError:列表索引必须是整数或切片,而不是元组 - New coder: TypeError: list indices must be integers or slices, not tuple TypeError:列表索引必须是整数或切片,而不是电影分级数据的元组 - TypeError: list indices must be integers or slices, not tuple for Movie Rating Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM