Python-OpenCV 处理视频(一): 输入输出
转载自:http://segmentfault.com/a/1190000003804797
视频的处理和图片的处理类似,只不过视频处理需要连续处理一系列图片。
一般有两种视频源,一种是直接从硬盘加载视频,另一种是获取摄像头视频。
0x00. 本地读取视频
核心函数:
cv.CaptureFromFile()
代码示例:
import cv2.cv as cvcapture = cv.CaptureFromFile('myvideo.avi')nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))#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 streamfps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)wait = int(1/fps * 1000/1)duration = (nbFrames * fps) / 1000print 'Num. Frames = ', nbFrames
print 'Frame Rate = ', fps, 'fps'
print 'Duration = ', duration, 'sec'for f in xrange( nbFrames ):frameImg = cv.QueryFrame(capture)print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES)cv.ShowImage("The Video", frameImg)cv.WaitKey(wait)
cv2
import numpy as np
import 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()
0x01. 摄像头视频读取
核心函数:
cv.CaptureFromCAM()
示例代码:
import cv2.cv as cvcapture = cv.CaptureFromCAM(0)while True:frame = cv.QueryFrame(capture)cv.ShowImage("Webcam", frame)c = cv.WaitKey(1)if c == 27: #Esc on Windowsbreak
cv2
import numpy as np
import cv2cap = cv2.VideoCapture(0)while(True):# Capture frame-by-frameret, frame = cap.read()# Our operations on the frame come heregray = 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()
0x02. 写入视频
摄像头录制视频
import cv2.cv as cvcapture=cv.CaptureFromCAM(0)
temp=cv.QueryFrame(capture)
writer=cv.CreateVideoWriter("output.avi", cv.CV_FOURCC("D", "I", "B", " "), 5, cv.GetSize(temp), 1)
#On linux I used to take "M","J","P","G" as fourcccount=0
while count<50:print countimage=cv.QueryFrame(capture)cv.WriteFrame(writer, image)cv.ShowImage('Image_Window',image)cv.WaitKey(1)count+=1
从文件中读取视频并保存
import cv2.cv as cv
capture = cv.CaptureFromFile('img/mic.avi')nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))
width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
codec = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)wait = int(1/fps * 1000/1) #Compute the time to wait between each frame queryduration = (nbFrames * fps) / 1000 #Compute durationprint 'Num. Frames = ', nbFrames
print 'Frame Rate = ', fps, 'fps'writer=cv.CreateVideoWriter("img/new.avi", int(codec), int(fps), (width,height), 1) #Create writer with same parameterscv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES,80) #Set the number of framesfor f in xrange( nbFrames - 80 ): #Just recorded the 80 first frames of the videoframe = cv.QueryFrame(capture)print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES)cv.WriteFrame(writer, frame)cv.WaitKey(wait)
cv2
import numpy as np
import cv2cap = 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 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()
Python-OpenCV 处理视频(一): 输入输出相关推荐
- 解决Python OpenCV 读取视频并抽帧出现error while decoding的问题
解决Python OpenCV 读取视频抽帧出现error while decoding的问题 1. 问题 2. 解决 3. 源代码 参考 1. 问题 读取H264视频,抽帧视频并保存,报错如下: [ ...
- 使用Python,OpenCV在视频中进行实时条形码检测
使用Python,OpenCV在视频中进行实时条形码检测 1. 步骤 2. 适用场景及优化 3. 总结 4. 源码 参考 上一篇博客介绍了如何检测和查找图像中的条形码.这篇博客将进行一些优化以检测实时 ...
- python图片转视频加特效_使用Python opencv实现视频与图片的相互转换
因为最近要经常转换数据集进行实验,因此记录一下. 1.视频转图片 即为将视频解析为一帧一帧的图片: import cv2 vc=cv2.VideoCapture("/home/hqd/Pyc ...
- python opencv 图片/视频 拼接
python opencv 图片/视频 拼接 # coding: utf-8 # 像写诗一样写代码 import numpy as np import cv2img_A_path = "C: ...
- python opencv写视频——cv2.VideoWriter()
python opencv写视频--cv2.VideoWriter() 函数原型 cv2.VideoWriter() VideoWriter(filename, fourcc, fps, frameS ...
- python opencv PIL 视频分割成图片 图片合成为视频 修改图片大小(抗锯齿)
Python代码将原有的视频分割成图片,我的例子是一帧一帧的分割 用python+opencv完成视频的分割 import cv2 #导入opencv模块 print(2) #测试是否运行 vc=cv ...
- python opencv读取视频没声音_python + opencv: 解决不能读取视频的问题
博主一开始使用python2.7和Opencv2.4.10来获取摄像头图像,程序如下: cap = cv2.VideoCapture(0) ret, frame = cap.read() 使用这个程序 ...
- python+opencv横向拼接视频
如果想利用python+opencv把两段视频拼接在一起,可以有两种想法: 1. 第二个视频直接接在第一个视频后边,延长帧.这种我称为"纵向拼接": 2. 第一个视频和第二个视频每 ...
- python opencv 录制视频_如何使用OpenCV、Python和深度学习在图像和视频中实现面部识别?...
Face ID 的兴起带动了一波面部识别技术热潮.本文将介绍如何使用 OpenCV.Python 和深度学习在图像和视频中实现面部识别,以基于深度识别的面部嵌入,实时执行且达到高准确度. 以下内容由 ...
- 用Python Opencv实现视频快进
怎么用Opencv实现视频快进?小白可能会增大ftp,减小等待时间,但是这种方式大大增加了程序计算量,而且快进有上限,一般电脑全力计算也只不过能快进两三倍而已.要想实现大步幅快进就要用到跳帧:比如读取 ...
最新文章
- HTML做3D立体特效,html5 3D立体粒子波浪动画特效代码
- json2.js的初步学习与了解(转)
- php导出excel2007实例,PHPExcel精简版与导入导出案例
- twitter storm源码走读(二)
- socket 编程篇六之IPO多路复用-select poll epoll
- Caused by: org.springframework.boot.web.server.PortInUseException: Port 8081 is already in use 端口占用
- BGP基本配置(GNS3)
- Java关键字、标识符及基本数据类型
- 我去头条面试,面试官问我如何设计好API,看看我是如何吊打面试官的!
- java负数用什么类型定义_(转) Java中的负数及基本类型的转型详解
- Codeforces Round #253 DIV1 C 馋
- springboot集成webservice接口
- Android接口调用
- 字符串:1.给定一个字符串s,分割s使得s的每一个子串都是回文串
- 什么是软文?什么是软文广告?
- 湖南独立学院计算机排名2015,2015年湖南独立学院高校名单
- 老板想留住的人,都具备这些品质
- 如何高效学习_斯科特扬(读后,整体学习法试用,总结)
- 战队口号霸气押韵8字_霸气押韵的团队口号集锦
- 第9章 更复杂的光照