2017-05-19 5 views
0

現在、私のラップトップのウェブカムフィードをVideoCapture cap(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-in-opencv-c –

答えて

1

私は単純なキーボード入力で適切な解決策をインターネットで検索しました。 Therは、cv :: waitKeyを使用している間は、いつも足/遅れていました。

私が見つけた解決策は、Webカメラからフレームをキャプチャした直後にSleep(5)を追加することです。

以下の例は、異なるフォーラムスレッドの組み合わせです。

脚や遅延なしで動作します。 Windows OS。

"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; 
} 
関連する問題