opencvC++四路相机标定的代码
时间: 2023-07-07 19:35:03 浏览: 99
基于opencv&c++的相机标定
5星 · 资源好评率100%
以下是使用OpenCV库进行四路相机标定的C++代码示例:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
// 设置标定板大小和格子大小
int board_w = 9;
int board_h = 6;
float square_size = 0.023; // 单位为米
// 创建标定板对象
vector<Point3f> board;
for (int i = 0; i < board_h; i++) {
for (int j = 0; j < board_w; j++) {
board.push_back(Point3f(j * square_size, i * square_size, 0));
}
}
// 存储标定板角点的世界坐标和图像坐标
vector<vector<Point3f>> world_points;
vector<vector<Point2f>> image_points;
// 读取四路相机的图片
for (int i = 1; i <= 4; i++) {
Mat img = imread("img" + to_string(i) + ".jpg");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// 检测标定板角点
vector<Point2f> corners;
bool ret = findChessboardCorners(gray, Size(board_w, board_h), corners);
// 如果检测到角点,则添加到world_points和image_points中
if (ret) {
world_points.push_back(board);
image_points.push_back(corners);
drawChessboardCorners(img, Size(board_w, board_h), corners, ret);
imshow("img" + to_string(i), img);
waitKey(500);
}
}
destroyAllWindows();
// 进行相机标定
Mat camera_matrix, dist_coeffs;
vector<Mat> rvecs, tvecs;
calibrateCamera(world_points, image_points, Size(board_w, board_h), camera_matrix, dist_coeffs, rvecs, tvecs);
// 打印标定结果
cout << "Intrinsic matrix:\n" << camera_matrix << endl;
cout << "Distortion coefficients:\n" << dist_coeffs << endl;
for (int i = 0; i < 4; i++) {
cout << "Rotation vector " << i << ":\n" << rvecs[i] << endl;
cout << "Translation vector " << i << ":\n" << tvecs[i] << endl;
}
return 0;
}
```
其中,需要将`img1.jpg` ~ `img4.jpg`替换为你自己的四路相机拍摄的图片。代码中的`square_size`参数需要根据实际情况进行修改,表示标定板上格子的大小。最终输出的标定结果包括相机内参矩阵、畸变系数、旋转向量和平移向量。
阅读全文