如何删除此错误:(215:断言失败)!_图像为空()在函数“cv::imwrite”中

2024-06-12 06:45:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用spyder Python3.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

Tags: 代码imageimportread视频count错误cv2
1条回答
网友
1楼 · 发布于 2024-06-12 06:45:23

您需要调用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

相关问题 更多 >