【问题标题】:remove following: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'删除以下:错误:(-215:断言失败)!_img.empty()在函数'cv :: imwrite'中
【发布时间】:2019-11-28 17:58:53
【问题描述】:

我正在尝试通过以下 python 代码从视频中提取剪影,但提取后,最后出现以下错误:

cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty( ) 在函数'cv::imwrite'中

如何消除此错误。

Python 代码是:

import numpy as np
import cv2
import os
cap = cv2.VideoCapture('E:\\DCIM_with_two_cycle\\In_Lab\\021\\MVI_0098.MP4')
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
count = 0;
while 1:
    count = count + 1
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    # cv2.imshow('frame', fgmask)
    if count < 10:
        cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image000' + str(count) + '.jpg', fgmask)
    elif count < 100:
        cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image00' + str(count) + '.jpg', fgmask)
    elif count < 1000:
        cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image0' + str(count) + '.jpg', fgmask)
    elif count < 10000:
        cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image' + str(count) + '.jpg', fgmask)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
#cv2.destroyAllWindows()

【问题讨论】:

  • 你做了哪些研究?毫无疑问,您在 Stack Overflow 上发现了几个带有这个确切错误的问题。这些问题的答案对您没有帮助吗?

标签: python opencv


【解决方案1】:

我通过以下代码解决了这个问题:

import numpy as np
import cv2
import os

cap = cv2.VideoCapture('E:\\DCIM_with_two_cycle\\In_Lab\\021\\MVI_0098.MP4')
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
count = 0;
while 1:
    count = count + 1
    print(count)
    ret, frame = cap.read()
    fgmask = fgbg.apply(frame)
    # cv2.imshow('frame', fgmask)
    print((fgmask is None) == True)
    if (fgmask is None) == False:
        if count < 10:
            cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image000' + str(count) + '.jpg', fgmask)
        elif count < 100:
            cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image00' + str(count) + '.jpg', fgmask)
        elif count < 1000:
            cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image0' + str(count) + '.jpg', fgmask)
        elif count < 10000:
            cv2.imwrite('E:\\DCIM_with_two_cycle\\In_LabSilhoutte\\image' + str(count) + '.jpg', fgmask)
    else:
        break
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
cap.release()
# cv2.destroyAllWindows()

【讨论】:

  • 解释问题是什么以及您采取了哪些措施来解决它。没有解释的“代码转储”对其他人没有帮助。
  • 对您的解决方案的简短说明将有助于其他人更好地理解问题
猜你喜欢
  • 2020-04-22
  • 2021-04-14
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 2019-05-18
  • 2021-10-29
  • 2021-02-23
  • 2021-11-18
相关资源
最近更新 更多