#include<opencv2/core/core.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<iostream>
#include<iomanip>
#include<fstream>
#include<chrono>
#include<string>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture(0);
if (!capture.isOpened())
return -1;
capture.set(CAP_PROP_FRAME_WIDTH, 720);
capture.set(CAP_PROP_FRAME_HEIGHT, 720);
capture.set(CAP_PROP_FOURCC, CAP_OPENCV_MJPEG);
//摄像头帧数
capture.set(CAP_PROP_FPS, 15);
printf("width = %.2f\n", capture.get(CAP_PROP_FRAME_WIDTH));
printf("height = %.2f\n", capture.get(CAP_PROP_FRAME_HEIGHT));
printf("fbs = %.2f\n", capture.get(CAP_PROP_FPS));
//定义摄像头读取的一帧画面
Mat frame;
chrono::system_clock::time_point start, end;
int max_image = 0;
int count = 1;
//手动创建 _image 文件夹保存图片
string DATA = "_image";
while (1)
{
capture >> frame;//从相机读取新一帧
//镜像摄像头画面
flip(frame, frame, 1);
Mat src = frame;
if (!frame.empty()) {
start = chrono::system_clock::now();
ostringstream oss;
//给保存至文件夹中的图片进行编号
oss << setfill('0') << setw(3) << count;
imwrite(DATA + "\\pic" + oss.str() + ".png", frame);
imshow("video", src);
waitKey(1);
//保存图片max = 500
if (count == 500) {
cout << max_image;
break;
}
end = chrono::system_clock::now();
double max_image2 = chrono::duration_cast<chrono::milliseconds>(end - start).count();
max_image = max_image + max_image2;
count++;
}
}
return 0;
}
opencv 获取摄像头图像/帧保存为图片
最新推荐文章于 2024-07-21 01:19:28 发布