opencv相机标定c++
时间: 2023-09-10 11:10:45 浏览: 171
以下是使用OpenCV进行相机标定的C语言代码示例:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
// 读取标定图像
vector<vector<Point2f>> imagePoints;
vector<vector<Point3f>> objectPoints;
Size imageSize;
int numImages = 10;
float squareSize = 1.0;
Mat cameraMatrix, distCoeffs;
vector<Mat> rvecs, tvecs;
for (int i = 0; i < numImages; i++)
{
Mat image = imread(format("calibration%d.png", i), IMREAD_GRAYSCALE);
if (image.empty())
{
cerr << "Failed to read image " << i << endl;
return -1;
}
imageSize = image.size();
vector<Point2f> corners;
bool found = findChessboardCorners(image, Size(9, 6), corners);
if (found)
{
imagePoints.push_back(corners);
vector<Point3f> objectCorners;
for (int j = 0; j < 54; j++)
{
objectCorners.push_back(Point3f(j / 9 * squareSize, j % 9 * squareSize, 0));
}
objectPoints.push_back(objectCorners);
}
else
{
cerr << "Failed to find chessboard corners in image " << i << endl;
}
}
// 标定相机
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);
// 保存相机参数
FileStorage fs("camera_params.yml", FileStorage::WRITE);
fs << "camera_matrix" << cameraMatrix;
fs << "distortion_coefficients" << distCoeffs;
fs.release();
return 0;
}
```
代码中首先读取标定图像,然后使用`findChessboardCorners`函数寻找标定板角点,将角点坐标和标定板上实际点的坐标存储在`imagePoints`和`objectPoints`中。接着使用`calibrateCamera`函数对相机进行标定,得到相机矩阵和畸变系数,并将其保存在文件中。
阅读全文
相关推荐














