opencv4.6.0版本的四路相机标定C++代码
时间: 2024-03-02 08:50:36 浏览: 65
以下是使用OpenCV 4.6.0版本进行四路相机标定的C++代码示例:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// 定义相机数量和图像数量
const int numCameras = 4;
const int numBoards = 20;
const int numCornersHor = 9;
const int numCornersVer = 6;
const int numSquares = numCornersHor * numCornersVer;
const int boardSize = 30;
// 创建相机对象
vector<VideoCapture> cameras(numCameras);
for (int i = 0; i < numCameras; i++)
{
cameras[i].open(i);
if (!cameras[i].isOpened())
{
cout << "Error: Could not open camera " << i << endl;
return -1;
}
}
// 初始化图像点和物理点向量
vector<vector<Point2f>> imagePoints(numCameras);
vector<vector<Point3f>> objectPoints(numCameras);
// 获取棋盘格角点
vector<Point2f> corners;
bool found;
for (int i = 0; i < numCameras; i++)
{
for (int j = 0; j < numBoards; j++)
{
Mat image;
cameras[i] >> image;
found = findChessboardCorners(image, Size(numCornersHor, numCornersVer), corners);
if (found)
{
Mat grayImage;
cvtColor(image, grayImage, COLOR_BGR2GRAY);
cornerSubPix(grayImage, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.1));
drawChessboardCorners(image, Size(numCornersHor, numCornersVer), corners, found);
imshow("Chessboard", image);
waitKey(100);
// 添加图像点和物理点
vector<Point2f> cornersTransformed;
for (int k = 0; k < corners.size(); k++)
{
cornersTransformed.push_back(Point2f(corners[k].x + i * image.cols, corners[k].y));
}
imagePoints[i].push_back(cornersTransformed);
vector<Point3f> objectCorners;
for (int k = 0; k < numSquares; k++)
{
objectCorners.push_back(Point3f(k / numCornersHor * boardSize, k % numCornersHor * boardSize, 0.0f));
}
objectPoints[i].push_back(objectCorners);
}
}
}
// 进行标定
Mat cameraMatrix[numCameras], distCoeffs[numCameras];
vector<Mat> rvecs[numCameras], tvecs[numCameras];
for (int i = 0; i < numCameras; i++)
{
calibrateCamera(objectPoints[i], imagePoints[i], Size(boardSize * numCornersHor, boardSize * numCornersVer), cameraMatrix[i], distCoeffs[i], rvecs[i], tvecs[i]);
}
// 打印结果
for (int i = 0; i < numCameras; i++)
{
cout << "Camera " << i << ":" << endl;
cout << "Camera matrix:" << endl << cameraMatrix[i] << endl;
cout << "Distortion coefficients:" << endl << distCoeffs[i] << endl;
cout << "Rotation vectors:" << endl << rvecs[i] << endl;
cout << "Translation vectors:" << endl << tvecs[i] << endl;
}
return 0;
}
```
注意:这只是一个示例代码,你需要根据你的实际情况进行修改,比如相机数量、图像数量、棋盘格大小等。
阅读全文