cv2.videocapture
时间: 2023-09-11 17:03:27 浏览: 114
cv2.VideoCapture is a function in the OpenCV library that is used for capturing video from a camera or a video file. It creates a video capture object that can be used to read frames from the video source. The function takes a parameter that specifies the index of the camera or the path of the video file to be opened.
Syntax:
```python
cv2.VideoCapture(index or filename)
```
Example:
```python
import cv2
# Open the default camera
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if not cap.isOpened():
print("Error opening video stream or file")
# Read until video is completed
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
# Display the resulting frame
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
# Release the video capture object and close all windows
cap.release()
cv2.destroyAllWindows()
```
In this example, the function is used to open the default camera (index 0) and read frames from it. The frames are displayed using the cv2.imshow() function and the loop is exited when the user presses the 'q' key on the keyboard. Finally, the video capture object is released using the cap.release() function and all windows are closed using cv2.destroyAllWindows().
阅读全文