函数说明
实现这个功能,主要依靠OpenCV的 VideoCapture ,它可以用于打开摄像头或者视频,如果你使用笔记本,并且带有摄像头的话,可以使用 0 打开摄像头。如果给定视频位置,则打开视频。
VideoCapture ()
默认函数,通常我们不使用这个来构造VideoCapture对象。
VideoCapture (const String &filename, int apiPreference=CAP_ANY)
用来打开视频,也可以用来打开IP摄像头。其中 apiPreference 可以用来强制 OpenCV 使用某种方法对数据进行解码,可以用参数包括
- cv::CAP_FFMPEG
- cv::CAP_IMAGES
- cv::CAP_DSHOW.
VideoCapture (int index, int apiPreference=CAP_ANY)
index指示的是打开什么视频设备,通常我们令index为0,打开默认的摄像头。
使用Python打开摄像头或者视频
video = cv2.VideoCapture(0)
cv2.namedWindow("frame")
while video.isOpened():
success, frame = video.read()
if success and cv2.waitKey(1) & 0xFF != ord('q'):
cv2.imshow('frame', frame)
else:
break
cv2.destroyAllWindows()
video.release()
使用C++打开摄像头或者视频
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
int main() {
cv::VideoCapture video("sample.mp4");
cv::namedWindow("frame");
cv::Mat frame;
while(video.isOpened()) {
bool success = video.read(frame);
if (success && cv::waitKey(1) != 'q') {
cv::imshow("frame", frame);
} else {
break;
}
}
cv::destroyAllWindows();
video.release();
return -1;
}