opencvc++四个摄像头标定和去畸变,并保存标定参数以及使用畸变图像的代码
时间: 2024-03-08 11:45:55 浏览: 78
鱼眼相机内参和畸变参数标定(Python+OpenCV)
5星 · 资源好评率100%
好的,以下是使用OpenCV进行四个摄像头标定和去畸变,并保存标定参数和畸变图像的C++代码示例:
标定并保存标定参数:
```c++
// 定义标定板的大小
Size boardSize = Size(8, 6);
// 定义标定板上每个角点的实际坐标
vector<Point3f> objectPoints;
for (int i = 0; i < boardSize.height; i++) {
for (int j = 0; j < boardSize.width; j++) {
objectPoints.push_back(Point3f(j, i, 0));
}
}
// 读取图像和角点坐标
vector<vector<Point2f>> imagePoints;
vector<vector<Point3f>> objectPointsList;
for (int i = 0; i < 4; i++) {
string filename = "camera" + to_string(i) + ".png";
Mat image = imread(filename);
vector<Point2f> corners;
bool found = findChessboardCorners(image, boardSize, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK);
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));
imagePoints.push_back(corners);
objectPointsList.push_back(objectPoints);
}
}
// 标定相机
Mat cameraMatrix[4], distCoeffs[4];
vector<Mat> rvecs[4], tvecs[4];
calibrateCamera(objectPointsList, imagePoints, Size(image.cols, image.rows), cameraMatrix, distCoeffs, rvecs, tvecs);
// 保存标定参数
FileStorage fs("calibration.xml", FileStorage::WRITE);
fs << "cameraMatrix0" << cameraMatrix[0];
fs << "cameraMatrix1" << cameraMatrix[1];
fs << "cameraMatrix2" << cameraMatrix[2];
fs << "cameraMatrix3" << cameraMatrix[3];
fs << "distCoeffs0" << distCoeffs[0];
fs << "distCoeffs1" << distCoeffs[1];
fs << "distCoeffs2" << distCoeffs[2];
fs << "distCoeffs3" << distCoeffs[3];
fs.release();
```
去畸变并保存畸变图像:
```c++
// 读取标定参数
FileStorage fs("calibration.xml", FileStorage::READ);
Mat cameraMatrix[4], distCoeffs[4];
fs["cameraMatrix0"] >> cameraMatrix[0];
fs["cameraMatrix1"] >> cameraMatrix[1];
fs["cameraMatrix2"] >> cameraMatrix[2];
fs["cameraMatrix3"] >> cameraMatrix[3];
fs["distCoeffs0"] >> distCoeffs[0];
fs["distCoeffs1"] >> distCoeffs[1];
fs["distCoeffs2"] >> distCoeffs[2];
fs["distCoeffs3"] >> distCoeffs[3];
fs.release();
// 去畸变并保存畸变图像
Mat undistorted[4];
for (int i = 0; i < 4; i++) {
string filename = "camera" + to_string(i) + ".png";
Mat image = imread(filename);
undistort(image, undistorted[i], cameraMatrix[i], distCoeffs[i]);
string undistortedFilename = "undistorted" + to_string(i) + ".png";
imwrite(undistortedFilename, undistorted[i]);
}
```
请注意,上述代码仅供参考,并且假设您已经拍摄了足够的图像来进行标定。在实际应用中,需要根据具体情况进行调整。
阅读全文