摄像头捕获彩色视频:
import cv2 # import numpy # import matplotlib.pyplot as plot # 创建摄像头对象 # 使用opencv自带的Videocapture()函数定义摄像头对象,0表示第一个摄像头,一般是笔记本内摄像头 cap = cv2.VideoCapture(0) # 逐帧显示实现视频播放 while(1): # 一帧一帧获取图像 # cap.read() 返回一个布尔值(True/False)。如果帧读取的是正确的, # 就是True。所以最后你可以通过检查他的返回值来查看视频文件是否已经到了结尾 ret,frame = cap.read() # 一帧一帧显示图像 cv2.imshow("capture",frame) if cv2.waitKey(1)&0xFF == ord('q'): break cap.release() cap.destroyAllWindows()
参考: https://blog.csdn.net/huanglu_thu13/article/details/52337013
摄像头捕获灰度视频:
import numpy as np import cv2 cap = cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret,frame = cap.read() # our operations on the frame come here gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # display the resulting frame cv2.imshow('fram',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # when everything done,release the capture cap.release() cv2.destroyAllwindows()
参考: OpenCV-Python中文教程(段力辉 译)