Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
654d4476
Commit
654d4476
authored
Jun 13, 2010
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
calibration sample rewritten using the C++ API
parent
be292046
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
254 additions
and
299 deletions
+254
-299
calibration.cpp
samples/cpp/calibration.cpp
+254
-299
No files found.
samples/c/calibration.cpp
→
samples/c
pp
/calibration.cpp
View file @
654d4476
#include "cv.h"
#include "opencv2/core/core.hpp"
#include "highgui.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <time.h>
#include <time.h>
using
namespace
cv
;
using
namespace
std
;
// example command line (for copy-n-paste):
// example command line (for copy-n-paste):
// calibration -w 6 -h 8 -s 2 -n 10 -o camera.yml -op -oe [<list_of_views.txt>]
// calibration -w 6 -h 8 -s 2 -n 10 -o camera.yml -op -oe [<list_of_views.txt>]
...
@@ -22,124 +27,84 @@ other ones will be (those, in which the chessboard pattern will be found)
...
@@ -22,124 +27,84 @@ other ones will be (those, in which the chessboard pattern will be found)
enum
{
DETECTION
=
0
,
CAPTURING
=
1
,
CALIBRATED
=
2
};
enum
{
DETECTION
=
0
,
CAPTURING
=
1
,
CALIBRATED
=
2
};
double
compute_reprojection_error
(
const
CvMat
*
object_points
,
static
double
computeReprojectionErrors
(
const
CvMat
*
rot_vects
,
const
CvMat
*
trans_vects
,
const
vector
<
vector
<
Point3f
>
>&
objectPoints
,
const
CvMat
*
camera_matrix
,
const
CvMat
*
dist_coeffs
,
const
vector
<
vector
<
Point2f
>
>&
imagePoints
,
const
CvMat
*
image_points
,
const
CvMat
*
point_counts
,
const
vector
<
Mat
>&
rvecs
,
const
vector
<
Mat
>&
tvecs
,
CvMat
*
per_view_errors
)
const
Mat
&
cameraMatrix
,
const
Mat
&
distCoeffs
,
vector
<
float
>&
perViewErrors
)
{
{
CvMat
*
image_points2
=
cvCreateMat
(
image_points
->
rows
,
vector
<
Point2f
>
imagePoints2
;
image_points
->
cols
,
image_points
->
type
)
;
int
i
,
totalPoints
=
0
;
int
i
,
image_count
=
rot_vects
->
rows
,
points_so_far
=
0
;
double
totalErr
=
0
,
err
;
double
total_err
=
0
,
err
;
perViewErrors
.
resize
(
objectPoints
.
size
())
;
for
(
i
=
0
;
i
<
image_count
;
i
++
)
for
(
i
=
0
;
i
<
(
int
)
objectPoints
.
size
()
;
i
++
)
{
{
CvMat
object_points_i
,
image_points_i
,
image_points2_i
;
projectPoints
(
Mat
(
objectPoints
[
i
]),
rvecs
[
i
],
tvecs
[
i
],
int
point_count
=
point_counts
->
data
.
i
[
i
];
cameraMatrix
,
distCoeffs
,
imagePoints2
);
CvMat
rot_vect
,
trans_vect
;
err
=
norm
(
Mat
(
imagePoints
[
i
]),
Mat
(
imagePoints2
),
CV_L1
);
int
n
=
(
int
)
objectPoints
[
i
].
size
();
cvGetCols
(
object_points
,
&
object_points_i
,
perViewErrors
[
i
]
=
err
/
n
;
points_so_far
,
points_so_far
+
point_count
);
totalErr
+=
err
;
cvGetCols
(
image_points
,
&
image_points_i
,
totalPoints
+=
n
;
points_so_far
,
points_so_far
+
point_count
);
cvGetCols
(
image_points2
,
&
image_points2_i
,
points_so_far
,
points_so_far
+
point_count
);
points_so_far
+=
point_count
;
cvGetRow
(
rot_vects
,
&
rot_vect
,
i
);
cvGetRow
(
trans_vects
,
&
trans_vect
,
i
);
cvProjectPoints2
(
&
object_points_i
,
&
rot_vect
,
&
trans_vect
,
camera_matrix
,
dist_coeffs
,
&
image_points2_i
,
0
,
0
,
0
,
0
,
0
);
err
=
cvNorm
(
&
image_points_i
,
&
image_points2_i
,
CV_L1
);
if
(
per_view_errors
)
per_view_errors
->
data
.
db
[
i
]
=
err
/
point_count
;
total_err
+=
err
;
}
}
cvReleaseMat
(
&
image_points2
);
return
totalErr
/
totalPoints
;
return
total_err
/
points_so_far
;
}
}
static
void
calcChessboardCorners
(
Size
boardSize
,
float
squareSize
,
vector
<
Point3f
>&
corners
)
int
run_calibration
(
CvSeq
*
image_points_seq
,
CvSize
img_size
,
CvSize
board_size
,
float
square_size
,
float
aspect_ratio
,
int
flags
,
CvMat
*
camera_matrix
,
CvMat
*
dist_coeffs
,
CvMat
**
extr_params
,
CvMat
**
reproj_errs
,
double
*
avg_reproj_err
)
{
{
int
code
;
corners
.
resize
(
0
);
int
image_count
=
image_points_seq
->
total
;
int
point_count
=
board_size
.
width
*
board_size
.
height
;
for
(
int
i
=
0
;
i
<
boardSize
.
height
;
i
++
)
CvMat
*
image_points
=
cvCreateMat
(
1
,
image_count
*
point_count
,
CV_32FC2
);
for
(
int
j
=
0
;
j
<
boardSize
.
width
;
j
++
)
CvMat
*
object_points
=
cvCreateMat
(
1
,
image_count
*
point_count
,
CV_32FC3
);
corners
.
push_back
(
Point3f
(
float
(
j
*
squareSize
),
CvMat
*
point_counts
=
cvCreateMat
(
1
,
image_count
,
CV_32SC1
);
float
(
i
*
squareSize
),
0
));
CvMat
rot_vects
,
trans_vects
;
}
int
i
,
j
,
k
;
CvSeqReader
reader
;
cvStartReadSeq
(
image_points_seq
,
&
reader
);
// initialize arrays of points
for
(
i
=
0
;
i
<
image_count
;
i
++
)
{
CvPoint2D32f
*
src_img_pt
=
(
CvPoint2D32f
*
)
reader
.
ptr
;
CvPoint2D32f
*
dst_img_pt
=
((
CvPoint2D32f
*
)
image_points
->
data
.
fl
)
+
i
*
point_count
;
CvPoint3D32f
*
obj_pt
=
((
CvPoint3D32f
*
)
object_points
->
data
.
fl
)
+
i
*
point_count
;
for
(
j
=
0
;
j
<
board_size
.
height
;
j
++
)
for
(
k
=
0
;
k
<
board_size
.
width
;
k
++
)
{
*
obj_pt
++
=
cvPoint3D32f
(
j
*
square_size
,
k
*
square_size
,
0
);
*
dst_img_pt
++
=
*
src_img_pt
++
;
}
CV_NEXT_SEQ_ELEM
(
image_points_seq
->
elem_size
,
reader
);
}
cvSet
(
point_counts
,
cvScalar
(
point_count
)
);
*
extr_params
=
cvCreateMat
(
image_count
,
6
,
CV_32FC1
);
cvGetCols
(
*
extr_params
,
&
rot_vects
,
0
,
3
);
cvGetCols
(
*
extr_params
,
&
trans_vects
,
3
,
6
);
cvZero
(
camera_matrix
);
cvZero
(
dist_coeffs
);
static
bool
runCalibration
(
vector
<
vector
<
Point2f
>
>
imagePoints
,
Size
imageSize
,
Size
boardSize
,
float
squareSize
,
float
aspectRatio
,
int
flags
,
Mat
&
cameraMatrix
,
Mat
&
distCoeffs
,
vector
<
Mat
>&
rvecs
,
vector
<
Mat
>&
tvecs
,
vector
<
float
>&
reprojErrs
,
double
&
totalAvgErr
)
{
cameraMatrix
=
Mat
::
eye
(
3
,
3
,
CV_64F
);
if
(
flags
&
CV_CALIB_FIX_ASPECT_RATIO
)
if
(
flags
&
CV_CALIB_FIX_ASPECT_RATIO
)
{
cameraMatrix
.
at
<
double
>
(
0
,
0
)
=
aspectRatio
;
camera_matrix
->
data
.
db
[
0
]
=
aspect_ratio
;
camera_matrix
->
data
.
db
[
4
]
=
1.
;
distCoeffs
=
Mat
::
zeros
(
5
,
1
,
CV_64F
);
}
vector
<
vector
<
Point3f
>
>
objectPoints
(
1
);
cvCalibrateCamera2
(
object_points
,
image_points
,
point_counts
,
calcChessboardCorners
(
boardSize
,
squareSize
,
objectPoints
[
0
]);
img_size
,
camera_matrix
,
dist_coeffs
,
for
(
size_t
i
=
1
;
i
<
imagePoints
.
size
();
i
++
)
&
rot_vects
,
&
trans_vects
,
flags
);
objectPoints
.
push_back
(
objectPoints
[
0
]);
code
=
cvCheckArr
(
camera_matrix
,
CV_CHECK_QUIET
)
&&
calibrateCamera
(
objectPoints
,
imagePoints
,
imageSize
,
cameraMatrix
,
cvCheckArr
(
dist_coeffs
,
CV_CHECK_QUIET
)
&&
distCoeffs
,
rvecs
,
tvecs
,
flags
);
cvCheckArr
(
*
extr_params
,
CV_CHECK_QUIET
);
bool
ok
=
checkRange
(
cameraMatrix
,
CV_CHECK_QUIET
)
&&
*
reproj_errs
=
cvCreateMat
(
1
,
image_count
,
CV_64FC1
);
checkRange
(
distCoeffs
,
CV_CHECK_QUIET
);
*
avg_reproj_err
=
compute_reprojection_error
(
object_points
,
&
rot_vects
,
&
trans_vects
,
totalAvgErr
=
computeReprojectionErrors
(
objectPoints
,
imagePoints
,
camera_matrix
,
dist_coeffs
,
image_points
,
point_counts
,
*
reproj_errs
);
rvecs
,
tvecs
,
cameraMatrix
,
distCoeffs
,
reprojErrs
);
cvReleaseMat
(
&
object_points
);
cvReleaseMat
(
&
image_points
);
cvReleaseMat
(
&
point_counts
);
return
code
;
return
ok
;
}
}
void
save_camera_params
(
const
char
*
out_filename
,
int
image_count
,
CvSize
img_size
,
void
saveCameraParams
(
const
string
&
filename
,
CvSize
board_size
,
float
square_size
,
Size
imageSize
,
Size
boardSize
,
float
aspect_ratio
,
int
flags
,
float
squareSize
,
float
aspectRatio
,
int
flags
,
const
CvMat
*
camera_matrix
,
CvMat
*
dist_coeffs
,
const
Mat
&
cameraMatrix
,
const
Mat
&
distCoeffs
,
const
CvMat
*
extr_params
,
const
CvSeq
*
image_points_seq
,
const
vector
<
Mat
>&
rvecs
,
const
vector
<
Mat
>&
tvecs
,
const
CvMat
*
reproj_errs
,
double
avg_reproj_err
)
const
vector
<
float
>&
reprojErrs
,
const
vector
<
vector
<
Point2f
>
>&
imagePoints
,
double
totalAvgErr
)
{
{
CvFileStorage
*
fs
=
cvOpenFileStorage
(
out_filename
,
0
,
CV_STORAGE_
WRITE
);
FileStorage
fs
(
filename
,
FileStorage
::
WRITE
);
time_t
t
;
time_t
t
;
time
(
&
t
);
time
(
&
t
);
...
@@ -147,88 +112,138 @@ void save_camera_params( const char* out_filename, int image_count, CvSize img_s
...
@@ -147,88 +112,138 @@ void save_camera_params( const char* out_filename, int image_count, CvSize img_s
char
buf
[
1024
];
char
buf
[
1024
];
strftime
(
buf
,
sizeof
(
buf
)
-
1
,
"%c"
,
t2
);
strftime
(
buf
,
sizeof
(
buf
)
-
1
,
"%c"
,
t2
);
cvWriteString
(
fs
,
"calibration_time"
,
buf
)
;
fs
<<
"calibration_time"
<<
buf
;
cvWriteInt
(
fs
,
"image_count"
,
image_count
);
if
(
!
rvecs
.
empty
()
||
!
reprojErrs
.
empty
()
)
cvWriteInt
(
fs
,
"image_width"
,
img_size
.
width
);
fs
<<
"nframes"
<<
(
int
)
std
::
max
(
rvecs
.
size
(),
reprojErrs
.
size
());
cvWriteInt
(
fs
,
"image_height"
,
img_size
.
height
);
fs
<<
"image_width"
<<
imageSize
.
width
;
cvWriteInt
(
fs
,
"board_width"
,
board_size
.
width
);
fs
<<
"image_height"
<<
imageSize
.
height
;
cvWriteInt
(
fs
,
"board_height"
,
board_size
.
height
);
fs
<<
"board_width"
<<
boardSize
.
width
;
cvWriteReal
(
fs
,
"square_size"
,
square_size
);
fs
<<
"board_height"
<<
boardSize
.
height
;
fs
<<
"squareSize"
<<
squareSize
;
if
(
flags
&
CV_CALIB_FIX_ASPECT_RATIO
)
if
(
flags
&
CV_CALIB_FIX_ASPECT_RATIO
)
cvWriteReal
(
fs
,
"aspect_ratio"
,
aspect_ratio
)
;
fs
<<
"aspectRatio"
<<
aspectRatio
;
if
(
flags
!=
0
)
if
(
flags
!=
0
)
{
{
sprintf
(
buf
,
"flags: %s%s%s%s"
,
sprintf
(
buf
,
"flags: %s%s%s%s"
,
flags
&
CV_CALIB_USE_INTRINSIC_GUESS
?
"+use_intrinsic_guess"
:
""
,
flags
&
CV_CALIB_USE_INTRINSIC_GUESS
?
"+use_intrinsic_guess"
:
""
,
flags
&
CV_CALIB_FIX_ASPECT_RATIO
?
"+fix_aspect
_r
atio"
:
""
,
flags
&
CV_CALIB_FIX_ASPECT_RATIO
?
"+fix_aspect
R
atio"
:
""
,
flags
&
CV_CALIB_FIX_PRINCIPAL_POINT
?
"+fix_principal_point"
:
""
,
flags
&
CV_CALIB_FIX_PRINCIPAL_POINT
?
"+fix_principal_point"
:
""
,
flags
&
CV_CALIB_ZERO_TANGENT_DIST
?
"+zero_tangent_dist"
:
""
);
flags
&
CV_CALIB_ZERO_TANGENT_DIST
?
"+zero_tangent_dist"
:
""
);
cvWriteComment
(
fs
,
buf
,
0
);
cvWriteComment
(
*
fs
,
buf
,
0
);
}
}
cvWriteInt
(
fs
,
"flags"
,
flags
)
;
fs
<<
"flags"
<<
flags
;
cvWrite
(
fs
,
"camera_matrix"
,
camera_matrix
)
;
fs
<<
"camera_matrix"
<<
cameraMatrix
;
cvWrite
(
fs
,
"distortion_coefficients"
,
dist_coeffs
)
;
fs
<<
"distortion_coefficients"
<<
distCoeffs
;
cvWriteReal
(
fs
,
"avg_reprojection_error"
,
avg_reproj_err
)
;
fs
<<
"avg_reprojection_error"
<<
totalAvgErr
;
if
(
reproj_errs
)
if
(
!
reprojErrs
.
empty
()
)
cvWrite
(
fs
,
"per_view_reprojection_errors"
,
reproj_errs
);
fs
<<
"per_view_reprojection_errors"
<<
Mat
(
reprojErrs
);
if
(
extr_params
)
if
(
!
rvecs
.
empty
()
&&
!
tvecs
.
empty
()
)
{
{
cvWriteComment
(
fs
,
"a set of 6-tuples (rotation vector + translation vector) for each view"
,
0
);
Mat
bigmat
(
rvecs
.
size
(),
6
,
CV_32F
);
cvWrite
(
fs
,
"extrinsic_parameters"
,
extr_params
);
for
(
size_t
i
=
0
;
i
<
rvecs
.
size
();
i
++
)
{
Mat
r
=
bigmat
(
Range
(
i
,
i
+
1
),
Range
(
0
,
3
));
Mat
t
=
bigmat
(
Range
(
i
,
i
+
1
),
Range
(
3
,
6
));
rvecs
[
i
].
copyTo
(
r
);
tvecs
[
i
].
copyTo
(
t
);
}
cvWriteComment
(
*
fs
,
"a set of 6-tuples (rotation vector + translation vector) for each view"
,
0
);
fs
<<
"extrinsic_parameters"
<<
bigmat
;
}
}
if
(
image_points_seq
)
if
(
!
imagePoints
.
empty
()
)
{
{
cvWriteComment
(
fs
,
"the array of board corners projections used for calibration"
,
0
);
Mat
imagePtMat
(
imagePoints
.
size
(),
imagePoints
[
0
].
size
(),
CV_32FC2
);
assert
(
image_points_seq
->
total
==
image_count
);
for
(
size_t
i
=
0
;
i
<
imagePoints
.
size
();
i
++
)
CvMat
*
image_points
=
cvCreateMat
(
1
,
image_count
*
board_size
.
width
*
board_size
.
height
,
CV_32FC2
);
{
cvCvtSeqToArray
(
image_points_seq
,
image_points
->
data
.
fl
);
Mat
r
=
imagePtMat
.
row
(
i
).
reshape
(
2
,
imagePtMat
.
cols
);
Mat
(
imagePoints
[
i
]).
copyTo
(
r
);
}
fs
<<
"image_points"
<<
imagePtMat
;
}
}
cvWrite
(
fs
,
"image_points"
,
image_points
);
bool
readStringList
(
const
string
&
filename
,
vector
<
string
>&
l
)
cvReleaseMat
(
&
image_points
);
{
l
.
resize
(
0
);
FILE
*
f
=
fopen
(
filename
.
c_str
(),
"rt"
);
if
(
!
f
)
return
false
;
for
(;;)
{
char
buf
[
1000
];
if
(
!
fgets
(
buf
,
sizeof
(
buf
)
-
2
,
f
))
break
;
char
*
ptr
=
strchr
(
buf
,
'\n'
);
if
(
ptr
)
*
ptr
=
'\0'
;
if
(
buf
[
0
]
!=
'\0'
&&
buf
[
0
]
!=
'#'
)
l
.
push_back
(
string
(
buf
));
}
}
fclose
(
f
);
return
true
;
}
cvReleaseFileStorage
(
&
fs
);
bool
runAndSave
(
const
string
&
outputFilename
,
const
vector
<
vector
<
Point2f
>
>&
imagePoints
,
Size
imageSize
,
Size
boardSize
,
float
squareSize
,
float
aspectRatio
,
int
flags
,
Mat
&
cameraMatrix
,
Mat
&
distCoeffs
,
bool
writeExtrinsics
,
bool
writePoints
)
{
vector
<
Mat
>
rvecs
,
tvecs
;
vector
<
float
>
reprojErrs
;
double
totalAvgErr
=
0
;
bool
ok
=
runCalibration
(
imagePoints
,
imageSize
,
boardSize
,
squareSize
,
aspectRatio
,
flags
,
cameraMatrix
,
distCoeffs
,
rvecs
,
tvecs
,
reprojErrs
,
totalAvgErr
);
printf
(
"%s. avg reprojection error = %.2f
\n
"
,
ok
?
"Calibration succeeded"
:
"Calibration failed"
,
totalAvgErr
);
if
(
ok
)
saveCameraParams
(
outputFilename
,
imageSize
,
boardSize
,
squareSize
,
aspectRatio
,
flags
,
cameraMatrix
,
distCoeffs
,
writeExtrinsics
?
rvecs
:
vector
<
Mat
>
(),
writeExtrinsics
?
tvecs
:
vector
<
Mat
>
(),
writeExtrinsics
?
reprojErrs
:
vector
<
float
>
(),
writePoints
?
imagePoints
:
vector
<
vector
<
Point2f
>
>
(),
totalAvgErr
);
return
ok
;
}
}
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
CvSize
board_size
=
{
0
,
0
};
Size
boardSize
,
imageSize
;
float
square_size
=
1.
f
,
aspect_ratio
=
1.
f
;
float
squareSize
=
1.
f
,
aspectRatio
=
1.
f
;
const
char
*
out_filename
=
"out_camera_data.yml"
;
Mat
cameraMatrix
,
distCoeffs
;
const
char
*
input_filename
=
0
;
const
char
*
outputFilename
=
"out_camera_data.yml"
;
int
i
,
image_count
=
10
;
const
char
*
inputFilename
=
0
;
int
write_extrinsics
=
0
,
write_points
=
0
;
int
i
,
nframes
=
10
;
bool
writeExtrinsics
=
false
,
writePoints
=
false
;
bool
undistortImage
=
false
;
int
flags
=
0
;
int
flags
=
0
;
CvCapture
*
capture
=
0
;
VideoCapture
capture
;
FILE
*
f
=
0
;
bool
flipVertical
=
false
;
char
imagename
[
1024
];
CvMemStorage
*
storage
;
CvSeq
*
image_points_seq
=
0
;
int
elem_size
,
flip_vertical
=
0
;
int
delay
=
1000
;
int
delay
=
1000
;
clock_t
prev_timestamp
=
0
;
clock_t
prevTimestamp
=
0
;
CvPoint2D32f
*
image_points_buf
=
0
;
CvFont
font
=
cvFont
(
1
,
1
);
double
_camera
[
9
],
_dist_coeffs
[
4
];
CvMat
camera
=
cvMat
(
3
,
3
,
CV_64F
,
_camera
);
CvMat
dist_coeffs
=
cvMat
(
1
,
4
,
CV_64F
,
_dist_coeffs
);
CvMat
*
extr_params
=
0
,
*
reproj_errs
=
0
;
double
avg_reproj_err
=
0
;
int
mode
=
DETECTION
;
int
mode
=
DETECTION
;
int
undistort_image
=
0
;
CvSize
img_size
=
{
0
,
0
};
int
cameraId
=
0
;
int
cameraId
=
0
;
vector
<
vector
<
Point2f
>
>
imagePoints
;
const
char
*
live_capture_help
=
vector
<
string
>
imageList
;
const
char
*
liveCaptureHelp
=
"When the live video from camera is used as input, the following hot-keys may be used:
\n
"
"When the live video from camera is used as input, the following hot-keys may be used:
\n
"
" <ESC>, 'q' - quit the program
\n
"
" <ESC>, 'q' - quit the program
\n
"
" 'g' - start capturing images
\n
"
" 'g' - start capturing images
\n
"
...
@@ -245,12 +260,12 @@ int main( int argc, char** argv )
...
@@ -245,12 +260,12 @@ int main( int argc, char** argv )
" # of board views actually available)
\n
"
" # of board views actually available)
\n
"
" [-d <delay>] # a minimum delay in ms between subsequent attempts to capture a next view
\n
"
" [-d <delay>] # a minimum delay in ms between subsequent attempts to capture a next view
\n
"
" # (used only for video capturing)
\n
"
" # (used only for video capturing)
\n
"
" [-s <square
_s
ize>] # square size in some user-defined units (1 by default)
\n
"
" [-s <square
S
ize>] # square size in some user-defined units (1 by default)
\n
"
" [-o <out_camera_params>] # the output filename for intrinsic [and extrinsic] parameters
\n
"
" [-o <out_camera_params>] # the output filename for intrinsic [and extrinsic] parameters
\n
"
" [-op] # write detected feature points
\n
"
" [-op] # write detected feature points
\n
"
" [-oe] # write extrinsic parameters
\n
"
" [-oe] # write extrinsic parameters
\n
"
" [-zt] # assume zero tangential distortion
\n
"
" [-zt] # assume zero tangential distortion
\n
"
" [-a <aspect
_r
atio>] # fix aspect ratio (fx/fy)
\n
"
" [-a <aspect
R
atio>] # fix aspect ratio (fx/fy)
\n
"
" [-p] # fix the principal point at the center
\n
"
" [-p] # fix the principal point at the center
\n
"
" [-v] # flip the captured images around the horizontal axis
\n
"
" [-v] # flip the captured images around the horizontal axis
\n
"
" [input_data] # input data, one of the following:
\n
"
" [input_data] # input data, one of the following:
\n
"
...
@@ -258,7 +273,7 @@ int main( int argc, char** argv )
...
@@ -258,7 +273,7 @@ int main( int argc, char** argv )
" # - name of video file with a video of the board
\n
"
" # - name of video file with a video of the board
\n
"
" # if input_data not specified, a live view from the camera is used
\n
"
" # if input_data not specified, a live view from the camera is used
\n
"
"
\n
"
);
"
\n
"
);
printf
(
"%s"
,
live
_capture_h
elp
);
printf
(
"%s"
,
live
CaptureH
elp
);
return
0
;
return
0
;
}
}
...
@@ -267,27 +282,27 @@ int main( int argc, char** argv )
...
@@ -267,27 +282,27 @@ int main( int argc, char** argv )
const
char
*
s
=
argv
[
i
];
const
char
*
s
=
argv
[
i
];
if
(
strcmp
(
s
,
"-w"
)
==
0
)
if
(
strcmp
(
s
,
"-w"
)
==
0
)
{
{
if
(
sscanf
(
argv
[
++
i
],
"%u"
,
&
board
_size
.
width
)
!=
1
||
board_s
ize
.
width
<=
0
)
if
(
sscanf
(
argv
[
++
i
],
"%u"
,
&
board
Size
.
width
)
!=
1
||
boardS
ize
.
width
<=
0
)
return
fprintf
(
stderr
,
"Invalid board width
\n
"
),
-
1
;
return
fprintf
(
stderr
,
"Invalid board width
\n
"
),
-
1
;
}
}
else
if
(
strcmp
(
s
,
"-h"
)
==
0
)
else
if
(
strcmp
(
s
,
"-h"
)
==
0
)
{
{
if
(
sscanf
(
argv
[
++
i
],
"%u"
,
&
board
_size
.
height
)
!=
1
||
board_s
ize
.
height
<=
0
)
if
(
sscanf
(
argv
[
++
i
],
"%u"
,
&
board
Size
.
height
)
!=
1
||
boardS
ize
.
height
<=
0
)
return
fprintf
(
stderr
,
"Invalid board height
\n
"
),
-
1
;
return
fprintf
(
stderr
,
"Invalid board height
\n
"
),
-
1
;
}
}
else
if
(
strcmp
(
s
,
"-s"
)
==
0
)
else
if
(
strcmp
(
s
,
"-s"
)
==
0
)
{
{
if
(
sscanf
(
argv
[
++
i
],
"%f"
,
&
square
_size
)
!=
1
||
square_s
ize
<=
0
)
if
(
sscanf
(
argv
[
++
i
],
"%f"
,
&
square
Size
)
!=
1
||
squareS
ize
<=
0
)
return
fprintf
(
stderr
,
"Invalid board square width
\n
"
),
-
1
;
return
fprintf
(
stderr
,
"Invalid board square width
\n
"
),
-
1
;
}
}
else
if
(
strcmp
(
s
,
"-n"
)
==
0
)
else
if
(
strcmp
(
s
,
"-n"
)
==
0
)
{
{
if
(
sscanf
(
argv
[
++
i
],
"%u"
,
&
image_count
)
!=
1
||
image_count
<=
3
)
if
(
sscanf
(
argv
[
++
i
],
"%u"
,
&
nframes
)
!=
1
||
nframes
<=
3
)
return
printf
(
"Invalid number of images
\n
"
),
-
1
;
return
printf
(
"Invalid number of images
\n
"
),
-
1
;
}
}
else
if
(
strcmp
(
s
,
"-a"
)
==
0
)
else
if
(
strcmp
(
s
,
"-a"
)
==
0
)
{
{
if
(
sscanf
(
argv
[
++
i
],
"%f"
,
&
aspect
_ratio
)
!=
1
||
aspect_r
atio
<=
0
)
if
(
sscanf
(
argv
[
++
i
],
"%f"
,
&
aspect
Ratio
)
!=
1
||
aspectR
atio
<=
0
)
return
printf
(
"Invalid aspect ratio
\n
"
),
-
1
;
return
printf
(
"Invalid aspect ratio
\n
"
),
-
1
;
}
}
else
if
(
strcmp
(
s
,
"-d"
)
==
0
)
else
if
(
strcmp
(
s
,
"-d"
)
==
0
)
...
@@ -297,11 +312,11 @@ int main( int argc, char** argv )
...
@@ -297,11 +312,11 @@ int main( int argc, char** argv )
}
}
else
if
(
strcmp
(
s
,
"-op"
)
==
0
)
else
if
(
strcmp
(
s
,
"-op"
)
==
0
)
{
{
write
_p
oints
=
1
;
write
P
oints
=
1
;
}
}
else
if
(
strcmp
(
s
,
"-oe"
)
==
0
)
else
if
(
strcmp
(
s
,
"-oe"
)
==
0
)
{
{
write
_e
xtrinsics
=
1
;
write
E
xtrinsics
=
1
;
}
}
else
if
(
strcmp
(
s
,
"-zt"
)
==
0
)
else
if
(
strcmp
(
s
,
"-zt"
)
==
0
)
{
{
...
@@ -313,200 +328,140 @@ int main( int argc, char** argv )
...
@@ -313,200 +328,140 @@ int main( int argc, char** argv )
}
}
else
if
(
strcmp
(
s
,
"-v"
)
==
0
)
else
if
(
strcmp
(
s
,
"-v"
)
==
0
)
{
{
flip
_v
ertical
=
1
;
flip
V
ertical
=
1
;
}
}
else
if
(
strcmp
(
s
,
"-o"
)
==
0
)
else
if
(
strcmp
(
s
,
"-o"
)
==
0
)
{
{
out
_f
ilename
=
argv
[
++
i
];
out
putF
ilename
=
argv
[
++
i
];
}
}
else
if
(
s
[
0
]
!=
'-'
)
else
if
(
s
[
0
]
!=
'-'
)
{
{
if
(
isdigit
(
s
[
0
])
)
if
(
isdigit
(
s
[
0
])
)
sscanf
(
s
,
"%d"
,
&
cameraId
);
sscanf
(
s
,
"%d"
,
&
cameraId
);
else
else
input
_f
ilename
=
s
;
input
F
ilename
=
s
;
}
}
else
else
return
fprintf
(
stderr
,
"Unknown option %s"
,
s
),
-
1
;
return
fprintf
(
stderr
,
"Unknown option %s"
,
s
),
-
1
;
}
}
if
(
input
_f
ilename
)
if
(
input
F
ilename
)
{
{
capture
=
cvCreateFileCapture
(
input_filename
);
capture
.
open
(
inputFilename
);
if
(
!
capture
)
if
(
!
capture
.
isOpened
()
&&
!
readStringList
(
inputFilename
,
imageList
)
)
{
{
f
=
fopen
(
input_filename
,
"rt"
);
fprintf
(
stderr
,
"The input file could not be opened
\n
"
);
if
(
!
f
)
return
-
1
;
return
fprintf
(
stderr
,
"The input file could not be opened
\n
"
),
-
1
;
image_count
=
-
1
;
}
}
mode
=
CAPTURING
;
mode
=
CAPTURING
;
}
}
else
else
capture
=
cvCreateCameraCapture
(
cameraId
);
capture
.
open
(
cameraId
);
if
(
!
capture
&&
!
f
)
if
(
!
capture
.
isOpened
()
&&
imageList
.
empty
()
)
return
fprintf
(
stderr
,
"Could not initialize video capture
\n
"
),
-
2
;
return
fprintf
(
stderr
,
"Could not initialize video capture
\n
"
),
-
2
;
if
(
!
imageList
.
empty
()
)
nframes
=
(
int
)
imageList
.
size
();
if
(
capture
)
if
(
capture
.
isOpened
()
)
printf
(
"%s"
,
live_capture_help
);
printf
(
"%s"
,
liveCaptureHelp
);
elem_size
=
board_size
.
width
*
board_size
.
height
*
sizeof
(
image_points_buf
[
0
]);
storage
=
cvCreateMemStorage
(
MAX
(
elem_size
*
4
,
1
<<
16
));
image_points_buf
=
(
CvPoint2D32f
*
)
cvAlloc
(
elem_size
);
image_points_seq
=
cvCreateSeq
(
0
,
sizeof
(
CvSeq
),
elem_size
,
storage
);
cvN
amedWindow
(
"Image View"
,
1
);
n
amedWindow
(
"Image View"
,
1
);
for
(
;;
)
for
(
i
=
0
;;
i
++
)
{
{
IplImage
*
view
=
0
,
*
view_gray
=
0
;
Mat
view
,
viewGray
;
int
count
=
0
,
found
,
blink
=
0
;
bool
blink
=
false
;
CvPoint
text_origin
;
CvSize
text_size
=
{
0
,
0
};
int
base_line
=
0
;
char
s
[
100
];
int
key
;
if
(
f
&&
fgets
(
imagename
,
sizeof
(
imagename
)
-
2
,
f
))
if
(
capture
.
isOpened
()
)
{
int
l
=
strlen
(
imagename
);
if
(
l
>
0
&&
imagename
[
l
-
1
]
==
'\n'
)
imagename
[
--
l
]
=
'\0'
;
if
(
l
>
0
)
{
if
(
imagename
[
0
]
==
'#'
)
continue
;
view
=
cvLoadImage
(
imagename
,
1
);
}
}
else
if
(
capture
)
{
{
IplImage
*
view0
=
cvQueryFrame
(
capture
);
Mat
view0
;
if
(
view0
)
capture
>>
view0
;
{
view0
.
copyTo
(
view
);
view
=
cvCreateImage
(
cvGetSize
(
view0
),
IPL_DEPTH_8U
,
view0
->
nChannels
);
if
(
view0
->
origin
==
IPL_ORIGIN_BL
)
cvFlip
(
view0
,
view
,
0
);
else
cvCopy
(
view0
,
view
);
}
}
}
else
if
(
i
<
(
int
)
imageList
.
size
()
)
if
(
!
view
)
view
=
imread
(
imageList
[
i
],
1
);
if
(
!
view
.
data
)
{
{
if
(
image
_points_seq
->
total
>
0
)
if
(
image
Points
.
size
()
>
0
)
{
runAndSave
(
outputFilename
,
imagePoints
,
imageSize
,
image_count
=
image_points_seq
->
total
;
boardSize
,
squareSize
,
aspectRatio
,
goto
calibrate
;
flags
,
cameraMatrix
,
distCoeffs
,
}
writeExtrinsics
,
writePoints
);
break
;
break
;
}
}
imageSize
=
view
.
size
();
if
(
flip
_v
ertical
)
if
(
flip
V
ertical
)
cvF
lip
(
view
,
view
,
0
);
f
lip
(
view
,
view
,
0
);
img_size
=
cvGetSize
(
view
);
vector
<
Point2f
>
pointbuf
;
found
=
cvFindChessboardCorners
(
view
,
board_size
,
bool
found
=
findChessboardCorners
(
view
,
boardSize
,
pointbuf
,
CV_CALIB_CB_ADAPTIVE_THRESH
);
image_points_buf
,
&
count
,
CV_CALIB_CB_ADAPTIVE_THRESH
);
#if 1
// improve the found corners' coordinate accuracy
// improve the found corners' coordinate accuracy
view_gray
=
cvCreateImage
(
cvGetSize
(
view
),
8
,
1
);
cvtColor
(
view
,
viewGray
,
CV_BGR2GRAY
);
cvCvtColor
(
view
,
view_gray
,
CV_BGR2GRAY
);
cornerSubPix
(
viewGray
,
pointbuf
,
Size
(
11
,
11
),
cvFindCornerSubPix
(
view_gray
,
image_points_buf
,
count
,
cvSize
(
11
,
11
),
Size
(
-
1
,
-
1
),
TermCriteria
(
CV_TERMCRIT_EPS
+
CV_TERMCRIT_ITER
,
30
,
0.1
));
cvSize
(
-
1
,
-
1
),
cvTermCriteria
(
CV_TERMCRIT_EPS
+
CV_TERMCRIT_ITER
,
30
,
0.1
));
cvReleaseImage
(
&
view_gray
);
if
(
mode
==
CAPTURING
&&
found
&&
#endif
(
!
capture
.
isOpened
()
||
clock
()
-
prevTimestamp
>
delay
*
1e-3
*
CLOCKS_PER_SEC
)
)
if
(
mode
==
CAPTURING
&&
found
&&
(
f
||
clock
()
-
prev_timestamp
>
delay
*
1e-3
*
CLOCKS_PER_SEC
)
)
{
{
cvSeqPush
(
image_points_seq
,
image_points_buf
);
imagePoints
.
push_back
(
pointbuf
);
prev_timestamp
=
clock
();
prevTimestamp
=
clock
();
blink
=
!
f
;
blink
=
capture
.
isOpened
();
#if 1
if
(
capture
)
{
sprintf
(
imagename
,
"view%03d.png"
,
image_points_seq
->
total
-
1
);
cvSaveImage
(
imagename
,
view
);
}
#endif
}
}
cvDrawChessboardCorners
(
view
,
board_size
,
image_points_buf
,
count
,
found
);
drawChessboardCorners
(
view
,
boardSize
,
Mat
(
pointbuf
)
,
found
);
cvGetTextSize
(
"100/100"
,
&
font
,
&
text_size
,
&
base_line
);
string
msg
=
mode
==
CAPTURING
?
"100/100"
:
text_origin
.
x
=
view
->
width
-
text_size
.
width
-
10
;
mode
==
CALIBRATED
?
"Calibrated"
:
"Press 'g' to start"
;
text_origin
.
y
=
view
->
height
-
base_line
-
10
;
int
baseLine
=
0
;
Size
textSize
=
getTextSize
(
msg
,
1
,
1
,
1
,
&
baseLine
);
Point
textOrigin
(
view
.
cols
-
textSize
.
width
-
10
,
view
.
rows
-
baseLine
-
10
);
if
(
mode
==
CAPTURING
)
if
(
mode
==
CAPTURING
)
{
msg
=
format
(
"%d/%d"
,
(
int
)
imagePoints
.
size
(),
nframes
);
if
(
image_count
>
0
)
sprintf
(
s
,
"%d/%d"
,
image_points_seq
?
image_points_seq
->
total
:
0
,
image_count
);
else
sprintf
(
s
,
"%d/?"
,
image_points_seq
?
image_points_seq
->
total
:
0
);
}
else
if
(
mode
==
CALIBRATED
)
sprintf
(
s
,
"Calibrated"
);
else
sprintf
(
s
,
"Press 'g' to start"
);
cvPutText
(
view
,
s
,
text_origin
,
&
font
,
mode
!=
CALIBRATED
?
putText
(
view
,
msg
,
textOrigin
,
1
,
1
,
CV_RGB
(
255
,
0
,
0
)
:
CV_RGB
(
0
,
255
,
0
));
mode
!=
CALIBRATED
?
Scalar
(
0
,
0
,
255
)
:
Scalar
(
0
,
255
,
0
));
if
(
blink
)
if
(
blink
)
cvNot
(
view
,
view
);
bitwise_not
(
view
,
view
);
if
(
mode
==
CALIBRATED
&&
undistort
_i
mage
)
if
(
mode
==
CALIBRATED
&&
undistort
I
mage
)
{
{
IplImage
*
t
=
cvCloneImage
(
view
);
Mat
temp
=
view
.
clone
();
cvUndistort2
(
t
,
view
,
&
camera
,
&
dist_coeffs
);
undistort
(
temp
,
view
,
cameraMatrix
,
distCoeffs
);
cvReleaseImage
(
&
t
);
}
}
cvShowImage
(
"Image View"
,
view
);
imshow
(
"Image View"
,
view
);
key
=
cvWaitKey
(
capture
?
50
:
500
);
int
key
=
waitKey
(
capture
.
isOpened
()
?
50
:
500
);
if
(
key
==
27
)
if
(
(
key
&
255
)
==
27
)
break
;
break
;
if
(
key
==
'u'
&&
mode
==
CALIBRATED
)
if
(
key
==
'u'
&&
mode
==
CALIBRATED
)
undistort
_image
=
!
undistort_i
mage
;
undistort
Image
=
!
undistortI
mage
;
if
(
capture
&&
key
==
'g'
)
if
(
capture
.
isOpened
()
&&
key
==
'g'
)
{
{
mode
=
CAPTURING
;
mode
=
CAPTURING
;
cvClearMemStorage
(
storage
);
imagePoints
.
clear
();
image_points_seq
=
cvCreateSeq
(
0
,
sizeof
(
CvSeq
),
elem_size
,
storage
);
}
}
if
(
mode
==
CAPTURING
&&
(
unsigned
)
image_points_seq
->
total
>=
(
unsigned
)
image_count
)
if
(
mode
==
CAPTURING
&&
imagePoints
.
size
()
>=
(
unsigned
)
nframes
)
{
{
calibrate:
if
(
runAndSave
(
outputFilename
,
imagePoints
,
imageSize
,
cvReleaseMat
(
&
extr_params
);
boardSize
,
squareSize
,
aspectRatio
,
cvReleaseMat
(
&
reproj_errs
);
flags
,
cameraMatrix
,
distCoeffs
,
int
code
=
run_calibration
(
image_points_seq
,
img_size
,
board_size
,
writeExtrinsics
,
writePoints
))
square_size
,
aspect_ratio
,
flags
,
&
camera
,
&
dist_coeffs
,
&
extr_params
,
&
reproj_errs
,
&
avg_reproj_err
);
// save camera parameters in any case, to catch Inf's/NaN's
save_camera_params
(
out_filename
,
image_count
,
img_size
,
board_size
,
square_size
,
aspect_ratio
,
flags
,
&
camera
,
&
dist_coeffs
,
write_extrinsics
?
extr_params
:
0
,
write_points
?
image_points_seq
:
0
,
reproj_errs
,
avg_reproj_err
);
if
(
code
)
mode
=
CALIBRATED
;
mode
=
CALIBRATED
;
else
else
mode
=
DETECTION
;
mode
=
DETECTION
;
}
}
if
(
!
view
)
break
;
cvReleaseImage
(
&
view
);
}
}
if
(
capture
)
cvReleaseCapture
(
&
capture
);
if
(
storage
)
cvReleaseMemStorage
(
&
storage
);
return
0
;
return
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment