2017-05-19 56 views
0

我正在使用VideoCapture帽(0)功能獲取筆記本電腦的攝像頭反饋,然後將其顯示在Mat框架中。接下來我想要做的就是每當我按下一個鍵「c」時,它就會截取幀的截圖並將其作爲JPEG圖像保存到一個文件夾中。但我不知道如何去做。非常需要幫助,謝謝。使用OpenCV和C++拍攝Keypress上的攝像頭反饋截圖

+0

這線程也許能幫助你;在OP的代碼應該工作(他只是遇到了一些麻煩與Visual Studio):http://stackoverflow.com/questions/26940378/how-do-i-grab-a-still-image-from-a-cam-using- imwrite合的OpenCV-C –

回答

1

我花了幾天的時間在簡單的鍵盤輸入上搜索正確的解決方案。在使用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; 
}