我有 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;
}