/
kinect_calibration.cpp
150 lines (127 loc) · 3.96 KB
/
kinect_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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/contrib/contrib.hpp"
#include <stdio.h>
#include <iostream>
//#include <stdio.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
int numBoards = atoi(argv[1]);
int board_w = atoi(argv[2]);
int board_h = atoi(argv[3]);
Size board_sz = Size(board_w, board_h);
int board_n = board_w*board_h;
vector<vector<Point3f> > object_points;
vector<vector<Point2f> > image_points;
vector<Point2f> corners;
vector<Point3f> obj;
for (int j=0; j<board_n; j++)
{
obj.push_back(Point3f(j/board_h, j%board_h, 0.0f));
}
Mat img, gray;
cout << "Device opening ..." << endl;
VideoCapture capture;
capture.open( CV_CAP_OPENNI );
//registration
if(capture.get( CV_CAP_PROP_OPENNI_REGISTRATION ) == 0)
{
capture.set(CV_CAP_PROP_OPENNI_REGISTRATION, 1);
cout << "\nImages have been registered ..." << endl;
}
//cout << cv::getBuildInformation() << endl;
if( !capture.isOpened() )
{
cout << "Can not open a capture object." << endl;
return -1;
}
if( capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR_PRESENT ) )
{
cout <<
"\nImage generator output mode:" << endl <<
"FRAME_WIDTH " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_WIDTH ) <<
" | FRAME_HEIGHT " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FRAME_HEIGHT ) <<
" | FPS " << capture.get( CV_CAP_OPENNI_IMAGE_GENERATOR+CV_CAP_PROP_FPS ) << endl;
}
else
{
cout << "\nDevice doesn't contain image generator." << endl;
}
int success = 0;
int k = 0;
bool found = false;
while (success < numBoards)
{
if( !capture.grab() )
{
cout << "Can not grab images." << endl;
return -1;
}
//capture.retrieve( depth, CV_CAP_OPENNI_DEPTH_MAP );
capture.retrieve( img, CV_CAP_OPENNI_BGR_IMAGE );
cvtColor(img, gray, CV_BGR2GRAY);
found = findChessboardCorners(gray, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
if (found)
{
cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
drawChessboardCorners(gray, board_sz, corners, found);
}
// imshow("image", img);
imshow("corners", gray);
k = waitKey(10);
if (found)
{
k = waitKey(0);
}
if (k == 27)
{
break;
}
if (k == ' ' && found !=0)
{
image_points.push_back(corners);
object_points.push_back(obj);
printf ("Corners stored\n");
success++;
if (success >= numBoards)
{
break;
}
}
}
destroyAllWindows();
printf("Starting calibration\n");
Mat intrinsic = Mat(3, 3, CV_32FC1);
Mat distcoeffs;
vector<Mat> rvecs, tvecs;
intrinsic.at<float>(0, 0) = 1;
intrinsic.at<float>(1, 1) = 1;
calibrateCamera(object_points, image_points, img.size(), intrinsic, distcoeffs, rvecs, tvecs);
FileStorage fs1("mycalib.yml", FileStorage::WRITE);
fs1 << "CM1" << intrinsic;
fs1 << "D1" << distcoeffs;
fs1 << "Image Points " << image_points;
printf("calibration done\n");
// Mat imgU;
// while(1)
// {
// cap >> img;
// undistort(img, imgU, intrinsic, distcoeffs);
//
// imshow("image", img);
// imshow("undistort", imgU);
//
// k = waitKey(5);
// if (k == 27)
// {
// break;
// }
// }
// cap.release();
return(0);
}