作者之前在做棋盘格双目标定的时候是同时开启两个摄像头的驱动拍摄视频,然后举着棋盘格做出不同的姿势,然后再在左目和右目视频中找到对应的棋盘格姿势,把对应的帧挑出来作为标定的源图片,后来发现这样做误差太大,因为我们身体会有一定的小晃动,虽然左右帧看起来很类似,但是在标定程序看来还是有很大误差的。所以开始写个标定图片采集程序。
运行平台为VS2019,opencv版本为3.4.2
#include <opencv2\opencv.hpp>
#include <iostream>
#include <windows.h>
#include<string>
#include<vector>
using namespace cv;
using namespace std;
#define typecam//2个相机驱动
//int main17060500()
int main()
{
Mat view0;
#ifndef typecam
#else
VideoCapture m_InputCapturel(1);
VideoCapture m_InputCapturer(2);
m_InputCapturel.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
m_InputCapturel.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
m_InputCapturer.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
m_InputCapturer.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
while (!m_InputCapturel.isOpened())
{
Sleep(2);
cout << "cannot open the cameral!" << endl;
}
while (!m_InputCapturer.isOpened())
{
Sleep(2);
cout << "cannot open the camerar!" << endl;
}
#endif // !typecam
printf("按“C”键截取当前帧并保存为标定图片...\n按“Q”键退出截取帧过程...\n\n");
int number_image = 1;
string str1;
str1 = ".jpg";
char filenamel[1024] = "";
char filenamer[1024] = "";
for (;;)
{
Mat mLeftView, mRightView;
#ifndef typecam
#else
m_InputCapturel >> mLeftView;
m_InputCapturer >> mRightView;
#endif // !typecam
imshow("cameraR", mRightView);
imshow("cameraL", mLeftView);
waitKey(1);
char mod;
cout << "请输入mod:" << endl;
cin >> mod;
if (mod == 'c' || mod == 'C')
{
sprintf_s(filenamel, "C:/Users/17864/Desktop/Python/binocularResult/calibration pictures/Left/left%d.jpg", number_image);
//printf(mod);
cout << filenamel << endl;
imwrite(filenamel, mLeftView);
sprintf_s(filenamer, "C:/Users/17864/Desktop/Python/binocularResult/calibration pictures/Right/right%d.jpg", number_image);
imwrite(filenamer, mRightView);
cout << "成功获取当前帧,并以文件名" << filenamel << "保存...\n\n";
cout << "成功获取当前帧,并以文件名" << filenamer << "保存...\n\n";
printf("按“C”键截取当前帧并保存为标定图片...\n按“Q”键退出截取帧过程...\n\n");
number_image++;
}
else if (mod == 'q' || mod == 'Q')
{
printf("截取图像帧过程完成...\n\n");
cout << "共成功截取" << --number_image << "帧图像!!\n\n";
break;
}
}
return 0;
}
该程序运行后在dos窗口输入c或者C即可实现同时采集左右标定图片,想退出可以选择q或者Q。正常来说标定图片采集20组双目标定图即可。如果有什么困惑请在下面评论。