opencv学习笔记2:视频的读取、修改和保存
目标
1、读取video、显示和保存
2、从摄像头获取video并且显示
3、我们将使用cv2.functions() cv2.VedioWriter()
1、从摄像头捕获video
如何选择摄像头,用完别忘了释放摄像头
Often, we have to capture live stream with camera. OpenCV provides a very simple interface to this. Let’s capture a video from the camera (I am using the in-built webcam of my laptop), convert it into grayscale video and display it. Just a simple task to get started.
To capture a video, you need to create a VideoCapture object. Its argument can be either the device index or the name of a video file. Device index is just the number to specify which camera. Normally one camera will be connected (as in my case). So I simply pass 0 (or -1). You can select the second camera by passing 1 and so on. After that, you can capture frame-by-frame. But at the end, don’t forget to release the capture.
代码
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
cap.read() returns a bool (True/False). If frame is read correctly, it will be True. So you can check end of the video by checking this return value.
read error可能是因为摄像头没有初始化
Sometimes, cap may not have initialized the capture. In that case, this code shows error. You can check whether it is initialized or not by the method cap.isOpened(). If it is True, OK. Otherwise open it using cap.open().
摄像头读取的图片有0-18号特征,我们可以读取和改变这些特征,例如图片长、宽
You can also access some of the features of this video using cap.get(propId) method where propId is a number from 0 to 18. Each number denotes a property of the video (if it is applicable to that video) and full details can be seen here: Property Identifier. Some of these values can be modified using cap.set(propId, value). Value is the new value you want.
For example, I can check the frame width and height by cap.get(3) and cap.get(4). It gives me 640x480 by default. But I want to modify it to 320x240. Just use ret = cap.set(3,320) and
ret = cap.set(4,240).
2、从文件中播放视频
和从摄像头读取文件一样,只需要把函数中摄像头的编号改成视频文件即可,在播放的时候,使用cv2.waitKey()暂停播放。如果暂停的时间过短或者过长,视频会播放过快或者过慢,一般来说25帧每秒不错。
import numpy as np
import cv2
cap = cv2.VideoCapture('vttest.avi')
while(cap.isOpend()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cap.release()
cv2.destroALLWindow()
3、保存视频
我们现在回会播放视频,按帧处理视频现在我们看看怎么保存视频。对于图片的保存,我们使用cv2.imwrite,视频的保存需要一些工作。
我们需要创建一个VIdeoWriter对象,指明输出到哪个文件,例如1.avi。指明FFourCC代码(指明视频格式),每秒的帧数,图像的size,还有彩色等信息。
下面代码从摄像头获取图片,保存。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()