python - 如何消除此错误:(-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
问题描述
我正在尝试在 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
解决方案
您需要调用该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
推荐阅读
- javascript - jQuery 无法在 Safari 浏览器和 MAC 中的 chorome 上运行
- c++ - 在编译时进行字符串实习以进行分析
- php - 我在我的 sql 查询中传递了 php date 函数来查找结果,但我的 date 函数不起作用
- marklogic - 调试 MarkLogic 搜索查询
- java - 如何从两个不同对象列表中获取不常见的对象
- c# - 设置单击键盘上的“下一步”时要关注的 EditText(不同的回收器视图)
- reactjs - 在带有 React 和 Typescript 的 div 上使用 onFocus
- java - 如何在 Android 中每 60 秒运行一次代码
- pdf - itext 7 如何合并Pdfs?
- python-3.x - Scikit学习LinearSVC