删除以下内容:错误:(-215:声明失败)函数'cv :: imwrite'中的!_img.empty()

时间:2019-11-28 17:58:53

标签: python opencv

我试图通过下面的python代码从视频中提取剪影,但是提取后,最后出现以下错误:

cv2.error:OpenCV(4.1.2)C:\ projects \ opencv-python \ opencv \ modules \ imgcodecs \ src \ loadsave.cpp:715:错误:(-215:断言失败)!_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()

1 个答案:

答案 0 :(得分:-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()