十五天掌握OpenCV——GUI特性(视频部分)

捕获视频

  1. cap=cv2.VideoCapture(0) ——解释:获取视频。参数为设备索引号或者视频文件,内置摄像头取0。(按帧捕获视频)——实例化
  2. cap.read() ——解释:返回布尔值,如果帧数读取正确返回True。
  3. cap.isOpened() ——解释:检查对象是否成功初始化摄像头设备。
  4. cap.open() ——解释:初始化摄像头设备。
  5. cap.get(propId) ——解释:获得视频的一些参数信息,参数取值范围:0-18。
    5.1 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
    5.2 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
    5.3 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
    5.4 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
    5.5 CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
    5.6 CV_CAP_PROP_FPS Frame rate.
    5.7 CV_CAP_PROP_FOURCC 4-character code of codec.
    5.8 CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
    5.9 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
    5.10 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
    5.11 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
    5.12 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
    5.13 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
    5.14 CV_CAP_PROP_HUE Hue of the image (only for cameras).
    5.15 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
    5.16 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
    5.17 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
    5.18 CV_CAP_PROP_WHITE_BALANCE Currently unsupported
    5.19 CV_CAP_PROP_RECTIFICATION Rectificationflagforstereo
  6. cap.set(propId,value) ——解释:对propid属性修改成相应的value值。

捕获:代码演示

# coding=utf-8

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
	ret,frame=cap.read()
	gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
	cv2.imshow('frame',gray)
	if cv2.waitKey(1) &0xFF = ord('q'):
		break
cap.release()
cv2.destroyAllWindows()

播放视频——cv2.imshow()

  1. cv2.imshow('窗口名’,帧名) ——解释:显示保存的帧。

播放:代码演示

# coding=utf-8

import numpy as np
import cv2

cap=cv2.VideoCapture(0)
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)
		out.write(frame)
		cv2.imshow('frame',frame)
		if waitKey(1) &0xFF == ord('q'):
			break
	else:
		break
cap.release()
out.release()
cv2.destroyAllWindows()

保存视频

  1. 创建VideoWriter的对象。
  2. 确定输出文件的名字。
  3. 指定FourCC编码。
  4. 确定播放帧率和帧大小。
  5. 确定isColor标签。True:彩色图,否则灰度图。
  6. FourCC 就是一个 4 字节码用来确定的编码格式。

6.1 In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
6.2 In Windows: DIVX (More to be tested and added)
6.3 In OSX : (I dont have access to OSX. Can some one fill this?)

  1. FourCC 码以下的格式传给程序以 MJPG 为例 ——cv2.cv.FOURCC(‘M’,‘J’,‘P’,‘G’)或者cv2.cv.FOURCC(*‘MJPG’)。 下面的代码是从摄像头中捕获沿水平方向旋转每一帧并保存它。

保存:代码演示

# coding=utf-8

import numpy as np
import cv2

cap=cv2.VideoCapture(0)
fourcc=cv2.cvFOURCC(*'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)
		out.write(frame)
		cv2.imshow('frame',frame)
		if cv2.waitKey(1) &0xFF == ord('q'):
			break
cap.release()
out.release()
cv2.destroyAllWindows()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值