opencv十二点标定
时间: 2023-12-02 19:43:04 浏览: 287
OpenCV的十二点标定法是在九点标定法的基础上增加了三个点,以提高标定精度。这三个点分别位于棋盘格的左上角、右上角和右下角。与九点标定法类似,十二点标定法也需要使用多组不同角度的图片进行标定。下面是使用C++实现OpenCV的十二点标定法的代码:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 定义棋盘格的行数和列数
int board_w = 11;
int board_h = 8;
// 定义棋盘格中每个方格的大小
float square_size = 20;
// 定义棋盘格的三维坐标
vector<Point3f> obj;
for (int i = 0; i < board_h; i++)
{
for (int j = 0; j < board_w; j++)
{
obj.push_back(Point3f(j * square_size, i * square_size, 0));
}
}
// 定义图像中棋盘格的二维坐标
vector<vector<Point2f>> image_points;
vector<Point2f> corners;
// 读取多张图片进行标定
for (int i = 1; i <= 20; i++)
{
// 读取图片
Mat image = imread("image_" + to_string(i) + ".jpg");
// 寻找棋盘格角点
bool found = findChessboardCorners(image, Size(board_w, board_h), corners);
// 如果找到了角点,则进行亚像素级角点检测
if (found)
{
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1), TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1));
// 将角点添加到图像坐标中
image_points.push_back(corners);
// 在图像中绘制角点
drawChessboardCorners(image, Size(board_w, board_h), corners, found);
imshow("image", image);
waitKey(0);
}
}
// 进行标定
Mat camera_matrix, dist_coeffs;
vector<Mat> rvecs, tvecs;
calibrateCamera(objectPoints, image_points, Size(image.cols, image.rows), camera_matrix, dist_coeffs, rvecs, tvecs);
// 输出标定结果
cout << "camera_matrix:" << endl << camera_matrix << endl;
cout << "dist_coeffs:" << endl << dist_coeffs << endl;
return 0;
}
```
阅读全文