-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration.cpp
89 lines (68 loc) · 2.9 KB
/
calibration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <filesystem>
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++) {
if (std::string(argv[i]) == "--help")
{
std::cout << "Usage: " << argv[0] << " <directory>" << std::endl;
std::cout << "directory: The directory containing the calibration images." << std::endl;
return 0;
}
}
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <directory>" << std::endl;
return 1;
}
// Set the chessboard size (number of inner corners in width and height)
cv::Size chessboardSize(6, 9);
cv::Size imgSize;
// Create vectors to store the detected chessboard corners and corresponding image points
std::vector<std::vector<cv::Point3f>> objectPoints; // 3D world points
std::vector<std::vector<cv::Point2f>> imagePoints; // 2D image points
// Generate the 3D world points for the chessboard corners
std::vector<cv::Point3f> worldPoints;
for (int i = 0; i < chessboardSize.height; ++i) {
for (int j = 0; j < chessboardSize.width; ++j) {
worldPoints.emplace_back(j, i, 0); // Assuming the chessboard lies in the XY plane (Z=0)
}
}
std::string imagesDirectory = argv[1];
std::vector<cv::String> imageFiles;
cv::glob(imagesDirectory + "/*.jpg", imageFiles);
if (imageFiles.empty()) {
std::cerr << "Error: No calibration images found in the specified directory." << std::endl;
return -1;
}
for (const auto& imageFile : imageFiles) {
std::cout << "Processing image: " << imageFile << std::endl;
// Load the image
cv::Mat img = cv::imread(imageFile);
imgSize = img.size();
// Convert the image to grayscale
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
// Find chessboard corners
std::vector<cv::Point2f> corners;
if (findChessboardCorners(gray, chessboardSize, corners)) {
// Refine corner locations
cv::cornerSubPix(gray, corners, cv::Size(11, 11), cv::Size(-1, -1),
cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.001));
// Store object and image points
objectPoints.push_back(worldPoints);
imagePoints.push_back(corners);
} else {
std::cerr << "Warning: Chessboard pattern not found in image: " << imageFile << std::endl;
}
}
cv::Mat cameraMatrix, distCoeffs;
std::vector<cv::Mat> rvecs, tvecs;
calibrateCamera(objectPoints, imagePoints, imgSize,
cameraMatrix, distCoeffs, rvecs, tvecs);
cv::FileStorage fs(imagesDirectory + "/calibration_results.yaml", cv::FileStorage::WRITE);
fs << "cameraMatrix" << cameraMatrix;
fs << "distCoeffs" << distCoeffs;
fs.release(); // Release the file
return 0;
}