1、仅调用摄像头
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame;
VideoCapture capture(0);
namedWindow("My_Camera");
while(1)
{
if(capture.isOpened())
{
capture>>frame;
imshow("My_Camera",frame);
char c=waitKey(30);
if(c==27)
break;
}
else
{
cout<<"No Camera Input!"<<endl;
break;
}
}
return 0;
}
2、调用摄像头+空格键截图保存图片
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<iostream>
using namespace std;
using namespace cv;
#define camera_name "摄像头"
#define picwindow_name "截图图片"
int main()
{
VideoCapture capture(0);
if (!capture.isOpened())
{
cout << "摄像头打开失败!" << endl;
return 0;
}
Mat frame;
char key;
char picture_name[200];
int i = 0;
namedWindow(camera_name);
namedWindow(picwindow_name);
while (1)
{
key = waitKey(30);
capture >> frame;
imshow(camera_name, frame);
if (key == 27)
{
break;
}
if (key == 32)
{
sprintf(picture_name, "C:\\DZH\\pic%d.jpg", i++);
imwrite(picture_name, frame);
imshow(picwindow_name, frame);
}
}
return 0;
}