如何清除此错误:(-215:断言失败)!_img.empty()在函数'cv :: imwrite'

时间:2019-12-26 05:29:12

标签: python opencv

我正在尝试在spyder python 3.7.1中使用opencv 4.1.2从视频中提取帧,但出现此错误。

代码:

import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture('‪G:/TSR/TSR_vedios/blur/01_01_01_02_01.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
  cv2.imwrite("G:/TSR/TSR_vedios/tlevels/blurness/frame%d.jpg" % count, image)    
  success,image = vidcap.read()
  print ('Read a new frame: ', success)
  count += 1

1 个答案:

答案 0 :(得分:0)

您需要调用open函数,并确保图像是有效的,即宽度和高度> 0。

像这样修改您的代码:

import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture()
success = vidcap.open('‪G:/TSR/TSR_vedios/blur/01_01_01_02_01.mp4')
if not success:
    print("Failed to open the video")

count = 0
while success:
    success,image = vidcap.read()
    height, width, channels = img.shape
    if (height > 0) and (width > 0):
        cv2.imwrite("G:/TSR/TSR_vedios/tlevels/blurness/frame%d.jpg" % count, image)    
        print('Read a new frame: ', success)
    else:
        print("Empty image")
    count += 1