-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcalibFromFile.cpp
176 lines (149 loc) · 6.02 KB
/
calibFromFile.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// calibFromFile.cpp
// LearningOpenCV
//
// Created by YourtionGuo on 7/28/16.
// Copyright © 2016 Yourtion. All rights reserved.
//
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
int n_boards = 0; //Will be set by input list
const int board_dt = 10;
int board_w;
int board_h;
int main(int argc, char* argv[]) {
board_w = 12;
board_h = 12;
int board_n = board_w * board_h;
CvSize board_sz = cvSize( board_w, board_h );
FILE *fptr = fopen("./data/chessboards.txt","r");
char names[2048];
while(fscanf(fptr,"%s ",names)==1){
n_boards++;
}
rewind(fptr);
cvNamedWindow( "Calibration" );
CvMat* image_points = cvCreateMat(n_boards*board_n,2,CV_32FC1);
CvMat* object_points = cvCreateMat(n_boards*board_n,3,CV_32FC1);
CvMat* point_counts = cvCreateMat(n_boards,1,CV_32SC1);
CvMat* intrinsic_matrix = cvCreateMat(3,3,CV_32FC1);
CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);
IplImage* image = 0;// = cvQueryFrame( capture );
IplImage* gray_image = 0; //for subpixel
CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
int corner_count;
int successes = 0;
int step;
for( int frame=0; frame<n_boards; frame++ ) {
fscanf(fptr,"%s ",names);
if(image){
cvReleaseImage(&image);
image = 0;
}
image = cvLoadImage( names);
if(gray_image == 0 && image) //We'll need this for subpixel accurate stuff
gray_image = cvCreateImage(cvGetSize(image),8,1);
if(!image)
printf("null image\n");
int found = cvFindChessboardCorners(
image,
board_sz,
corners,
&corner_count,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS
);
cvCvtColor(image, gray_image, CV_BGR2GRAY);
cvFindCornerSubPix(gray_image, corners, corner_count,
cvSize(11,11),cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
cvDrawChessboardCorners(image, board_sz, corners, corner_count, found);
cvShowImage( "Calibration", image );
if( corner_count == board_n ) {
step = successes*board_n;
printf("Found = %d for %s\n",found,names);
for( int i=step, j=0; j<board_n; ++i,++j ) {
CV_MAT_ELEM(*image_points, float,i,0) = corners[j].x;
CV_MAT_ELEM(*image_points, float,i,1) = corners[j].y;
CV_MAT_ELEM(*object_points,float,i,0) = j/board_w;
CV_MAT_ELEM(*object_points,float,i,1) = j%board_w;
CV_MAT_ELEM(*object_points,float,i,2) = 0.0f;
}
// CV_MAT_ELEM(*point_counts, int,0,successes) = board_n;
CV_MAT_ELEM(*point_counts, int,successes,0) = board_n;
successes++;
}
int c = cvWaitKey(15);
if(c == 'p') {
c = 0;
while(c != 'p' && c != 27){
c = cvWaitKey(250);
}
}
if(c == 27)
return 0;
}
printf("successes = %d, n_boards=%d\n",successes,n_boards);
CvMat* object_points2 = cvCreateMat(successes*board_n,3,CV_32FC1);
CvMat* image_points2 = cvCreateMat(successes*board_n,2,CV_32FC1);
CvMat* point_counts2 = cvCreateMat(successes,1,CV_32SC1);
for(int i = 0; i<successes*board_n; ++i){
CV_MAT_ELEM(*image_points2, float,i,0) = CV_MAT_ELEM(*image_points, float,i,0);
CV_MAT_ELEM(*image_points2, float,i,1) = CV_MAT_ELEM(*image_points, float,i,1);
CV_MAT_ELEM(*object_points2,float,i,0) = CV_MAT_ELEM(*object_points,float,i,0) ;
CV_MAT_ELEM(*object_points2,float,i,1) = CV_MAT_ELEM(*object_points,float,i,1) ;
CV_MAT_ELEM(*object_points2,float,i,2) = CV_MAT_ELEM(*object_points,float,i,2) ;
}
for(int i=0; i<successes; ++i){
CV_MAT_ELEM(*point_counts2,int,i, 0) = CV_MAT_ELEM(*point_counts, int,i,0);
}
cvReleaseMat(&object_points);
cvReleaseMat(&image_points);
cvReleaseMat(&point_counts);
CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0 ) = 1.0f;
CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1 ) = 1.0f;
printf("cvCalibrateCamera2\n");
cvCalibrateCamera2(
object_points2,
image_points2,
point_counts2,
cvGetSize( image ),
intrinsic_matrix,
distortion_coeffs,
NULL,
NULL,
0
);
// Save our work
cvSave("Intrinsics.xml",intrinsic_matrix);
cvSave("Distortion.xml",distortion_coeffs);
// Load test
CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
IplImage* mapx = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
IplImage* mapy = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
printf("cvInitUndistortMap\n");
cvInitUndistortMap(
intrinsic,
distortion,
mapx,
mapy
);
rewind(fptr);
cvNamedWindow( "Undistort" );
printf("\n\nPress any key to step through the images, ESC to quit\n\n");
while(fscanf(fptr,"%s ",names)==1){
if(image){
cvReleaseImage(&image);
image = 0;
}
image = cvLoadImage( names);
IplImage *t = cvCloneImage(image);
cvShowImage( "Calibration", image );
cvRemap( t, image, mapx, mapy );
cvReleaseImage(&t);
cvShowImage("Undistort", image);
if((cvWaitKey()&0x7F) == 27) break;
}
return 0;
}