opencvc++四个摄像头标定的代码
时间: 2023-07-24 13:28:30 浏览: 105
摄像头标定的opencv代码
下面是用OpenCV库进行四个摄像头标定的示例代码,包含了角点提取、摄像头标定和保存标定结果等步骤:
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
// 设置标定板参数
Size boardSize(9, 6);
float squareSize = 2.5; // 单位为厘米
vector<Point3f> objPoints;
for (int i = 0; i < boardSize.height; i++)
{
for (int j = 0; j < boardSize.width; j++)
{
objPoints.push_back(Point3f(j * squareSize, i * squareSize, 0));
}
}
// 获取摄像头图像
vector<string> cameraNames = {"camera0", "camera1", "camera2", "camera3"};
vector<VideoCapture> cameras(4);
for (int i = 0; i < 4; i++)
{
cameras[i].open(i);
if (!cameras[i].isOpened())
{
cerr << "Failed to open camera " << i << endl;
return -1;
}
}
// 提取角点
vector<vector<Point2f>> imagePoints(4);
Mat image, gray;
vector<Point2f> corners;
namedWindow("Camera", WINDOW_NORMAL);
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 4; j++)
{
cameras[j] >> image;
cvtColor(image, gray, COLOR_BGR2GRAY);
bool found = findChessboardCorners(gray, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);
if (found)
{
drawChessboardCorners(image, boardSize, corners, found);
imshow(cameraNames[j], image);
waitKey(500);
imagePoints[j].push_back(corners);
}
}
}
destroyAllWindows();
// 标定摄像头
vector<Mat> rvecs, tvecs;
vector<Mat> cameraMatrix(4), distCoeffs(4);
for (int i = 0; i < 4; i++)
{
calibrateCamera(objPoints, imagePoints[i], image.size(), cameraMatrix[i], distCoeffs[i], rvecs, tvecs);
}
// 保存标定结果
FileStorage fs("calibration.yml", FileStorage::WRITE);
for (int i = 0; i < 4; i++)
{
fs << cameraNames[i] << "{"
<< "cameraMatrix" << cameraMatrix[i]
<< "distCoeffs" << distCoeffs[i]
<< "}";
}
fs.release();
return 0;
}
```
在以上代码中,我们首先设置了标定板参数和摄像头名称,然后通过循环获取每个摄像头的图像,并对图像进行角点提取。接着,我们利用得到的角点和标定板参数,对每个摄像头进行标定,得到内参矩阵和畸变系数。最后,将标定结果保存到文件中。
阅读全文