-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalibration.cpp
More file actions
62 lines (46 loc) · 1.69 KB
/
Calibration.cpp
File metadata and controls
62 lines (46 loc) · 1.69 KB
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
#include "Calibration.h"
using namespace cv;
using namespace std;
const Size BOARD_SIZE = Size(9, 6);
const int MINIMUM_CALIBRATION_IMAGES = 15;
vector<Mat> savedImages;
vector<vector<Point2f>> imagePoints;
void getKnownCorners(Size boardSize, float squareSize, vector<Point3f>& corners) {
for (int i = 0; i < boardSize.height; i++) {
for (int j = 0; j < boardSize.width; j++) {
corners.push_back(Point3f(j * squareSize, i * squareSize, 0.0f));
}
}
}
int supply_calibration_image(image_u8* img) {
Mat mat = Mat(img->height, img->stride, CV_8UC1, img->buf); // assumes no padding; cols*elSize
vector<Point2f> corners;
bool found = findChessboardCorners(mat, BOARD_SIZE, corners, CALIB_CB_FAST_CHECK | CALIB_CB_NORMALIZE_IMAGE);
if (found) {
savedImages.push_back(mat);
imagePoints.push_back(corners);
}
return savedImages.size();
}
int clear_calibration_images() {
savedImages.clear();
imagePoints.clear();
return savedImages.size();
}
bool calibrate(float squareSize, intrinsics* outputIntrinsics) {
if (savedImages.size() < MINIMUM_CALIBRATION_IMAGES) {
return false;
}
vector<vector<Point3f>> objectPoints(1);
getKnownCorners(BOARD_SIZE, squareSize, objectPoints[0]);
objectPoints.resize(imagePoints.size(), objectPoints[0]);
vector<Mat> rVectors, tVectors;
Mat distanceCoefficients = Mat::zeros(8, 1, CV_64F);
Mat cameraMatrix;
calibrateCamera(objectPoints, imagePoints, BOARD_SIZE, cameraMatrix, distanceCoefficients, rVectors, tVectors);
outputIntrinsics->fx = cameraMatrix.at<double>(0, 0);
outputIntrinsics->fy = cameraMatrix.at<double>(1, 1);
outputIntrinsics->cx = cameraMatrix.at<double>(0, 2);
outputIntrinsics->cy = cameraMatrix.at<double>(1, 2);
return true;
}