我试过这段代码
import cv2
img = cv2.imread("image.jpg")
crop_img = img[100:200, 200:400] # Crop from x, y, w, h -> 100, 200, 100, 200
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
它可以工作几次......但是突然间出现了这个错误:
Traceback (most recent call last):
File "test5.py", line 7, in <module>
crop_img = img[100:200, 200:400] # Crop from x, y, w, h -> 100, 200, 100, 200``
TypeError: 'NoneType' object has no attribute '__getitem__'
任何人都可以帮助我..
答案 0 :(得分:1)
如果cv2.imread
找不到文件名i t returns None
。
如果img
为无,那么img[100:200, 200:400]
将会提升
TypeError: 'NoneType' object has no attribute '__getitem__'