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;
}