Operators |
calibrate_cameras — Determine all camera parameters by a simultaneous minimization process.
calibrate_cameras( : : CalibDataID : Error)
The operator calibrate_cameras calculates the internal and external camera parameters of a calibration data model specified in CalibDataID. For a detailed description of the camera parameters, see section “Camera parameters”.
The calibration data model describes a setup of one or more cameras and is specified during the creation of the data model. See section “Preparing the calibration input data” for details.
The root mean square error (RMSE) of the back projection of the optimization is returned in Error (in pixels). The error gives a general indication whether the optimization was successful. See section “Checking the success of the calibration” for details.
For a successful calibration, at least one calibration object with accurately known metric properties is needed, e.g., a HALCON calibration plate. Before calling calibrate_cameras , take a series of images of the calibration object in different orientations and make sure that the whole field of view or measurement volume is covered. The success of the calibration highly depends on the quality of the calibration object and the images. So you might want to exercise special diligence during the acquisiton of the calibration images. See the section “How to take a set of suitable images?” for details.
In the following, the calibration process is explained in detail:
Before calling calibrate_cameras , you must create and fill the calibration data model with the following steps:
Create a calibration data model with the operator create_calib_data, specifying the number of cameras in the setup and the number of used calibration objects.
Specify the camera type and the initial internal camera parameters for all cameras with the operator set_calib_data_cam_param. Note that only cameras of the same type can be calibrated in a single setup.
Specify the description of all calibration objects with the operator set_calib_data_calib_object.
Collect observation data with the operators find_calib_object or set_calib_data_observ_points, i.e., the image coordinates of the extracted calibration marks of the calibration object and a roughly estimated pose of the calibration object relative to the observing camera.
Configure the calibration process, e.g., specify the reference camera or exclude certain internal or external camera parameters from the optimization. With the operator set_calib_data, you can specify parameters for the complete setup or configure parameters of individual cameras or calibration object poses in the setup. For example, if the image sensor cell size of camera 0 is known precisely and only the rest of the parameters need to be calibrated, you call
set_calib_data (CalibDataID, 'camera', 0, \ 'excluded_settings', ['sx','sy']).
Depending on the camera type being calibrated in the setup, calibrate_cameras performs the calibration in three different ways.
For projective area-scan cameras ('area_scan_division' , 'area_scan_polynomial' , 'area_scan_tilt_division' , and 'area_scan_tilt_polynomial' ), the calibration is performed in four steps. First, the algorithm tries to build a chain of observation poses that connects all cameras and calibration object poses to the reference camera.
(1) | (2) |
If there is a camera that cannot be reached (i.e., it is not observing any calibration object pose that can be connected in the chain), the calibration process is terminated with an error. Otherwise, the algorithm initializes all calibration items' poses by going down this chain. In the second step, calibrate_cameras performs the actual optimization for all optimization parameters that where not explicitly excluded from the calibration. Based on the so-far calibrated cameras, in the third step the algorithm corrects all observations that contain mark contour information (see find_calib_object). Then, the calibration setup is optimized anew for the corrections to take effect. If no contour information was available, this step is skipped. In the last step, calibrate_cameras computes the standard deviations and the covariances of the calibrated internal camera parameters.
For telecentric area-scan cameras ('area_scan_telecentric_division' , 'area_scan_telecentric_polynomial' , 'area_scan_telecentric_tilt_division' , or 'area_scan_telecentric_tilt_polynomial' ) the same four steps are executed as for projective area-scan cameras. In the first step (building a chain of observation poses that connects all cameras and calibration objects), additional conditions must hold. Since the pose of an object can only be determined up to a translation along the optical axis, each calibration object must be observed by at least two cameras to determine its relative location. Otherwise, its pose is excluded from the calibration. Also, since a planar calibration object appears the same from two different observation angles, the relative pose of the cameras among each other cannot be determined unambiguously. There are always two valid alternative relative poses. Note that both alternatives result in a consistent camera setup which can be used for measuring. Since the ambiguity cannot be resolved, the first of the alternatives is returned. Note also that, if the returned pose is not the real pose but the alternative one, then this will result in a mirrored reconstruction.
For line-scan cameras ('line_scan' ), the operator internally calls camera_calibration. Therefore, some of the restrictions of camera_calibration are inherited as well: in addition to the restriction of only one camera and only one calibration object per setup, there is a further restriction that the calibration does not deliver information about standard deviations and covariances for the estimated parameters. Furthermore, for calibration plates with rectangularly arranged marks (see gen_caltab) all observations must contain the projection coordinates of all calibration marks of the calibration object. For calibration plates with hexagonally arranged marks (see create_caltab) this restriction is not applied.
After a successful calibration, the root mean square error (RMSE) of the back projection of the optimization is returned in Error (in pixels). The error gives a general indication whether the optimization was successful.
If only a single camera is calibrated, an Error in the order of 0.1 pixel (the typical detection error by extraction of the coordinates of the projected calibration markers) is an indication that the optimization fits the observation data well. If Error strongly differs from 0.1 pixels, the calibration did not perform well. Reasons for this might be, e.g., a poor image quality, an insufficient number of calibration images, or an inaccurate calibration plate. If more than one camera is calibrated simultaneously, the value of Error is more difficult to judge. As a rule of thumb, Error should be as small as possible and at least smaller than 1.0, thus indicating that a subpixel precise evaluation of the data is possible with the calibrated parameters. This value might be difficult to reach in particular configurations. For further analysis of the quality of the calibration, refer to the standard deviations and covariances of the estimated parameters (currently for area-scan cameras only, see get_calib_data).
The results of the calibration, i.e., internal camera parameters, camera poses (external camera parameters), calibration objects poses etc., can be queried with get_calib_data. The poses of telecentric cameras can only be determined up to a displacement along the z-axis of the coordinate system of the respective camera. Therefore, all camera poses are moved along this axis until they all lie on a common sphere. The center of the sphere is defined by the pose of the first calibration object.
The camera parameters can be divided into the internal and external camera parameters.
These parameters describe the characteristics of the used camera, especially the dimension of the sensor itself and the projection properties of the used combination of lens, camera, and frame grabber.
Area scan cameras have 8 to 14 internal parameters depending on the camera type.
CameraType | CameraParam | # |
---|---|---|
'area_scan_division' | [Focus, Kappa, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 8 |
'area_scan_telecentric_division' | [0, Kappa, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 8 |
'area_scan_tilt_division' | [Focus, Kappa, Tilt, Rot, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 10 |
'area_scan_telecentric_tilt_division' | [0, Kappa, Tilt, Rot, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 10 |
'area_scan_polynomial' | [Focus, K1, K2, K3, P1, P2, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 12 |
'area_scan_telecentric_polynomial' | [0, K1, K2, K3, P1, P2, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 12 |
'area_scan_tilt_polynomial' | [Focus, K1, K2, K3, P1, P2, Tilt, Rot, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 14 |
'area_scan_telecentric_tilt_polynomial' | [0, K1, K2, K3, P1, P2, Tilt, Rot, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight] | 14 |
'line_scan' | [Focus, Kappa, Sx, Sy, Cx, Cy, ImageWidth, ImageHeight, Vx, Vy, Vz] | 11 |
Focal length of the lens. 0 for telecentric lenses.
The initial value is the nominal focal length of the used lens, e.g., 0.008m
Distortion coefficient to model the radial lens distortions (only for division model).
Use 0.0 as initial value.
Distortion coefficients to model the radial and decentering lens distortions (only for polynomial model).
Use 0.0 as initial value for all five coefficents.
The tilt angle describes the angle by which the optical axis is tilted with respect to the normal of the sensor plane. The rotation angle describes the direction in which the optical axis is tilted. These parameters are only used if a tilt lens is part of the camera setup.
These angels are typically roughly known based on the considerations that led to the use of the tilt lens or can be read off from the mechanism by which the lens is tilted.
Scale factors. For pinhole cameras, this corresponds to the horizontal and vertical distance between two neighboring cells on the sensor. For telecentric cameras, this represents the horizontal and vertical size of a pixel in world coordinates (i.e., the distance of the cells on the sensor divided by the magnification of the lens). Since in most cases the image signal is sampled line-synchronously, is determined by the dimension of the sensor and does not need to be estimated for pinhole cameras by the calibration process.
The initial values depend on the dimensions of the used chip of the camera. See the technical specification of your camera for the actual values. Attention: These values increase if the image is subsampled!
Column () and row () coordinate of the principal point of the image (center of the radial distortion).
Use the half image width and height as initial values. Attention: These values decrease if the image is subsampled!
Width and height of the sampled image. Attention: These values decrease if the image is subsampled!
Line scan cameras have the following 11 internal parameters:
Focal length of the lens.
The initial value is the nominal focal length of the used lens, e.g., 0.008m
Distortion coefficient of the division model to model the radial lens distortions. Use 0.0 as initial value.
Scale factor. Corresponds to the horizontal distance between two neighboring cells on the sensor. Note that Focus and cannot be determined simultaneously. Therefore, is kept fixed in the calibration. The initial value for can be taken from the technical specifications of the camera. Attention: This value increases if the image is subsampled!
Scale factor. During the calibration, it appears only in the form . Consequently, and cannot be determined simultaneously. Therefore, in the calibration, is kept fixed. describes the distance of the image center point from the sensor line in meters. The initial value for can be taken from the technical specifications of the camera. Attention: This value increases if the image is subsampled!
Column coordinate of the image center point (center of the radial distortions). The initial value for is the half image width. Attention: This value decreases if the image is subsampled!
Distance of the image center point (center of the radial distortions) from the sensor line in scanlines. The initial value can normally be set to 0.
Width and height of the sampled image. Attention: These values decrease if the image is subsampled!
X-, Y-, and Z-component of the motion vector.
The initial values for the x-, y-, and z-component of the motion vector depend on the image acquisition setup. Assuming a camera that looks perpendicularly onto a conveyor belt and that is rotated around its optical axis such that the sensor line is perpendicular to the conveyor belt, i.e., the y-axis of the camera coordinate system is parallel to the conveyor belt, use the initial values . The initial value for can then be determined, e.g., from a line scan image of an object with known size (e.g., calibration plate, ruler):
With
If, compared to the above setup, the camera is rotated 30 degrees around its optical axis, i.e., around the z-axis of the camera coordinate system, the above determined initial values must be changed as follows:
If, compared to the first setup, the camera is rotated -20 degrees around the x-axis of the camera coordinate system, the following initial values result:
Note that the term focal length is not quite correct and would be appropriate only for an infinite object distance. To simplify matters, always the term focal length is used even if the image distance is meant.
Note that for all operators that use camera parameters as input the parameter values are checked as to whether they fullfill the following restrictions:
For some operators the restrictions differ slightly. In particular, for operators that do not support telecentric cameras the following restriction applies:
For operators that do not support line scan cameras the following restriction applies:
Translation along the x-axis of the camera coordinate system.
Translation along the y-axis of the camera coordinate system.
Translation along the z-axis of the camera coordinate system.
Rotation around the x-axis of the camera coordinate system.
Rotation around the y-axis of the camera coordinate system.
Rotation around the z-axis of the camera coordinate system.
These 6 parameters describe the 3D pose, i.e., the position and orientation of the world coordinate system relative to the camera coordinate system. For line scan cameras, the pose of the world coordinate system refers to the camera coordinate system of the first image line. The three parameters (TransX, TransY and TransZ) describe the translation, the three parameters (RotX, RotY, RotZ) the rotation. See create_pose for more information about 3D poses.
When using a standard HALCON calibration plate, the world coordinate system is defined by the coordinate system of the calibration plate. For calibration plates with hexagonally arranged marks (see create_caltab), the origin of the coordinate system is located at the center of the central mark of the first finder pattern. For calibration plates with rectangularly arranged marks gen_caltab the origin is located in the middle of the surface of the calibration plate. In both cases, the z-axis of the coordinate system is pointing into the calibration plate, its x-axis is pointing to the right, and its y-axis is pointing downwards.
If a HALCON calibration plate is used, you can use the operator find_calib_object to determine initial values for all 6 parameters. Using HALCON calibration plates with rectangularly arranged marks (see gen_caltab), a combination of the two operators find_caltab and find_marks_and_pose will have the same effect.
The use of calibrate_cameras leads to some questions, which are addressed in the following sections:
You can obtain high-precision calibration plates in various sizes and materials from your local distributor. These calibration plates come with associated description files and can be easily extracted with find_calib_object.
It is also possible to use any arbitrary object for calibration. The only requirement is that the object has characteristic points that can be robustly detected in the image and that the 3D world position of these points is known with high accuracy. See the “Solution Guide III-C 3D Vision” for details.
Self-printed calibration objects are usually not accurate enough for high-precision applications.
If you use a HALCON calibration plate, you can proceed in the following way: With the combination of lens (fixed focus setting!), camera, and frame grabber to be calibrated, a set of images of the calibration plate must be taken (see open_framegrabber and grab_image). The following items must be considered:
At least a total of 10 to 20 images should be used for the calibration.
Reflections etc. on the calibration plate should be avoided.
The aperture of the camera must not be changed during the acquisition of the images. If the aperture is changed after the calibration, the camera has to be calibrated anew.
The position of the camera must not be changed during the image acquisition.
Within the set of images, the calibration plate should appear in different positions and orientations: E.g., at all four corners of the image, in the middle, at the borders, and at different distances. Furthermore, the calibration plate should be rotated and tilted, so the perspective distortions of the calibration object are clearly visible.
The calibration plate should fill at least a quarter of the whole image to ensure the robust detection of the marks and a robust modelling of radial distortions.
The calibration marks must not be acquired inverted. This can e.g happen if a calibration plate made of glas is acquired on the backside.
Your local distributor can provide you with two different types of HALCON calibration plates: Calibration plates with hexagonally arranged marks (see create_caltab) and calibration plates with rectangularly arranged marks (see gen_caltab). Since these two calibration plates substantially differ from each other, additional particularities have to be considered beside the general advices (see also find_calib_object):
HALCON calibration plates with hexagonally arranged marks
The calibration plate may fill out the entire image and may even protrude beyond the rim of the image. It is sufficient if at least one finder pattern of the calibration plate is visible in the image. Nevertheless, care has to be taken that as many marks as possible can be seen from the camera. Thereby, more marks can be extracted per image, which not only results in a robust calibration with less images but also renders a more reliable estimation of the distortions.
Owing to the vast number of marks, usually less images are necessary for a precise calibration compared to the usage of calibration plates with rectangularly arranged marks.
If at least two finder patterns are visible in the image, it is possible to detect whether the calibration plate is mirrored or not. In a mirrored case, HDevelop will return a suitable error.
HALCON calibration plates with rectangularly arranged marks
The calibration plate must be completely visible in the image (including its border!).
For area scan cameras, two distortion models can be used: The division model and the polynomial model. The division model uses one parameter to model the radial distortions while the polynomial model uses five parameters to model radial and decentering distortions (see section “Camera parameters”).
The advantages of the division model are that the distortions can be applied faster, especially the inverse distortions, i.e., if world coordinates are projected into the image plane. Furthermore, if only few calibration images are used or if the field of view is not covered sufficiently, the division model typically yields more stable results than the polynomial model. The main advantage of the polynomial model is that it can model the distortions more accurately because it uses higher order terms to model the radial distortions and because it also models the decentering distortions. Note that the polynomial model cannot be inverted analytically. Therefore, the inverse distortions must be calculated iteratively, which is slower than the calculation of the inverse distortions with the (analytically invertible) division model.
Typically, the division model should be used for the calibration. If the accuracy of the calibration is not high enough, the polynomial model can be used. Note, however, that the calibration sequence used for the polynomial model must provide an even better coverage of the area in which measurements will later be performed. The distortions may be modeled inaccurately outside of the area that was covered by the calibration plate. This holds for the image border as well as for areas inside the field of view that were not covered by the calibration plate.
In general, camera calibration means the exact determination of the parameters that model the (optical) projection of any 3D world point into a (sub-)pixel (r,c) in the image. This is important if the original 3D pose of an object must be computed from the image (e.g., measuring of industrial parts).
To transform a 3D point which is given in world coordinates, into a 2D point , which is given in pixel coordinates, a chain of transformations is needed:
First, is transformed into the camera coordinate system into . Then, is projected into the image plane, i.e., converted to the 2D point , still in metric coordinates. Then, lens distortion is applied to , transforming it into the distorted point . If a tilt lens is used, only lies on a virtual image plane of a system without tilt. This is corrected by projecting to the point on the tilted image plane. Finally, the coordinates of the distorted point (or ) are converted to pixel coordinates, which results in the final point .
The following describes these steps in more detail:
The point is transformed from world into camera coordinates by (points as homogeneous vectors, compare affine_trans_point_3d):
Then, the point is projected into the image plane, i.e., onto the sensor chip.
For the modeling of this projection process, which is determined by the used combination of camera, lens, and frame grabber, HALCON provides the following three 3D camera models:
The combination of an area scan camera with a lens that effects a perspective projection and that may show radial and decentering distortions. The lens may be a tilt lens, i.e., the optical axis of the lens may be tilted with respect to the camera's sensor (this is sometimes called a Scheimpflug lens).
The combination of an area scan camera with a telecentric lens that effects a parallel projection and that may show radial and decentering distortions. The lens may be a tilt lens. In this case, the lens must be bilaterally telecentric.
The combination of a line scan camera with a lens that effects a perspective projection and that may show radial distortions. Tilt lenses are currently not supported for line scan cameras.
If the underlying camera model is an area scan pinhole camera, the projection of into the image plane is described by the following equation:
If an area scan telecentric camera is used, the corresponding equation is:
For both types of area scan cameras, the lens distortions can be modeled either by the division model or by the polynomial model.
The division model uses one parameter () to model the radial distortions.
The following equations transform the distorted image plane coordinates into undistorted image plane coordinates if the division model is used:
These equations can be inverted analytically, which leads to the following equations that transform undistorted coordinates into distorted coordinates:
The polynomial model uses three parameters () to model the radial distortions and two parameters () to model the decentering distortions.
The following equations transform the distorted image plane coordinates into undistorted image plane coordinates if the polynomial model is used: