Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 6cc8efe

Browse files
xooxalalek
authored andcommitted
Merge pull request opencv#12772 from xoox:calib-release-object
More accurate pinhole camera calibration with imperfect planar target 42 commits: Add derivatives with respect to object points Add an output parameter to calculate derivatives of image points with respect to 3D coordinates of object points. The output jacobian matrix is a 2Nx3N matrix where N is the number of points. This commit introduces incompatibility to old function signature. Set zero for dpdo matrix before using dpdo is a sparse matrix with only non-zero value close to major diagonal. Set it to zero because only elements near major diagonal are computed. Add jacobian columns to projectPoints() The output jacobian matrix of derivatives with respect to coordinates of 3D object points are added. This might break callers who assume the columns of jacobian matrix. Adapt test code to updated project functions The test cases for projectPoints() and cvProjectPoints2() are updated to fit new function signatures. Add accuracy test code for dpdo Add badarg test for dpdo Add new enum item for new calibration method CALIB_RELEASE_OBJECT is used to whether to release 3D coordinates of object points. The method was proposed in: K. H. Strobl and G. Hirzinger. "More Accurate Pinhole Camera Calibration with Imperfect Planar Target". In Proceedings of the IEEE International Conference on Computer Vision (ICCV 2011), 1st IEEE Workshop on Challenges and Opportunities in Robot Perception, Barcelona, Spain, pp. 1068-1075, November 2011. Add releasing object method into internal function It's a simple extension of the standard calibration scheme. We choose to fix the first and last object point and a user-selected fixed point. Add interfaces for extended calibration method Refine document for calibrateCamera() When releasing object points, only the z coordinates of the objectPoints[0].back is fixed. Add link to strobl2011iccv paper Improve documentation for calibrateCamera() Add implementations of wrapping calibrateCamera() Add checking for params of new calibration method If input parameters are not qualified, then fall back to standard calibration method. Add camera calibration method of releasing object The current implementation is equal to or better than https://github.com/xoox/calibrel Update doc for CALIB_RELEASE_OBJECT CALIB_USE_QR or CALIB_USE_LU could be used for faster calibration with potentially less precise and less stable in some rare cases. Add RELEASE_OBJECT calibration to tutorial code To select the calibration method of releasing object points, a command line parameter `-d=<number>` should be provided. Update tutorial doc for camera_calibration If the method of releasing object points is merged into OpenCV. It will be expected to be firstly released in 4.1, I think. Reduce epsilon for cornerSubPix() Epsilon of 0.1 is a bigger one. Preciser corner positions are required with calibration method of releasing object. Refine camera calibration tutorial The hypothesis coordinates are used to indicate which distance must be measured between two specified object points. Update sample calibration code method selection Similar to camera_calibration tutorial application, a command line argument `-dt=<number>` is used to select the calibration method. Add guard to flags of cvCalibrateCamera2() cvCalibrateCamera2() doesn't accept CALIB_RELEASE_OBJECT unless overload interface is added in the future. Simplify fallback when iFixedPoint is out of range Refactor projectPoints() to keep compatibilities Fix arg string "Bad rvecs header" Read calibration flags from test data files Instead of being hard coded into source file, the calibration flags will be read from test data files. opencv_extra/testdata/cv/cameracalibration/calib?.dat must be sync with the test code. Add new C interface of cvCalibrateCamera4() With this new added C interface, the extended calibration method with CALIB_RELEASE_OBJECT can be called by C API. Add regression test of extended calibration method It has been tested with new added test data in xoox:calib-release-object branch of opencv_extra. Fix assertion in test_cameracalibration.cpp The total number of refined 3D object coordinates is checked. Add checker for iFixedPoint in cvCalibrateCamera4 If iFixedPoint is out of rational range, fall back to standard method. Fix documentation for overloaded calibrateCamera() Remove calibration flag of CALIB_RELEASE_OBJECT The method selection is based on the range of the index of fixed point. For minus values, standard calibration method will be chosen. Values in a rational range will make the object-releasing calibration method selected. Use new interfaces instead of function overload Existing interfaces are preserved and new interfaces are added. Since most part of the code base are shared, calibrateCamera() is now a wrapper function of calibrateCameraRO(). Fix exported name of calibrateCameraRO() Update documentation for calibrateCameraRO() The circumstances where this method is mostly helpful are described. Add note on the rigidity of the calibration target Update documentation for calibrateCameraRO() It is clarified that iFixedPoint is used as a switch to select calibration method. If input data are not qualified, exceptions will be thrown instead of fallback scheme. Clarify iFixedPoint as switch and remove fallback iFixedPoint is now used as a switch for calibration method selection. No fallback scheme is utilized anymore. If the input data are not qualified, exceptions will be thrown. Add badarg test for object-releasing method Fix document format of sample list List items of same level should be indented the same way. Otherwise they will be formatted as nested lists by Doxygen. Add brief intro for objectPoints and imagePoints Sync tutorial to sample calibration code
1 parent a9c8a52 commit 6cc8efe

11 files changed

Lines changed: 758 additions & 111 deletions

File tree

‎doc/tutorials/calib3d/camera_calibration/camera_calibration.markdown‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ Source code
7777

7878
You may also find the source code in the `samples/cpp/tutorial_code/calib3d/camera_calibration/`
7979
folder of the OpenCV source library or [download it from here
80-
](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/calib3d/camera_calibration/camera_calibration.cpp). The program has a
81-
single argument: the name of its configuration file. If none is given then it will try to open the
80+
](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/calib3d/camera_calibration/camera_calibration.cpp). For the usage of the program, run it with `-h` argument. The program has an
81+
essential argument: the name of its configuration file. If none is given then it will try to open the
8282
one named "default.xml". [Here's a sample configuration file
8383
](https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/calib3d/camera_calibration/in_VID5.xml) in XML format. In the
8484
configuration file you may choose to use camera as an input, a video file or an image list. If you
@@ -131,6 +131,8 @@ Explanation
131131
Similar images result in similar equations, and similar equations at the calibration step will
132132
form an ill-posed problem, so the calibration will fail. For square images the positions of the
133133
corners are only approximate. We may improve this by calling the @ref cv::cornerSubPix function.
134+
(`winSize` is used to control the side length of the search window. Its default value is 11.
135+
`winSzie` may be changed by command line parameter `--winSize=<number>`.)
134136
It will produce better calibration result. After this we add a valid inputs result to the
135137
*imagePoints* vector to collect all of the equations into a single container. Finally, for
136138
visualization feedback purposes we will draw the found points on the input image using @ref
@@ -170,7 +172,7 @@ of the calibration variables we'll create these variables here and pass on both
170172
calibration and saving function. Again, I'll not show the saving part as that has little in common
171173
with the calibration. Explore the source file in order to find out how and what:
172174
@snippet samples/cpp/tutorial_code/calib3d/camera_calibration/camera_calibration.cpp run_and_save
173-
We do the calibration with the help of the @ref cv::calibrateCamera function. It has the following
175+
We do the calibration with the help of the @ref cv::calibrateCameraRO function. It has the following
174176
parameters:
175177

176178
- The object points. This is a vector of *Point3f* vector that for each input image describes how
@@ -184,13 +186,33 @@ parameters:
184186
@code{.cpp}
185187
vector<vector<Point3f> > objectPoints(1);
186188
calcBoardCornerPositions(s.boardSize, s.squareSize, objectPoints[0], s.calibrationPattern);
189+
objectPoints[0][s.boardSize.width - 1].x = objectPoints[0][0].x + grid_width;
190+
newObjPoints = objectPoints[0];
191+
187192
objectPoints.resize(imagePoints.size(),objectPoints[0]);
188193
@endcode
194+
@note If your calibration board is inaccurate, unmeasured, roughly planar targets (Checkerboard
195+
patterns on paper using off-the-shelf printers are the most convenient calibration targets and
196+
most of them are not accurate enough.), a method from @cite strobl2011iccv can be utilized to
197+
dramatically improve the accuracies of the estimated camera intrinsic parameters. This new
198+
calibration method will be called if command line parameter `-d=<number>` is provided. In the
199+
above code snippet, `grid_width` is actually the value set by `-d=<number>`. It's the measured
200+
distance between top-left (0, 0, 0) and top-right (s.squareSize*(s.boardSize.width-1), 0, 0)
201+
corners of the pattern grid points. It should be measured precisely with rulers or vernier calipers.
202+
After calibration, newObjPoints will be updated with refined 3D coordinates of object points.
189203
- The image points. This is a vector of *Point2f* vector which for each input image contains
190204
coordinates of the important points (corners for chessboard and centers of the circles for the
191205
circle pattern). We have already collected this from @ref cv::findChessboardCorners or @ref
192206
cv::findCirclesGrid function. We just need to pass it on.
193207
- The size of the image acquired from the camera, video file or the images.
208+
- The index of the object point to be fixed. We set it to -1 to request standard calibration method.
209+
If the new object-releasing method to be used, set it to the index of the top-right corner point
210+
of the calibration board grid. See cv::calibrateCameraRO for detailed explanation.
211+
@code{.cpp}
212+
int iFixedPoint = -1;
213+
if (release_object)
214+
iFixedPoint = s.boardSize.width - 1;
215+
@endcode
194216
- The camera matrix. If we used the fixed aspect ratio option we need to set \f$f_x\f$:
195217
@snippet samples/cpp/tutorial_code/calib3d/camera_calibration/camera_calibration.cpp fixed_aspect
196218
- The distortion coefficient matrix. Initialize with zero.
@@ -202,11 +224,15 @@ parameters:
202224
coordinate space). The 7-th and 8-th parameters are the output vector of matrices containing in
203225
the i-th position the rotation and translation vector for the i-th object point to the i-th
204226
image point.
227+
- The updated output vector of calibration pattern points. This parameter is ignored with standard
228+
calibration method.
205229
- The final argument is the flag. You need to specify here options like fix the aspect ratio for
206-
the focal length, assume zero tangential distortion or to fix the principal point.
230+
the focal length, assume zero tangential distortion or to fix the principal point. Here we use
231+
CALIB_USE_LU to get faster calibration speed.
207232
@code{.cpp}
208-
double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
209-
distCoeffs, rvecs, tvecs, s.flag|CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
233+
rms = calibrateCameraRO(objectPoints, imagePoints, imageSize, iFixedPoint,
234+
cameraMatrix, distCoeffs, rvecs, tvecs, newObjPoints,
235+
s.flag | CALIB_USE_LU);
210236
@endcode
211237
- The function returns the average re-projection error. This number gives a good estimation of
212238
precision of the found parameters. This should be as close to zero as possible. Given the

‎doc/tutorials/calib3d/table_of_content_calib3d.markdown‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Although we get most of our images in a 2D format they do come from a 3D world.
1313

1414
- @subpage tutorial_camera_calibration
1515

16-
*Compatibility:* \> OpenCV 2.0
16+
*Compatibility:* \> OpenCV 4.1
1717

1818
*Author:* Bernát Gábor
1919

‎modules/calib3d/doc/calib3d.bib‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,16 @@ @article{penate2013exhaustive
3939
year={2013},
4040
publisher={IEEE}
4141
}
42+
43+
@inproceedings{strobl2011iccv,
44+
title={More accurate pinhole camera calibration with imperfect planar target},
45+
author={Strobl, Klaus H. and Hirzinger, Gerd},
46+
booktitle={2011 IEEE International Conference on Computer Vision (ICCV)},
47+
pages={1068-1075},
48+
month={Nov},
49+
year={2011},
50+
address={Barcelona, Spain},
51+
publisher={IEEE},
52+
url={https://elib.dlr.de/71888/1/strobl_2011iccv.pdf},
53+
doi={10.1109/ICCVW.2011.6130369}
54+
}

‎modules/calib3d/include/opencv2/calib3d.hpp‎

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pattern (every view is described by several 3D-2D point correspondences).
170170
*rectification* transformation that makes the camera optical axes parallel.
171171
172172
@note
173-
- A calibration sample for 3 cameras in horizontal position can be found at
173+
- A calibration sample for 3 cameras in horizontal position can be found at
174174
opencv_source_code/samples/cpp/3calibration.cpp
175175
- A calibration sample based on a sequence of images can be found at
176176
opencv_source_code/samples/cpp/calibration.cpp
@@ -1077,7 +1077,7 @@ The algorithm performs the following steps:
10771077
patternSize=cvSize(cols,rows) in findChessboardCorners .
10781078
10791079
@sa
1080-
findChessboardCorners, solvePnP, initCameraMatrix2D, stereoCalibrate, undistort
1080+
calibrateCameraRO, findChessboardCorners, solvePnP, initCameraMatrix2D, stereoCalibrate, undistort
10811081
*/
10821082
CV_EXPORTS_AS(calibrateCameraExtended) double calibrateCamera( InputArrayOfArrays objectPoints,
10831083
InputArrayOfArrays imagePoints, Size imageSize,
@@ -1089,18 +1089,89 @@ CV_EXPORTS_AS(calibrateCameraExtended) double calibrateCamera( InputArrayOfArray
10891089
int flags = 0, TermCriteria criteria = TermCriteria(
10901090
TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
10911091

1092-
/** @overload double calibrateCamera( InputArrayOfArrays objectPoints,
1092+
/** @overload */
1093+
CV_EXPORTS_W double calibrateCamera( InputArrayOfArrays objectPoints,
10931094
InputArrayOfArrays imagePoints, Size imageSize,
10941095
InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
10951096
OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
1096-
OutputArray stdDeviations, OutputArray perViewErrors,
10971097
int flags = 0, TermCriteria criteria = TermCriteria(
1098-
TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) )
1098+
TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
1099+
1100+
/** @brief Finds the camera intrinsic and extrinsic parameters from several views of a calibration pattern.
1101+
1102+
This function is an extension of calibrateCamera() with the method of releasing object which was
1103+
proposed in @cite strobl2011iccv. In many common cases with inaccurate, unmeasured, roughly planar
1104+
targets (calibration plates), this method can dramatically improve the precision of the estimated
1105+
camera parameters. Both the object-releasing method and standard method are supported by this
1106+
function. Use the parameter **iFixedPoint** for method selection. In the internal implementation,
1107+
calibrateCamera() is a wrapper for this function.
1108+
1109+
@param objectPoints Vector of vectors of calibration pattern points in the calibration pattern
1110+
coordinate space. See calibrateCamera() for details. If the method of releasing object to be used,
1111+
the identical calibration board must be used in each view and it must be fully visible, and all
1112+
objectPoints[i] must be the same and all points should be roughly close to a plane. **The calibration
1113+
target has to be rigid, or at least static if the camera (rather than the calibration target) is
1114+
shifted for grabbing images.**
1115+
@param imagePoints Vector of vectors of the projections of calibration pattern points. See
1116+
calibrateCamera() for details.
1117+
@param imageSize Size of the image used only to initialize the intrinsic camera matrix.
1118+
@param iFixedPoint The index of the 3D object point in objectPoints[0] to be fixed. It also acts as
1119+
a switch for calibration method selection. If object-releasing method to be used, pass in the
1120+
parameter in the range of [1, objectPoints[0].size()-2], otherwise a value out of this range will
1121+
make standard calibration method selected. Usually the top-right corner point of the calibration
1122+
board grid is recommended to be fixed when object-releasing method being utilized. According to
1123+
\cite strobl2011iccv, two other points are also fixed. In this implementation, objectPoints[0].front
1124+
and objectPoints[0].back.z are used. With object-releasing method, accurate rvecs, tvecs and
1125+
newObjPoints are only possible if coordinates of these three fixed points are accurate enough.
1126+
@param cameraMatrix Output 3x3 floating-point camera matrix. See calibrateCamera() for details.
1127+
@param distCoeffs Output vector of distortion coefficients. See calibrateCamera() for details.
1128+
@param rvecs Output vector of rotation vectors estimated for each pattern view. See calibrateCamera()
1129+
for details.
1130+
@param tvecs Output vector of translation vectors estimated for each pattern view.
1131+
@param newObjPoints The updated output vector of calibration pattern points. The coordinates might
1132+
be scaled based on three fixed points. The returned coordinates are accurate only if the above
1133+
mentioned three fixed points are accurate. If not needed, noArray() can be passed in. This parameter
1134+
is ignored with standard calibration method.
1135+
@param stdDeviationsIntrinsics Output vector of standard deviations estimated for intrinsic parameters.
1136+
See calibrateCamera() for details.
1137+
@param stdDeviationsExtrinsics Output vector of standard deviations estimated for extrinsic parameters.
1138+
See calibrateCamera() for details.
1139+
@param stdDeviationsObjPoints Output vector of standard deviations estimated for refined coordinates
1140+
of calibration pattern points. It has the same size and order as objectPoints[0] vector. This
1141+
parameter is ignored with standard calibration method.
1142+
@param perViewErrors Output vector of the RMS re-projection error estimated for each pattern view.
1143+
@param flags Different flags that may be zero or a combination of some predefined values. See
1144+
calibrateCamera() for details. If the method of releasing object is used, the calibration time may
1145+
be much longer. CALIB_USE_QR or CALIB_USE_LU could be used for faster calibration with potentially
1146+
less precise and less stable in some rare cases.
1147+
@param criteria Termination criteria for the iterative optimization algorithm.
1148+
1149+
@return the overall RMS re-projection error.
1150+
1151+
The function estimates the intrinsic camera parameters and extrinsic parameters for each of the
1152+
views. The algorithm is based on @cite Zhang2000, @cite BouguetMCT and @cite strobl2011iccv. See
1153+
calibrateCamera() for other detailed explanations.
1154+
@sa
1155+
calibrateCamera, findChessboardCorners, solvePnP, initCameraMatrix2D, stereoCalibrate, undistort
10991156
*/
1100-
CV_EXPORTS_W double calibrateCamera( InputArrayOfArrays objectPoints,
1101-
InputArrayOfArrays imagePoints, Size imageSize,
1157+
CV_EXPORTS_AS(calibrateCameraROExtended) double calibrateCameraRO( InputArrayOfArrays objectPoints,
1158+
InputArrayOfArrays imagePoints, Size imageSize, int iFixedPoint,
1159+
InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
1160+
OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
1161+
OutputArray newObjPoints,
1162+
OutputArray stdDeviationsIntrinsics,
1163+
OutputArray stdDeviationsExtrinsics,
1164+
OutputArray stdDeviationsObjPoints,
1165+
OutputArray perViewErrors,
1166+
int flags = 0, TermCriteria criteria = TermCriteria(
1167+
TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
1168+
1169+
/** @overload */
1170+
CV_EXPORTS_W double calibrateCameraRO( InputArrayOfArrays objectPoints,
1171+
InputArrayOfArrays imagePoints, Size imageSize, int iFixedPoint,
11021172
InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
11031173
OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
1174+
OutputArray newObjPoints,
11041175
int flags = 0, TermCriteria criteria = TermCriteria(
11051176
TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
11061177

‎modules/calib3d/include/opencv2/calib3d/calib3d_c.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,22 @@ CVAPI(double) cvCalibrateCamera2( const CvMat* object_points,
263263
CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria(
264264
CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,DBL_EPSILON)) );
265265

266+
/* Finds intrinsic and extrinsic camera parameters
267+
from a few views of known calibration pattern */
268+
CVAPI(double) cvCalibrateCamera4( const CvMat* object_points,
269+
const CvMat* image_points,
270+
const CvMat* point_counts,
271+
CvSize image_size,
272+
int iFixedPoint,
273+
CvMat* camera_matrix,
274+
CvMat* distortion_coeffs,
275+
CvMat* rotation_vectors CV_DEFAULT(NULL),
276+
CvMat* translation_vectors CV_DEFAULT(NULL),
277+
CvMat* newObjPoints CV_DEFAULT(NULL),
278+
int flags CV_DEFAULT(0),
279+
CvTermCriteria term_crit CV_DEFAULT(cvTermCriteria(
280+
CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,30,DBL_EPSILON)) );
281+
266282
/* Computes various useful characteristics of the camera from the data computed by
267283
cvCalibrateCamera2 */
268284
CVAPI(void) cvCalibrationMatrixValues( const CvMat *camera_matrix,

0 commit comments

Comments
 (0)