Python+OpenCV 图像处理系列(2)—— 视频捕获、播放和保存
1.視頻捕獲
為了獲取視頻,首先需要創建一個 VideoCapture 類對象。它的參數可以是設備的索引號,或者是一個視頻文件。設備索引號就是在指定要使用的攝像頭。一般的筆記本電腦都有內置攝像頭。所以參數就是 0。你可以通過設置成 1 或者其他的來選擇別的攝像頭。
import cv2
cap = cv2.VideoCapture()
flag = cap.isOpened()if flag:while 1:# Capture frame-by-frameret, frame = cap.read()gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# Display the resulting framecv2.imshow('frame', gray)if cv2.waitKey(1) & 0xFF == ord('q'):break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
cap.read() 返回一個布爾值(True/False)。如果幀讀取的是正確的,就是True。
有時 cap 可能不能成功的初始化攝像頭設備。這種情況下上面的代碼會報錯。你可以使用cap.isOpened(),來檢查是否成功初始化了。如果返回值是 True,那就沒有問題。否則就要使用函數 cap.open()。
可以使用函數 cap.get(propId) 來獲得視頻的一些參數信息。這里 propId 可以是 0 到 18 之間的任何整數。每一個數代表視頻的一個屬性,見下表。
| 屬性 | 說明 |
|---|---|
| CV_CAP_PROP_POS_MSEC | Current position of the video file in milliseconds. |
| 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 | Width of the frames in the video stream. |
| CV_CAP_PROP_FRAME_HEIGHT | Height of the frames in the video stream. |
| CV_CAP_PROP_FPS | Frame rate. |
| CV_CAP_PROP_FOURCC | 4-character code of codec. |
| CV_CAP_PROP_FRAME_COUNT | Number of frames in the video file. |
| CV_CAP_PROP_FORMAT | Format of the Mat objects returned by retrieve() . |
| 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 unsupported |
| CV_CAP_PROP_RECTIFICATION | Rectification flag for stereo (note: only supported by DC1394 v 2.x backend currently) |
其中的一些值可以使用 cap.set(propId, value) 來修改,value 就是你想要設置成的新值。
例如,我可以使用 cap.get(3) 和 cap.get(4) 來查看每一幀的寬和高。默認情況下得到的值是 640X480。但是我可以使用 ret=cap.set(3,320) 和 ret=cap.set(4,240) 來把寬和高改成 320X240。
2.視頻播放
與從攝像頭中捕獲一樣,你只需要把設備索引號改成視頻文件的名字。在播放每一幀時,使用 cv2.waiKey() 設置適當的持續時間。如果設置的太低視頻就會播放的非常快,如果設置的太高就會播放的很慢(你可以使用這種方法控制視頻的播放速度)。通常情況下 25 毫秒就可以了。
import cv2cap = cv2.VideoCapture("2018-12-16_11-33.mp4")
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc('XVID') # cv3 的寫法,cv2 需要修改
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 frameout.write(frame)cv2.imshow('frame', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakelse:break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
如果安裝的是 OpenCV 2 的版本會出現如下錯誤:
AttributeError: 'module' object has no attribute 'VideoWriter_fourcc'
查看 OpenCV 的版本號
>>> import cv2
>>> cv2.__version__
'2.4.9.1'
cv2.VideoWriter_fourcc() 是 Opencv3.0 所用,而 cv2.cv.CV_FOURCC() 用于 Opencv2.4.*
修改該行為
fourcc = cv2.cv.CV_FOURCC('M', 'J', 'P', 'G')
3.視頻保存
要創建一個 VideoWriter 的對象。我們應該確定一個輸出文件的名字。接下來指定 FourCC 編碼(下面會介紹)。播放頻率和幀的大小也都需要確定。最后一個是 isColor 標簽。如果是 True,每一幀就是彩色圖,否則就是灰度圖。
FourCC 就是一個4 字節碼,用來確定視頻的編碼格式。
? In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
? In Windows: DIVX (More to be tested and added)
? In OSX : (I don’t have access to OSX. Can some one fill this?)
FourCC 碼以下面的格式傳給程序,以 MJPG 為例:
cv2.cv.FOURCC('M','J','P','G') 或者 cv2.cv.FOURCC(*'MJPG')
下面的代碼是從攝像頭中捕獲視頻,沿水平方向旋轉每一幀并保存它。
import cv2cap = cv2.VideoCapture("2018-12-16_11-33.mp4")
# Define the codec and create VideoWriter object# 此處fourcc的在 ubuntu 16.04 上有效,如果視頻保存為空,那么可以改一下這個參數試試, 也可以是-1
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')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)))out = cv2.VideoWriter('output.avi', fourcc, fps, size)while cap.isOpened():ret, frame = cap.read()if ret == True:# frame = cv2.flip(frame, 0) 該行表示翻轉視頻,播放已經存在的視頻時注釋改行# write the flipped frameout.write(frame)cv2.imshow('frame', frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakelse:break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
編碼器常用的幾種:
cv2.VideoWriter_fourcc(“I”, “4”, “2”, “0”)
壓縮的yuv顏色編碼器,4:2:0色彩度子采樣 兼容性好,產生很大的視頻 avi
cv2.VideoWriter_fourcc(“P”, I", “M”, “1”)
采用mpeg-1編碼,文件為avi
cv2.VideoWriter_fourcc(“X”, “V”, “T”, “D”)
采用mpeg-4編碼,得到視頻大小平均 拓展名avi
cv2.VideoWriter_fourcc(“T”, “H”, “E”, “O”)
Ogg Vorbis, 拓展名為ogv
cv2.VideoWriter_fourcc(“F”, “L”, “V”, “1”)
FLASH視頻,拓展名為.flv
4.總結
以下代碼在 OpenCV 3.4.4 中測試通過,
import cv2
import numpy as npvideo_path = "video/test.mp4"
cap = cv2.VideoCapture(video_path)height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = cap.get(cv2.CAP_PROP_FPS)
print "height is {}, width is {}, count is {}, fps is {}".format(height, width, count, fps)
# height is 720.0, width is 1280.0, count is 3599.0, fps is 11.9941808935
out_video = cv2.VideoWriter("video/out_video.mp4", cv2.VideoWriter_fourcc('D', 'I', 'V', 'X'),15, (np.int(width), np.int(height)), True)while True:ret, frame = cap.read()print "ret is {}, frame is {}, frame's shape is {}".format(ret, frame, frame.shape)if ret is True:cv2.imshow("video_out", frame)out_video.write(frame)c = cv2.waitKey(50)if c & 0xFF == 27:breakelse:breakcap.release()
out_video.release()
部分輸出結果:
ret is True, frame is [[[209 190 180][209 190 180][209 190 180]...[ 60 39 47][ 57 39 49][ 62 44 54]][[209 190 180][209 190 180][209 190 180]...[ 60 39 47][ 61 43 53][ 57 39 49]][[209 190 180][209 190 180][209 190 180]...[ 74 43 56][ 74 45 52][ 66 37 44]]...[[108 102 119][108 102 119][107 101 118]...[163 158 147][163 158 147][163 158 147]][[108 102 119][108 102 119][107 101 118]...[168 157 146][168 157 146][168 157 146]][[108 102 119][108 102 119][107 101 118]...[161 150 139][161 150 139][161 150 139]]], frame's shape is (720, 1280, 3)
總結
以上是生活随笔為你收集整理的Python+OpenCV 图像处理系列(2)—— 视频捕获、播放和保存的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python+OpenCV 图像处理系列
- 下一篇: Python+OpenCV 图像处理系列