open获取摄像头视频
头文件:opencv2/highgui/highgui.hpp
**作用:**从摄像头获取视频流
函数原型:
(VideoCapture).open(index)
-
如果默认笔记本/台式机只有一个USB摄像头,Index=0; 如果有2个,一般Index为0和1,根据具体情况区分,摄像头接入和断开会改变Index值
-
如果接入2个以上,但只想用指定的一个,可以在设备管理器中禁用其他,同时Index设置为0
示例:
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<bits/stdc++.h>
using namespace std;
char file[50] = "C:\\Users\\Administrator\\Desktop\\test.avi";
int main() {
cv::namedWindow("1",1);
cv::VideoCapture frames;
frames.open(0);
if (!frames.isOpened()) return 0;
cv::Mat frame;
while(1) {
frames >> frame;
if (frame.empty())break;
//高斯滤波进行平滑处理降噪
cv::GaussianBlur(frame, frame, cv::Size(3, 3), 5, 5);
cv::cvtColor(frame, frame, cv::COLOR_BGR2GRAY);
cv::GaussianBlur(frame, frame, cv::Size(5, 5), 10, 10);
cv::Canny(frame, frame, 1, 20,3,1);
cv::imshow("1", frame);
if (cv::waitKey(1) >=0)break;
}
frames.release();
cv::waitKey(0);
return 0;
}