python + opencv 学习系列 2 : Getting Started with Videos
来源:互联网 发布:sql删除重复字段数据 编辑:程序博客网 时间:2024/06/06 00:19
本节主要是学习:读取视频,播放视频,保存视频从摄像头中读取视频,并保存视频将会使用两个主要视频处理函数: cv2.VideoCapture(),cv2.VideoWriter()参考文献: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video http://www.2cto.com/kf/201312/261521.html
下面直接给出注释的比较全的代码:
def CaptureVideoFromCamera(): ''' 从摄像头中读取视频帧 并 显示 ''' import numpy as np import cv2 #获取视频对象,参数是表示读取哪个摄像头的视频帧,如果只有一个那么参数就是0或者-1,有几个的话就以此类推啦!! cap = cv2.VideoCapture(0 ) while(True ): #逐帧获取视频 ret, frame = cap.read() #ret表示是否读取正确(正确True 错误False),frame存储视频帧 if ret == False: print "read capture error!" break #将获取到的视频帧转换为灰度模式 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY ) #播放处理后的结果 如果不想播放为灰度模式,则注销上一句并将gray改为frame即可 cv2.imshow("frame from video", gray ) #保持时间以及退出方式 if cv2.waitKey(55) & 0xFF == ord("q"): #55毫秒是正常播放速度 break #退出,则释放摄像头,并销毁所有窗户 cap.release() cv2.destroyAllWindows()def PlayingVideoFromFile(): ''' 从文件中获取视频帧 并 进行播放 ''' import numpy as np import cv2 #上面一个函数已经对所有语句注释过了,下面不进行重复注释了。 cap = cv2.VideoCapture("007.flv") while(cap.isOpened() ): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY ) cv2.imshow("frame", gray ) if cv2.waitKey(55) & 0xFF == ord("q"): break cap.release() cv2.destroyAllWindows()def SavingVideo(): ''' 从文件中读取一个视频并保存 ''' import numpy as np import cv2 #上面一个函数已经对所有语句注释过了,下面不进行重复注释了。 cap = cv2.VideoCapture("007.flv") #获得码率及尺寸 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.CV_FOURCC('M','J','P','G') out = cv2.VideoWriter('007_output.avi',fourcc, fps, size ) ret = True while(cap.isOpened() & ret ): ret, frame = cap.read() if ret == True: #进行图像翻转的函数 #frame = cv2.flip(frame, 0 ) #写入帧对象内 out.write(frame ) cv2.imshow("frame", frame ) if cv2.waitKey(55) & 0xFF == ord("q"): break else: break #写入任务结束啦,现在所有的东西都要释放掉。 cap.release() out.release() cv2.destroyAllWindows()def mainPy(): ''' python入口函数 ''' #CaptureVideoFromCamera() #PlayingVideoFromFile() SavingVideo()if __name__ == "__main__": mainPy( )
欢迎交流!!!
0 0
- python + opencv 学习系列 2 : Getting Started with Videos
- python + opencv 学习系列 1 : Getting Started with Images
- Getting started with OpenCV
- 学习Getting Started With TensorFlow
- python channels笔记--Getting started with channels
- Getting Started with Pipeline 学习Pipeline
- Tutorials:Getting Started with jQuery---2
- Getting Started with Sencha Touch 2
- Getting Started with D3 [Part 2]
- windbg - Getting Started with WinDBG - Part 2
- Getting Started with Smartphone
- Getting Started With JasperReports
- Getting Started with DWR
- Getting Started With JasperReports
- Getting Started with MASM
- Getting started with BlazeDS
- Getting Started with RMAN
- Getting started with LAM
- 40个Java集合面试问题和答案
- C#集合类(数据结构)
- UVALive 4853 Emoogle Balance
- 移动设备上音频文件不能播放怎么办?
- 2014广州站 I - Little Zu Chongzhi's Triangles(贪心)
- python + opencv 学习系列 2 : Getting Started with Videos
- 第8章 应用协议
- hdu 2795 Billboard(线段树 单点更新)
- 我所具备的
- 总结
- HDU 3829 Cat VS Dog
- Gym 100685 F Flood
- poj-3083 Children of the Candy Corn
- AVAudioSession