TypeError: tuple indices must be integers or slices, not tuple when cropping OpenCV image taken from VideoCapture

TypeError: tuple indices must be integers or slices, not tuple when cropping OpenCV image taken from VideoCapture

我正在尝试使用 OpenCV 使用以下代码在 Raspberry Pi 3B+ 运行 Raspbian GNU/Linux 11(靶心)和我得到这个输出:

Traceback (most recent call last):
File "/home/pi/Desktop/test-code-elias/camquali.py", line 141, in <module>
right_wall = img[1:1944, 1:1000]
TypeError: tuple indices must be integers or slices, not tuple

我的代码:

img = cam.read()
right_wall = img[0:1944, 0:1000]

img 是一个元组。显然 cam.read() returns 是一个元组,而不是您所期望的。元组中的元素之一可能是您要查找的图像数组,因此您必须先提取它。元组是 one-dimensional,您正在为二维提供索引,这就是错误消息的来源。

如果正如 https://whosebug.com/users/16487900/berak 所补充的那样,输出是 (success, img),您需要像这样更改代码:

success, img = cam.read()
assert success
right_wall = img[0:1944, 0:1000]