CVBpy 15.0
foundation/MetricCalibrationRigidBodyTransformation
1"""
2CVBpy Example Script for AQS12 Calibration - Use Case 3.
3
4This example shows how to bring an intrinsically calibrated point cloud to
5a world coordinate system via a rigid body transformation.
6This workflow is recommended for area 3D compact sensors.
7
8See also use case 3 described in the CVB Metric documentation:
9https://help.commonvisionblox.com/NextGen/15.0/md_theory_of_operation_tools__metric.html#calibration_setup
10
11The rigid body transformation solely inlcudes rotation and translation.
12"""
13
14import os
15
16import cvb
17import cvb.foundation
18
19
20def print_trafo(trafo: cvb.AffineMatrix3D) -> None:
21 print("Estimated transformation:")
22 print("Translation:")
23 print(f"[{trafo.translation.x}, {trafo.translation.y}, "
24 f"{trafo.translation.z}]")
25 print("Transformation matrix:")
26 print(f"[[{trafo.matrix.at(0, 0)}, "
27 f"{trafo.matrix.at(0, 1)}, "
28 f"{trafo.matrix.at(0, 2)}],")
29 print(f"[{trafo.matrix.at(1, 0)}, "
30 f"{trafo.matrix.at(1, 1)}, "
31 f"{trafo.matrix.at(1, 2)}],")
32 print(f"[{trafo.matrix.at(2, 0)}, "
33 f"{trafo.matrix.at(2, 1)}, "
34 f"{trafo.matrix.at(2, 2)}]]")
35
36
37def print_trafo_parameters(atp: cvb.AffineTransformationParameters) -> None:
38 print("Rotation angles about X, Y, Z axis in degrees:")
39 print(f"{atp.rotation_angles.x}, {atp.rotation_angles.y}, "
40 f"{atp.rotation_angles.z}")
41 print("Shear Syx, Syz:")
42 print(f"{atp.s_yx}, {atp.s_yz}")
43 print("Inclination of laser plane about X, Z axis in degrees:")
44 print(f"{atp.inclination_x}, {atp.inclination_z}")
45 print("Scale in X, Y, Z:")
46 print(f"{atp.scale.x}, {atp.scale.y}, {atp.scale.z}")
47
48
49def print_point_3d_list(points: list[cvb.Point3D]) -> None:
50 data_list = list()
51 for p in points:
52 data_list.append(f"[{p.x}, {p.y}, {p.z}]")
53 delimiter = ",\n"
54 print(f"[{delimiter.join(data_list)}]")
55
56
57def print_residuals(points: list[cvb.Point3D]) -> None:
58 print("Residuals:")
59 print_point_3d_list(points)
60
61
62def print_extrinsic_matrix(matrix: cvb.AffineMatrix3D) -> None:
63 rotation = matrix.matrix.rotation_angles
64 print("Transformation results:")
65 print("Rotation about x, y, and z in degrees:")
66 print(f"{rotation[0]}, {rotation[1]}, {rotation[2]}")
67 print("Translation about x, y, and z in millimeters:")
68 print(f"{matrix.translation.x}, {matrix.translation.y}, "
69 f"{matrix.translation.z}")
70
71
72def create_aqs12():
73 # list of known point coordinates of the AQS12
74 points = [
75 cvb.Point3D(20.0018, 44.9941, 15.0000),
76 cvb.Point3D(24.0018, 39.9942, 14.9994),
77 cvb.Point3D(23.9994, 24.9972, 15.0001),
78 cvb.Point3D(20.0021, 20.0035, 15.0011),
79 cvb.Point3D(15.9994, 25.0079, 15.0016),
80 cvb.Point3D(16.0000, 39.9919, 15.0010),
81 cvb.Point3D(20.0095, 59.9985, 4.9902),
82 cvb.Point3D(32.0093, 44.9958, 4.9909),
83 cvb.Point3D(32.0052, 19.9925, 4.9920),
84 cvb.Point3D(20.0021, 4.9961, 4.9939),
85 cvb.Point3D(8.0024, 19.9980, 5.0009),
86 cvb.Point3D(8.0065, 45.0009, 4.9984)]
87 return cvb.foundation.AQS12Piece(points, 0)
88
89
90# If you like to save the calibrated point cloud, turn this flag on:
91save = False
92
93print("Estimation of a rigid body transformation (rotation and translation)")
94
95# load range map of the calibration target AQS12
96print("Loading range map and calibration file.")
97range_map_file = os.path.join(
98 os.path.join(cvb.install_path(), "tutorial", "Metric", "Images",
99 "RangeMapCalibrationPattern.tif"))
100
101range_map = cvb.Image(range_map_file)
102
103print(f"Range map loaded with size of {range_map.width} x {range_map.height} "
104 f"from {range_map_file}.")
105
106# create AQS12 object with known reference coordinates of corner points
107aqs12 = create_aqs12()
108
109# create (intrinsically) calibrated dense point cloud
110calibrator_file = os.path.join(
111 cvb.install_path(), "tutorial", "Metric", "Images", "SICalibration.json")
112calibrator = cvb.Calibrator3D.load(calibrator_file)
113calibrator.range_map_ignore_value = 0.
114cloud_intrinsic = cvb.PointCloudFactory.create_dense(
115 range_map.planes[0], calibrator,
116 cvb.PointCloudFlags.Float | cvb.PointCloudFlags.XYZConfidence)
117
118print("Dense point cloud created from range map and calibration file "
119 f"with {cloud_intrinsic.num_points} points.")
120
121# create segmentor (segmenting AQS12 faces on dense point cloud)
123 cvb.foundation.SegmentationMethod.KmeansClustering)
124
125# estimate calibration parameters
126print("Estimating a rigid-body transformation.")
127transformation_, residuals_, transformation_parameters_ = \
129 cloud_intrinsic, segmentor, aqs12)
130calibrator.extrinsic_matrix = transformation_
131
132# show results
133if transformation_:
134 print_trafo(transformation_)
135
136if transformation_parameters_:
137 print_trafo_parameters(transformation_parameters_)
138
139if residuals_:
140 print_residuals(residuals_)
141
142print_extrinsic_matrix(calibrator.extrinsic_matrix)
143
144# create calibrated point cloud
145print("Creating calibrated point cloud.")
147 range_map.planes[0], calibrator,
148 cvb.PointCloudFlags.Float | cvb.PointCloudFlags.XYZConfidence)
149
150if save:
151 cloud.save("cloud.ply")
Union[cvb.Calibrator3DAT, cvb.LaserPlaneHomographyCalibrator3D, cvb.LaserPlaneZigZagCalibrator3D, cvb.FactorsCalibrator3D, cvb.MatrixCalibrator3D, cvb.PinholeCameraCalibrator3D] load(str file_name)
Loads a 3D calibration from file.
Definition: __init__.py:671
The Common Vision Blox image.
Definition: __init__.py:2097
Multi-purpose 3D vector class.
Definition: __init__.py:4322
Union[cvb.PointCloud, cvb.DensePointCloud, cvb.SparsePointCloud] create(cvb.ImagePlane range_map, cvb.Calibrator3D calibrator, int flags, Union[Type[cvb.PointCloud|cvb.DensePointCloud|cvb.SparsePointCloud]] point_cloud_type=DensePointCloud)
Creates a new Cartesian 3D point cloud from the given 2.5D range map image.
Definition: __init__.py:4701
cvb.DensePointCloud create_dense(cvb.ImagePlane range_map, cvb.Calibrator3D calibrator, int flags)
Creates a new dense Cartesian 3D point cloud from the given 2.5D range map image.
Definition: __init__.py:4732
cvb.foundation.AQS12DensePointCloudSegmentor create(int method)
Creates an AQS12 segmentor for dense point clouds based on given segmentation method.
Definition: __init__.py:18
Object to collect all input parameters for the AQS12 calibration piece.
Definition: __init__.py:76
Common Vision Blox Foundation module for Python.
Definition: __init__.py:1
Tuple[cvb.AffineMatrix3D, Optional[List[cvb.Point3D]], cvb.AffineTransformationParameters] calculate_rigid_body_transformation_from_aqs12_piece(cvb.DensePointCloud cloud, cvb.foundation.AQS12DensePointCloudSegmentor segmentor, cvb.foundation.AQS12Piece aqs12, Optional[cvb.Rect] aoi=None)
Calculates a rigid body transformation from a given dense cloud of an AQS12 calibration piece.
Definition: __init__.py:2320
str install_path()
Directory Common Vision Blox has been installed to.
Definition: __init__.py:8318