各位朋友,我ubuntu14.04安装python3.4(系统自带)和opencv3.4,我测试了倒入import cv2模块没有出错,但是运行程序时出现如下错误:
liuxin@sunshine-virtual-machine:~/work/python/opencv$ python3 test.py
select timeout
select timeout
OpenCV Error: Assertion failed (total() == 0 || data != __null) in Mat, file /home/liuxin/opencv-3.4.0/modules/core/include/opencv2/core/mat.inl.hpp, line 500
Traceback (most recent call last):
File "test.py", line 33, in <module>
CaptureVideo('capturevideo', 0)
File "test.py", line 16, in CaptureVideo
ret, frame = cap.read()
cv2.error: /home/liuxin/opencv-3.4.0/modules/core/include/opencv2/core/mat.inl.hpp:500: error: (-215) total() == 0 || data != __null in function Mat
我的程序源代码如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: LiuXin
import cv2
import numpy as np
def CaptureVideo(window_name, camera_id):
# cv2.namedWindow(window_name)
cap = cv2.VideoCapture(camera_id)
while True:
# 如果cap没有打开则将其打开
if not cap.isOpened():
cap.open(camera_id)
# 逐帧(frame-by-frame)读取视频内容
ret, frame = cap.read()
if not ret:
print('capture err.')
break
# 将当前帧转换成灰度图片
#grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 显示图片
#cv2.imshow(window_name, grey)
cv2.imshow(window_name, frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
# 释放摄像头并销毁所有窗口
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
CaptureVideo('capturevideo', 0)
感谢各位大神指教。