关于数组、矩阵的说明。参考:关于Python数组和矩阵的用法X[:,0]、X[:,1]、X[:,:,0]、X[:,:,1]、X[:,m:n]和X[:,:,m:n] – 蒋智昊的博客 (chanpinxue.cn)
X[:,0] 取二维数组中第一维的所有数据。
X[:,1] 取二维数组中第二维的所有数据。
X[:,m:n] 取二维数组中第m维到第n-1维的所有数据。
同理三维及更高维度。 “,”分割维度,“:”表示取值范围区间
测试:
import numpy as np
import cv2
if __name__ == '__main__':
a = [[0,0,150], [1,0,150], [2,0,150]]
print(a)
print(a[:, 0])
print(a[:, 1])
print(a[:, 2])
报错,“TypeError: list indices must be integers or slices, not tuple”
原因:这是因为此时矩阵存储在列表(list)中,而列表中的每一个元素大小可能不同,因此不能直接取其某一列进行操作
解决方案
可以利用numpy.array
函数将其转变为标准矩阵,再对其进行取某一列的操作,参考(89条消息) python-TypeError: list indices must be integers or slices, not tuple 报错解决方案_章鱼千的博客-CSDN博客