-
Notifications
You must be signed in to change notification settings - Fork 2
/
calib.cpp
115 lines (84 loc) · 2.44 KB
/
calib.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "leap.h"
#include <opencv2/opencv.hpp>
#define NUM_FRAMES 30
using namespace cv;
bool found;
Size patternsize(4,11);
Size imagesize(480,640);
Mat distCoeffs = Mat::zeros(8, 1, CV_64F);
Mat cameraMatrix;
bool calibrated = false;
float square_size = 1.0;
Mat display(480,640, CV_8UC3, Scalar(0,0,255));
vector<Point2f> centers;
vector<vector<Point2f> > points;
static void calcCorners(Size boardSize, float squareSize, vector<Point3f>& corners)
{
corners.resize(0);
for( int i = 0; i < boardSize.height; i++ )
for( int j = 0; j < boardSize.width; j++ )
corners.push_back(Point3f(float((2*j + i % 2)*squareSize),
float(i*squareSize), 0));
}
void run_calibration()
{
vector<Point3f> corners;
vector<vector<Point3f> > objectPoints(1);
vector<Mat> rvecs;
vector<Mat> tvecs;
printf("Running calibration\n");
calcCorners(patternsize, square_size, objectPoints[0]);
objectPoints.resize(points.size(), objectPoints[0]);
double rms = calibrateCamera(objectPoints, points, imagesize, cameraMatrix,
distCoeffs, rvecs, tvecs, CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
///*|CV_CALIB_FIX_K3*/|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
printf("RMS error reported by calibrateCamera: %g\n", rms);
calibrated = true;
}
void render_callback(leap_t *leap, int key)
{
Mat raw_left(leap->left);
Mat raw_right(leap->right);
if(key >= 0)
{
if(key == 'c' && found == true)
{
points.push_back(centers);
printf("Captured %lu/%d\n", points.size(), NUM_FRAMES);
if(points.size() >= NUM_FRAMES)
{
run_calibration();
}
}
}
imshow("left", raw_left);
imshow("right", raw_right);
imshow("display", display);
}
void callback(leap_t *leap)
{
Mat fixed(480,640, CV_8UC1, Scalar(0));
Mat left(leap->left);
if(calibrated == true)
{
undistort(left, fixed, cameraMatrix, distCoeffs);
cvtColor(fixed, display, CV_GRAY2RGB);
}
else
{
found = findCirclesGrid(left, patternsize, centers, CALIB_CB_ASYMMETRIC_GRID);
cvtColor(left, display, CV_GRAY2RGB);
drawChessboardCorners(display, patternsize, Mat(centers), found);
}
}
int main()
{
leap_t *leap;
leap_open(&leap, &callback);
leap_diag(leap);
leap->render_callback = render_callback;
leap_run(leap);
}