opencv视频读取并显示代码

matplotlib.pylib显示图像非实时,好像不能用于视频显示。比较好的是使用opencv

import numpy as np
import cv2


cap = cv2.VideoCapture(r'D:\rawData\movies\highway1raw.avi')
fps = cap.get(cv2.CAP_PROP_FPS) 
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) 
fourcc = cap.get(cv2.CAP_PROP_FOURCC) 
#fourcc = cv2.CAP_PROP_FOURCC(*'CVID') 
print('fps=%d,size=%r,fourcc=%r'%(fps,size,fourcc))

delay=1000/int(fps)
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
#        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow('frame',frame)
        cv2.waitKey(np.uint(delay))

        print('Ongoing...')        
    else:
        break
    
cap.release()
cv2.destroyAllWindows()

注意:

cv2.waitkey(delay):delay需要时int型,否则卡死

posted @ 2017-10-18 16:08  Osler  阅读(616)  评论(0编辑  收藏  举报