内参标定中理想点 怎么求出畸变点 C++
时间: 2024-02-03 11:12:22 浏览: 26
在相机标定中,我们通过拍摄多张不同姿态下的棋盘格图像,来获得相机的内参和畸变参数。其中,内参包括相机的焦距、主点等,畸变参数则是为了补偿图像中的畸变而引入的。
在畸变参数的求解过程中,我们需要找到理想点和畸变点。理想点是指在没有畸变的情况下,棋盘格上每个角点对应的在相机坐标系下的坐标值。而畸变点则是指由于相机镜头等因素引起的畸变而出现的实际角点坐标值。
下面是一个求解畸变点的示例代码(使用OpenCV库):
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// 定义棋盘格的宽高
int board_width = 9;
int board_height = 6;
// 生成理想点坐标
std::vector<Point3f> obj_points;
for (int i = 0; i < board_height; i++)
{
for (int j = 0; j < board_width; j++)
{
obj_points.push_back(Point3f(j, i, 0));
}
}
// 读取多张棋盘格图像
std::vector<std::vector<Point2f>> img_points;
std::vector<std::string> img_names = {"img1.jpg", "img2.jpg", "img3.jpg"};
for (const auto& img_name : img_names)
{
Mat img = imread(img_name);
cvtColor(img, img, COLOR_BGR2GRAY);
std::vector<Point2f> corners;
bool found = findChessboardCorners(img, Size(board_width, board_height), corners);
if (found)
{
// 计算畸变点坐标
cornerSubPix(img, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 30, 0.1));
img_points.push_back(corners);
}
}
// 求解相机的内参和畸变参数
Mat camera_matrix, dist_coeffs;
std::vector<Mat> rvecs, tvecs;
calibrateCamera(obj_points, img_points, Size(640, 480), camera_matrix, dist_coeffs, rvecs, tvecs);
// 输出结果
std::cout << "camera_matrix: \n" << camera_matrix << std::endl;
std::cout << "dist_coeffs: \n" << dist_coeffs << std::endl;
return 0;
}
```
在上述代码中,我们首先定义了理想点坐标,然后通过 `findChessboardCorners()` 函数找到每张图像中的角点坐标值,再通过 `cornerSubPix()` 函数对角点坐标进行亚像素级别的精确化,最后通过 `calibrateCamera()` 函数求解相机的内参和畸变参数。在输出结果中,`camera_matrix` 就是相机的内参矩阵,`dist_coeffs` 就是相机的畸变参数。