OpenCV学习笔记2:GUI特性 -- 处理视频

1.捕获视频

cap = cv2.VideoCapture(xx)

arg1:设备的index或者视频文件名

之后就可以一帧一帧的捕获,最后不要忘记释放捕获的视频cap

2.捕获一帧

ret, frame = cap.read()

ret是返回的布尔值,是否捕获成功,frame是捕获的一个帧

有时候因为捕获未被初始化而发生错误,用cap.isOpened()来判断。如果返回False,用cap.open()来打开

3.捕获视频的属性

通过cap.get(propId)查看视频的属性。propId值为0~18,代表不同的属性。

通过cap.set(propId, value)来设定属性值

4.保存视频

cv2.VideoWriter()

arg1:保存的文件名,arg2:编码器code,arg3:每秒的帧数,arg4:每帧的size,arg5:彩色Flg(待确认)

----- sample coding -----

import cv2

cap = cv2.VideoCapture(0)

#定义codec

fourcc = cv2.VideoWriter_fourcc(*'XVID')

#创建VideoWriter对象

out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while True:

    # Capture frame-by-frame

    ret, frame = cap.read()

    #operations on the frame 

    # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # cv2.imshow('Capture Camera', gray)

    # write the flipped frame

    out.write(frame)

    cv2.imshow('Capture Camera', frame)

    # 按q键退出

    if cv2.waitKey(1) == ord('q'):

        break

#release the capture

cap.release()

out.release()

cv2.destroyAllWindows()

-- End --

猜你喜欢

转载自stef.iteye.com/blog/2406124
今日推荐