赞
踩
目录
1.2.4——cap.get(propId) cap.set(propIDd,value)
语法:cv.VideoCapture(device)
参数:device可以是设备索引(device index) 也可以是视频文件名称/地址 (the name of a video file)
- import numpy as np
- import cv2 as cv
- cap = 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 capture
- cap.release()
- cv.destroyAllWindows()
ret,frame=cap.read()
cap.read()返回一个布尔值,这里用ret接收,如果为True,则说明每一帧图像都被正常读取
frame用于接收得到的每一帧图片
- if not cap.isOpened():
- print("Cannot open camera")
- exit()
用于判断cap是否正常初始化,返回布尔值,正常则返回True
可以使用函数 cap.get(propId) 来获得视频的一些参数信息。这里 propId 可以是 0 到 18 之间的任何整数。每一个数代表视频的一个属性,其中的一些值可以使用 cap.set(propId,value) 来修改,value 就是 你想要设置成的新值
可以使用 cap.get(3) 和 cap.get(4) 来查看每一帧的宽和高。 默认情况下得到的值是 640X480。但是可以使用 cap.set(3,320) 和 cap.set(4,240) 来把宽和高改成 320X240
propId有以下值:(以下值可以以数字代替,按顺序从0开始)
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 cameras (note: only supported by DC1394 v 2.x backend cur- rently) |
- import numpy as np
- import cv2 as cv
- cap = cv.VideoCapture('vtest.avi')
- while cap.isOpened():
- ret, frame = cap.read()
- # if frame is read correctly ret is True
- if not ret:
- print("Can't receive frame (stream end?). Exiting ...")
- break
- gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
- cv.imshow('frame', gray)
- if cv.waitKey(1) == ord('q'):
- break
- cap.release()
- cv.destroyAllWindows()
将frame接收到的每一帧图片,一帧帧播放出来(imshow)
语法:VideoWriter(output_filename,FourCC,fps,size)
参数:
output_filename:文件路径
FourCC:编码
fps:帧率 (每秒的帧数)
size:视频窗口画幅 (width,height)
编码设置:用cv.VideoWriter_fourcc()设置编码,常用编码格式有以下几种:
FourCC | 视频文件后缀 |
---|---|
cv2.VideoWriter_fourcc('M', 'P', '4', 'V') | .mp4 |
cv2.VideoWriter_fourcc('X', 'V', 'I', 'D') | .avi |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。