-
目標(biāo)
- 利用筆記本的的內(nèi)置攝像頭錄像并且保存鸵赫。
-
cv2.VideoCapture()
、cv2.VideoWriter()
躏升、read()
辩棒、write()
的使用。
-
示例代碼
python
# -*- coding:utf-8-*-
import numpy as np
import cv2
def video_capture(filePath):
cap = cv2.VideoCapture(0)
fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.cv.FOURCC(*'CVID')
out = cv2.VideoWriter(filePath, fourcc, fps, size)
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame, 0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('iframe', gray)
out.write(gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
filePath = 'D:\output.avi'
video_capture(filePath)
-
獲取視頻
-
cap = cv2.VideoCapture(0)
打開筆記本的內(nèi)置攝像頭膨疏。 -
cap = cv2.VideoCapture('D:\output.avi')
打開視頻文件
** 注意**:若初始化攝像頭或者打開視頻文件不成功一睁,opencv不會提示你。使用print cap.isOpened()
查看佃却,若返回值是True
者吁,則表明成功,否則返回值是False
饲帅。
-
讀入視頻
-
cap.read()
按幀讀取視頻复凳,它的返回值有兩個(gè):ret
,frame
瘤泪。其中ret
是布爾值,如果讀取幀是正確的則返回True育八,如果文件讀取到結(jié)尾对途,它的返回值就為False。frame
就是每一幀的圖像髓棋,是個(gè)三維矩陣实檀。
-
播放視頻
-
cv2.imshow('iframe', gray)
播放視頻,第一個(gè)參數(shù)是視頻播放窗口的名稱仲锄,第二個(gè)參數(shù)是視頻的當(dāng)前幀劲妙。 -
cv2.waitKey(25)
每一幀的播放時(shí)間,毫秒級儒喊。
-
停止捕獲視頻
本示例中有兩種停止捕獲視頻的方式:
if ret == True:
frame = cv2.flip(frame, 0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('iframe', gray)
out.write(gray)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
- 通過外部鍵盤輸入
cv2.waitKey(25) & 0xFF == ord('q'):
break
在25毫秒內(nèi)如果鍵盤輸入了“q”,則停止捕獲視頻;
- 通過
cap.read()
的返回值ret镣奋,若ret值為False,則停止捕獲視頻怀愧。這種適合讀取視頻文件時(shí)進(jìn)行判定侨颈,通過攝像頭錄像則只能通過第一種方式停捕獲視頻。
-
視頻的一些處理方式
- 灰度視頻
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
本例中是將攝像頭捕獲的視頻轉(zhuǎn)換為灰色并且保存芯义。 - 視頻旋轉(zhuǎn)
cv2.flip(frame, 0)
第一個(gè)參數(shù)表示要旋轉(zhuǎn)的視頻哈垢,第二個(gè)參數(shù)表示旋轉(zhuǎn)的方向,0表示繞x軸旋轉(zhuǎn)扛拨,大于0的數(shù)表示繞y軸旋轉(zhuǎn)耘分,小于0的負(fù)數(shù)表示繞x和y軸旋轉(zhuǎn)。
-
獲取視頻的參數(shù)信息
使用函數(shù) cap.get(propId)
來獲得視頻的一些參數(shù)信息,使用cap.set(propId, value)
設(shè)置視頻的一些參數(shù)信息绑警,propId的值從0到18分別為:
- **CV_CAP_PROP_POS_MSEC ** Current position of the video file in milliseconds or video capture timestamp.
- CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
- CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
- CV_CAP_PROP_FRAME_WIDTH 視頻每一幀的寬求泰。
- CV_CAP_PROP_FRAME_HEIGHT 視頻每一幀的高。
- CV_CAP_PROP_FPS 視頻的幀速计盒。
- CV_CAP_PROP_FOURCC 4個(gè)字符表示的視頻編碼器格式渴频。
- CV_CAP_PROP_FRAME_COUNT 視頻的幀數(shù)。
- CV_CAP_PROP_FORMAT Format of the Mat objects returned byretrieve().
- CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
- CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
- CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
- CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
- CV_CAP_PROP_HUE Hue of the image (only for cameras).
- CV_CAP_PROP_GAIN Gain of the image (only for cameras).
- CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
- CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
- CV_CAP_PROP_WHITE_BALANCE Currently not supported
-
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
注意:當(dāng)你查詢的屬性是VideoCapture()不支持的屬性時(shí)北启,返回值為0.
-
保存視頻
-
out = cv2.VideoWriter(filePath, fourcc, 800, size)
設(shè)置輸出視頻的名稱卜朗,視頻的格式,視頻的幀速咕村,視頻的大小等场钉。 -
fourcc = cv2.cv.FOURCC(*'CVID')
設(shè)置要保存視頻的格式。
-
釋放對象和銷毀窗口
cap.release()
out.release()
cv2.destroyAllWindows()