opencvc++四个摄像头标定的C++代码
时间: 2023-07-24 18:30:14 浏览: 98
以下是使用OpenCV C++标定四个摄像头的示例代码:
```c++
#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
int numBoards = 20;
int numCornersHor = 9;
int numCornersVer = 6;
Size boardSize = Size(numCornersHor, numCornersVer);
vector<vector<Point3f>> objectPoints;
vector<vector<Point2f>> imagePoints;
vector<Point2f> corners;
int successes = 0;
vector<Mat> images;
Mat image;
for (int i = 0; i < 4; i++)
{
for (int j = 1; j <= numBoards; j++)
{
string filename = "camera" + to_string(i) + "_" + to_string(j) + ".jpg";
image = imread(filename, IMREAD_GRAYSCALE);
bool found = findChessboardCorners(image, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH | CALIB_CB_NORMALIZE_IMAGE);
if (found)
{
cornerSubPix(image, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.1));
drawChessboardCorners(image, boardSize, corners, found);
imshow("Board", image);
waitKey(100);
imagePoints.push_back(corners);
objectPoints.push_back(vector<Point3f>());
vector<Point3f> &opt = objectPoints.back();
for (int k = 0; k < numCornersHor * numCornersVer; k++)
{
opt.push_back(Point3f((float)(k / numCornersHor) * 25, (float)(k % numCornersHor) * 25, 0.0f));
}
successes++;
if (successes >= numBoards)
{
break;
}
}
}
}
vector<Mat> rvecs, tvecs;
vector<double> reprojErrs;
double rms = calibrateCameraRO(objectPoints, imagePoints, boardSize, 0, 0, rvecs, tvecs);
cout << "RMS: " << rms << endl;
Mat cameraMatrix, distCoeffs;
vector<Mat> rvecsMat, tvecsMat;
for (int i = 0; i < 4; i++)
{
Mat rvec, tvec;
calibrateCameraRO(objectPoints, imagePoints, boardSize, cameraMatrix, distCoeffs, rvec, tvec, i, 0);
rvecsMat.push_back(rvec);
tvecsMat.push_back(tvec);
}
cout << "Camera matrix: " << endl;
cout << cameraMatrix << endl;
cout << "Distortion coefficients: " << endl;
cout << distCoeffs << endl;
return 0;
}
```
上述代码使用了OpenCV中的`calibrateCameraRO`函数进行标定,并采用棋盘格进行标定。其中,`numBoards`表示采集的标定板数量,`numCornersHor`和`numCornersVer`分别表示棋盘格的水平和垂直角点数。通过对每个摄像头采集图像数据,并分别进行标定,最终得到了每个摄像头的内参矩阵和畸变系数。
阅读全文