按下Python / Opencv键时保存图像

时间:2013-10-18 11:31:06

标签: python opencv

我正在尝试在按键时保存网络摄像头中的图像但不起作用。在这里,我附上我的代码:

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()

3 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

您只需按' 即可保存图像。以下是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

希望这会有所帮助。 :)

答案 2 :(得分: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;
}