一、读取写入视频文件- 1 import cv2
- 2
- 3 # 创建一个视屏捕获对象
- 4 videoCapture = cv2.VideoCapture('AVI.avi')
- 5
- 6 # 获取视频的属性值,cv2.CAP_PROP_FPS获取视频帧率
- 7 fps = videoCapture.get(cv2.CAP_PROP_FPS)
- 8
- 9 # cv2.CAP_PROP_FRAME_WIDTH/HEIGHT 返回float类型 获取视频帧的宽高
- 10 size = int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), \
- 11 int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
- 12
- 13 '''
- 14 创建一个写入对象,将帧写入输出的视频
- 15 cv2.VideoWriter_fourcc()函数指定编码器为 I420
- 16 fps 和 size 指定输出的帧率和尺寸
- 17 '''
- 18 videoWrite = cv2.VideoWriter('Out.avi',
- 19 cv2.VideoWriter_fourcc('I', '4', '2', '0'),
- 20 fps, size
- 21 )
- 22
- 23 '''
- 24 对捕获到的视频对象进行读取帧,success表示是否成功读取一帧,frame表示当前帧。
- 25 循环读取写入输出视频。
- 26 '''
- 27 success, frame = videoCapture.read()
- 28 while success:
- 29 videoWrite.write(frame)
- 30 success, frame = videoCapture.read()
复制代码
二、捕获摄像头帧- 1 import cv2
- 2
- 3 cameraCapture = cv2.VideoCapture(0)
- 4
- 5 fps = 30
- 6
- 7 size = int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), \
- 8 int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))
- 9
- 10 videoWriter = cv2.VideoWriter(
- 11 'OutVideo_GRAB.avi',
- 12 cv2.VideoWriter_fourcc('I', '4', '2', '0'),
- 13 fps,
- 14 size
- 15 )
- 16
- 17 success, frame = cameraCapture.read()
- 18
- 19 numFramesRemaining = 10 * fps
- 20 while success and numFramesRemaining > 0:
- 21 videoWriter.write(frame)
- 22 success, frame = cameraCapture.read()
- 23 numFramesRemaining -= 1
复制代码 和视频的读取写入没有什么差异,都是需要先创建一个VideoCapture Object来操作,下述是细微差别:
5 Line:需要手动设置fps的值
19 Line:需要设定一个时间,numFramesRemaining代表持续捕获300个帧,而每30个帧为一秒,所以将生成一个10秒钟的视频文件
三、窗口显示图片- 1 import cv2
- 2
- 3 img = cv2.imread('CopyPic.png')
- 4 cv2.imshow('', img)
- 5 cv2.waitKey()
- 6 cv2.destroyAllWindows()
复制代码
四、窗口显示摄像头
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |