0
我正在使用VideoCapture帽(0)功能获取笔记本电脑的摄像头反馈,然后将其显示在Mat框架中。接下来我想要做的就是每当我按下一个键“c”时,它就会截取帧的截图并将其作为JPEG图像保存到一个文件夹中。但我不知道如何去做。非常需要帮助,谢谢。使用OpenCV和C++拍摄Keypress上的摄像头反馈截图
我正在使用VideoCapture帽(0)功能获取笔记本电脑的摄像头反馈,然后将其显示在Mat框架中。接下来我想要做的就是每当我按下一个键“c”时,它就会截取帧的截图并将其作为JPEG图像保存到一个文件夹中。但我不知道如何去做。非常需要帮助,谢谢。使用OpenCV和C++拍摄Keypress上的摄像头反馈截图
我花了几天的时间在简单的键盘输入上搜索正确的解决方案。在使用cv :: waitKey的时候,它总是有些腿/延迟。
我发现的解决方案是在从网络摄像头捕获帧之后添加睡眠(5)。
以下示例是不同论坛线程的组合。
这工作没有任何腿/延迟。 Windows操作系统。
按“q”键捕获并保存帧。
有一个摄像头馈送总是存在的。您可以更改序列以显示拍摄的帧/图像。
PS“tipka” - 表示键盘上的“键”。
的问候,安德烈
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h> // For Sleep
using namespace cv;
using namespace std;
int ct = 0;
char tipka;
char filename[100]; // For filename
int c = 1; // For filename
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID + apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press a to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
Sleep(5); // Sleep is mandatory - for no leg!
// show live and wait for a key with timeout long enough to show images
imshow("CAMERA 1", frame); // Window name
tipka = cv::waitKey(30);
if (tipka == 'q') {
sprintf_s(filename, "C:/Images/Frame_%d.jpg", c); // select your folder - filename is "Frame_n"
cv::waitKey(10);
imshow("CAMERA 1", frame);
imwrite(filename, frame);
cout << "Frame_" << c << endl;
c++;
}
if (tipka == 'a') {
cout << "Terminating..." << endl;
Sleep(2000);
break;
}
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
这线程也许能帮助你;在OP的代码应该工作(他只是遇到了一些麻烦与Visual Studio):http://stackoverflow.com/questions/26940378/how-do-i-grab-a-still-image-from-a-cam-using- imwrite合的OpenCV-C –