cv2 python上的Mac照片亭,为什么我的代码会导致错误



我希望代码使用cv2以1秒的间隔拍摄4张图片,然后使用hconcat函数将其相加。当我尝试运行这个代码时,我得到了('numy.ndarray'对象没有属性'release'(,有人能帮助吗

import cv2,random
num = random.randint(0,2000)
cam = cv2.VideoCapture(0)
cv2.namedWindow("Mac")
def concat_tile(im_list_2d):
return cv2.vconcat([cv2.hconcat(im_list_h) for im_list_h in im_list_2d])
x = []
for i in range(4):
ret,frame = cam.read()
x.append(frame)
frame.release()
cv2.destroyAllWindows()
im_v = concat_tile([[x[0],x[1]],
[x[2],x[3]]])
img_name = "opencv_frame_{}.png".format(num)
cv2.imwrite(img_name,im_v)

不应该是frame.release(),而应该尝试cam.release()

此外,release()cv2.destroyAllWindows()应该写在代码的末尾,而不是在for循环中

for i in range(4):
ret,frame = cam.read()
x.append(frame)
cam.release()
cv2.destroyAllWindows()

最新更新