简体   繁体   English

使用OpenCV和C ++拍摄Keypress上的网络摄像头摘要的屏幕截图

[英]Take screenshot of webcam feed on Keypress using OpenCV and C++

I'm currently getting the webcam feed of my laptop using VideoCapture cap(0) function and then display it in a Mat frame. 我目前正在使用VideoCapture cap(0)函数获取笔记本电脑的网络摄像头,然后将其显示在Mat框架中。 What I want to do next is whenever I press a key 'c' for example, it takes the screenshot of the frame and save it into a folder as a JPEG image. 我接下来要做的是例如每当按下键“ c”时,它都会获取框架的屏幕截图并将其保存为JPEG图像到文件夹中。 However I have no idea on how to do so. 但是我不知道该怎么做。 Help is much needed, thank you. 非常需要帮助,谢谢。

I have spent several days searching the internet for the right solution with simple keyboard input. 我花了几天的时间在互联网上搜索简单的键盘输入即可找到正确的解决方案。 Ther was allways some leg / delay while using cv::waitKey. 在使用cv :: waitKey时,它们总是有些延迟。

The solution i have found is with adding Sleep(5) just after the capturing the frame from webcam. 我发现的解决方案是在从网络摄像头捕获帧之后添加Sleep(5)。

The below example is a combination of different forum threads. 以下示例是不同论坛线程的组合。

It works without any leg / delay. 它的工作没有任何延迟。 Windows OS. Windows操作系统。

Press "q" to capture and save the frame. 按“ q”捕获并保存帧。

There is a webcam feed always present. 总有一个网络摄像头。 You can change the sequence to show the captured frame / image. 您可以更改顺序以显示捕获的帧/图像。

PS "tipka" - means "key" on the keyboard. PS“ tipka”-表示键盘上的“键”。

Regards, Andrej 问候安德烈

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM