1

当我按下一个键但没有工作时,我正在尝试保存来自网络摄像头的图像。在这里我附上我的代码:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
i=0

def repeat():
 global capture 
 global camera_index
 frame = cv.QueryFrame(capture)
 cv.ShowImage("w1", frame)
 c = cv.WaitKey(25)
 if(c=="n"): 
  cv.SaveImage("f"+str(i)+".jpg",frame) 
  i=i+1
while True:
    repeat()
4

3 回答 3

1

cv.WaitKey() 不会返回密钥,但会在超时时给出 -1。

解决了:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
i=0

def repeat():
 global capture 
 global camera_index
 frame = cv.QueryFrame(capture)
 cv.ShowImage("w1", frame)
 c = cv.WaitKey(25)
 if (c != -1): 
  cv.SaveImage("f"+str(i)+".jpg",frame) 
  i=i+1
while True:
    repeat()
于 2013-10-18T11:53:56.617 回答
0

您可以简单地按's'来保存图像。以下是 C++ 中的示例。

像这样简单地做

char ch =  cvWaitKey(25);  // Wait for 25 ms for user to hit any key

  // Save image if s was keyboard
  if(ch=='s')
  {
     cvSaveImage(path,small);
  }
  if(ch==27) break;  // If Escape Key was hit just exit the loop

希望这可以帮助。:)

于 2017-01-07T11:31:36.340 回答
0

我有 Opencv c++ 代码,可以在按下任何键盘按钮时保存图像。您可以将此 opencv c++ 代码转换为 opencv python。

用法:

选项:

按 1 从 CAM 1 捕获

按 2 从 CAM 2 捕获

按 d 从 CAM 1 和 CAM 2 捕获

按 a 终止

在这里,我使用了两台 FOV > 180 度的相机。因此,两个相机可以捕捉到具有共同视野的一个图案。或者您也可以从单台相机拍摄。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
#include <unistd.h>


using namespace cv;
using namespace std;


int ct = 0;
char key;
char filename[100];
int  c = 1;
char filename1[100];
int  d = 1;

int main(int, char**)
{


    Mat frame,frame1;
    cout <<"USAGE:\n"
           "Options:\n"
           "Press 1 for capturing from CAM 1\n"
           "Press 2 for capturing from CAM 2\n"
           "Press d for capturing from both CAM 1 & CAM 2\n"
           "Press a for Termination"<<endl;

    //--- INITIALIZE VIDEOCAPTURE
    VideoCapture cap(0);
    VideoCapture cap1(1);
//    cap.set(CAP_PROP_FRAME_WIDTH,1024);
//    cap.set(CAP_PROP_FRAME_HEIGHT,768);
//    cap1.set(CAP_PROP_FRAME_WIDTH,1024);
//    cap1.set(CAP_PROP_FRAME_HEIGHT,768);


    while(true)
    {

        cap >> frame;
        cap1 >> frame1;

        double cam_1 =cap.get(CV_CAP_PROP_POS_MSEC);
        double cam_2 =cap.get(CV_CAP_PROP_POS_MSEC);

        if (frame.empty()||frame1.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }

        usleep(5);
        imshow("CAMERA 1", frame);
        imshow("CAMERA 2", frame1);

        tipka = cv::waitKey(30);

        if (key == 'd')         //CAPTURING IMAGE FROM TWO CAMERA AT A SAME TIME
        {
            sprintf(filename, "/home/user/capture_imgs/Two_cam/0-%d.jpg", c);
            sprintf(filename1, "/home/user/capture_imgs/Two_cam/1-%d.jpg", d);

            //            imshow("CAMERA 1", frame);
            //            imshow("CAMERA 2", frame1);
            imwrite(filename, frame);
            imwrite(filename1, frame1);
            cout << "Cam 1 image captured   = " << c << endl;
            cout << "Cam 2 image captured   = " << d << endl;
            c++;
            d++;
        }
        if(key == '1')      //CAPTURING IMAGE FROM CAM 1
        {
            sprintf(filename, "/home/user/capture_imgs/CAM_1/0-%d.jpg", c);
            //            imshow("CAMERA 1", frame);
            imwrite(filename, frame);
            cout << "Cam 1 image captured   = " << c << endl;
            c++;
        }
        if(key == '2')      //CAPTURING IMAGE FROM CAM 2
        {
            sprintf(filename1, "/home/user/capture_imgs/CAM_2/1-%d.jpg", d);
            //            imshow("CAMERA 2", frame1);
            imwrite(filename1, frame1);
            cout << "Cam 2 image captured   = " << d << endl;
            d++;

        }
        if (key == 'a')
        {
            cout << "Terminated..." << endl;
            usleep(1000);
            break;
        }
    }
    return 0;
}
于 2018-02-22T06:49:40.300 回答