国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

python利用opencv保存、播放視頻

瀏覽:24日期:2022-07-06 16:00:33

代碼已上傳至:https://gitee.com/tqbx/python-opencv/tree/master/Getting_started_videos

目標(biāo)

學(xué)習(xí)讀取視頻,播放視頻,保存視頻。學(xué)習(xí)從相機(jī)中捕捉幀并展示。學(xué)習(xí)cv2.VideoCapture(),cv2.VideoWriter()的使用

從相機(jī)中捕捉視頻

通過自帶攝像頭捕捉視頻,并將其轉(zhuǎn)化為灰度視頻顯示出來。

基本步驟如下:

1.首先創(chuàng)建一個VideoCapture對象,它的參數(shù)包含兩種:

設(shè)備索引,指定攝像機(jī)的編號。 視頻文件的名稱。

2.逐幀捕捉。

3.釋放捕捉物。

import numpy as npimport cv2 as cvcap = cv.VideoCapture(0)if not cap.isOpened(): print('Cannot open camera') exit()while True: # Capture frame-by-frame ret, frame = cap.read() # if frame is read correctly ret is True if not ret: print('Can’t receive frame (stream end?). Exiting ...') break # Our operations on the frame come here gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # Display the resulting frame cv.imshow(’frame’, gray) if cv.waitKey(1) == ord(’q’): break# When everything done, release the capturecap.release()cv.destroyAllWindows()

其他:

cap.read()返回布爾值,如果frame讀取正確,為True,可以通過這個值判斷視頻是否已經(jīng)結(jié)束。 有時,cap可能會初始化捕獲失敗,可以通過cap.isOpened()來檢查其是否被初始化,如果為True那是最好,如果不是,可以使用cap.open()來嘗試打開它。 當(dāng)然,你可以使用cap.get(propId)的方式獲取視頻的一些屬性,如幀的寬度,幀的高度,幀速等。propId是0-18的數(shù)字,每個數(shù)字代表一個屬性,對應(yīng)關(guān)系見底部附錄。 既然可以獲取,當(dāng)然也可以嘗試設(shè)置,假設(shè)想要設(shè)置幀的寬度和高度為320和240:cap.set(3,320), cap.set(4,240)。

從文件中播放視頻

代碼和從相機(jī)中捕獲視頻基本相同,不同之處在于傳入VideoCapture的參數(shù),此時傳入視頻文件的名稱。

在顯示每一幀的時候,可以使用cv2.waitKey()設(shè)置適當(dāng)?shù)臅r間,如果值很小,視頻將會很快。正常情況下,25ms就ok。

import numpy as npimport cv2cap = cv2.VideoCapture(’vtest.avi’)while(cap.isOpened()): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow(’frame’,gray) if cv2.waitKey(1) & 0xFF == ord(’q’): breakcap.release()cv2.destroyAllWindows()

保存視頻

1.創(chuàng)建一個VideoWriter 對象,指定如下參數(shù):

輸出的文件名,如output.avi。 FourCC code。 每秒的幀數(shù)fps。 幀的size。

2.FourCC code傳遞有兩種方式:

fourcc = cv2.VideoWriter_fourcc(*’XVID’) fourcc = cv2.VideoWriter_fourcc(’X’,’V’,’I’,’D’)

3.FourCC是一個用于指定視頻編解碼器的4字節(jié)代碼。

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?)

import numpy as npimport cv2cap = cv2.VideoCapture(0)# Define the codec and create VideoWriter objectfourcc = 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 finishedcap.release()out.release()cv2.destroyAllWindows()

附錄

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 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_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently) CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)

參考閱讀

Getting Started with Videos

作者:天喬巴夏丶出處:https://www.cnblogs.com/summerday152/本文已收錄至Gitee:https://gitee.com/tqbx/JavaBlog若有興趣,可以來參觀本人的個人小站:https://www.hyhwky.com

以上就是python利用opencv保存、播放視頻的詳細(xì)內(nèi)容,更多關(guān)于python opencv的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: www.成人网 | 波多野结衣视频免费在线观看 | 亚洲免费区| 久久精品国产免费看久久精品 | 久久经典免费视频 | 国产性较精品视频免费 | 538在线视频二三区视视频 | 欧美aaa毛片免费看 欧美aaa视频 | 男人的天堂久久精品激情 | 成人免费小视频 | 精品视频h| 在线中文字幕精品第5页 | 黄色三级视频 | 欧美一级久久久久久久大 | 亚洲黄区 | 日本韩经典三级在线播放 | 亚洲最新在线视频 | 久久国内精品视频 | 国产成人一区二区三区免费观看 | 三级黄色网址 | 亚洲天码中文字幕第一页 | 久色网址| 成人午夜毛片在线看 | 香蕉一区二区三区观 | 国产美女白丝袜精品_a不卡 | 免费国产a国产片高清不卡 免费国产不卡午夜福在线 免费国产不卡午夜福在线观看 | 荡公乱妇蒂芙尼中文字幕 | 国产日韩线路一线路二 | 国产在线精品一区二区 | 国产成人亚洲精品91专区高清 | 纯欧美一级毛片免费 | 国产精品不卡无毒在线观看 | 精品一区二区三区四区在线 | 亚洲精品国产一区二区在线 | 日韩有码第一页 | 日本苍井一级毛片 | 免费在线看黄网址 | 中国国产一国产一级毛片视频 | 国产系列在线 | 亚洲九九色 | 手机看片国产免费 |