一、打开摄像头
import cv2
# 打开摄像头并灰度化显示
capture = cv2.VideoCapture(0)
while(True):
# 获取一帧
ret, frame = capture.read()
# 将这帧转换为灰度图
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)
# 如果输入q,则退出
if cv2.waitKey(1) == ord('q'):
break
cv2.VideoCapture(0)创建VideoCapture对象,参数0表示摄像头编号,如果你有两个摄像头,也可以传入1
# 获取捕获的分辨率
# propId可以直接写数字,也可以用OpenCV的符号表示
width, height = capture.get(3), capture.get(4)
print(width, height)
# 以原分辨率的一倍来捕获
capture.set(cv2.CAP_PROP_FRAME_WIDTH, width * 2)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, width * 2)
想了解完整的属性列表可以参考:VideoCaptureProperties。也可以使用cap.set(propId, value)来修改属性值
二、播放本地视频
# 播放本地视频
capture = cv2.VideoCapture('4.视频展台的使