官术网_书友最值得收藏!

Reading camera frames

This first example, read_camera.py, shows you how to read frames from a camera that's connected to your computer. The required argument is index_camera, which indicates the index of the camera to read. If you have connected a webcam to your computer, it has an index of 0. Additionally, if you have a second camera, you can select it by passing 1. As you can see, the type of this parameter is int

The first step to work with cv2.VideoCapture is to create an object to work with. In this case, the object is capture, and we call the constructor like this:

 # We create a VideoCapture object to read from the camera (pass 0):
capture = cv2.VideoCapture(args.index_camera)

If index_camera is 0 (your first connected camera), it is equivalent to cv2.VideoCapture(0). To check whether the connection has been established correctly, we have the capture.isOpened() method, which returns False if the connection could not be established. In the same way, if the capture was initialized correctly, this method returns True.

To capture footage frame by frame from the camera, we call the capture.read() method, which returns the frame from the camera. This frame has the same structure as an image in OpenCV, so we can work with it in the same way. For example, to convert the frame into grayscale, do the following:

gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

Additionally, capture.read() returns a bool. This bool indicates whether the frame has been correctly read from the capture object. 

主站蜘蛛池模板: 无棣县| 芜湖市| 东台市| 宝丰县| 桂东县| 宜黄县| 茶陵县| 榆中县| 秀山| 中阳县| 诸暨市| 霍山县| 乌兰察布市| 常州市| 丽水市| 左云县| 景宁| 航空| 仁布县| 兰考县| 罗江县| 六枝特区| 金川县| 郓城县| 托里县| 惠州市| 龙胜| 桓台县| 噶尔县| 诸暨市| 灌阳县| 乌拉特前旗| 昭通市| 阿拉善右旗| 壤塘县| 南乐县| 东兴市| 尼木县| 吉安县| 宜兰市| 长岭县|