【问题标题】:Error: !_img.empty() in function 'imwrite'错误:函数“imwrite”中的 !_img.empty()
【发布时间】:2021-02-15 06:28:18
【问题描述】:

我想从名为project.avi 的视频创建帧并将它们保存到frameIn 文件夹。但是某些类型的错误并没有让我完成。我怎么解决这个问题。代码如下:

cap = cv2.VideoCapture('project.avi')

currentFrame = 0

while(True):

ret, frame = cap.read()

name = 'frameIn/frame' + str(currentFrame) + '.jpg'
print ("Creating file... " + name)
cv2.imwrite(name, frame)

frames.append(name)

currentFrame += 1

cap.release()
cv2.destroyAllWindows()

错误是:

Traceback (most recent call last):
  File "videoreader.py", line 28, in <module>
    cv2.imwrite(name, frame)
cv2.error: OpenCV(4.4.0) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-2rx9f0ng/opencv/modules/imgcodecs/src/loadsave.cpp:738: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

【问题讨论】:

    标签: python python-3.x cv2 opencv-python


    【解决方案1】:

    原因可能是图片是空的,所以, 您应该在阅读帧之前检查天气视频是否正确打开:cap.isOpened()。 然后,在执行ret, frame = cap.read()后检查ret变量值是否为真,以确保正确抓取帧。

    要清除的代码:

    cap = cv2.VideoCapture('project.avi')
    if cap.isOpened():
        current_frame = 0
        while True:
            ret, frame = cap.read()
            if ret:
                name = f'frameIn/frame{current_frame}.jpg'
                print(f"Creating file... {name}")
                cv2.imwrite(name, frame)
                frames.append(name)
            current_frame += 1
        cap.release()
    cv2.destroyAllWindows()
    

    希望对你有帮助。

    【讨论】:

    • 非常感谢感谢感谢感谢。我的天啊!这个问题我找了一个月!
    • 谢谢,我的荣幸。但是您能否将此答案标记为解决方案?
    • 哦,对不起!我忘了。
    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 2021-04-14
    • 2020-04-22
    • 2020-06-07
    • 2020-12-13
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    相关资源
    最近更新 更多