Skip to content

Camera

Mono/stereo camera models.

Camera(intrinsics=CameraIntrinsics(), distortion=DistortionCoefficients(), extrinsics=Pose3D()) dataclass

Bases: CameraBase

Standard pinhole camera.

Attributes:

Name Type Description
intrinsics CameraIntrinsics

Intrinsic parameters.

distortion DistortionCoefficients

Distortion coefficients.

extrinsics Pose3D

Extrinsic parameters.

project(points, points_frame=None)

Project 3D points into the image plane.

Parameters:

Name Type Description Default
points ndarray

3D points as a 3xN numpy array.

required
points_frame Optional[Pose3D]

Pose of the points frame relative to the camera frame. If None, the points are assumed to be in the camera frame.

None

Returns:

Type Description
ndarray

Projected points as a 2xN numpy array.

Source code in src/compas_camcal/models/camera.py
 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
def project(self, points: np.ndarray, points_frame: Optional[Pose3D] = None) -> np.ndarray:
    """
    Project 3D points into the image plane.

    Args:
        points: 3D points as a 3xN numpy array.
        points_frame: Pose of the points frame relative to the camera frame. If None, the points are assumed to be in the camera frame.

    Returns:
        Projected points as a 2xN numpy array.
    """
    if points_frame is not None:
        # Convert rotation/translation
        rodrigues_rotation, _ = cv2.Rodrigues(points_frame.rotation.to_matrix())
        translation = points_frame.translation.to_vector()
    else:
        rodrigues_rotation = np.zeros((3, 1))
        translation = np.zeros((3, 1))

    # Project points into image plane
    image_points, _ = cv2.projectPoints(
        points,
        rodrigues_rotation,
        translation,
        self.intrinsics.to_matrix(),
        self.distortion.to_vector().ravel(),
    )

    image_points = image_points.reshape(-1, 2)

    return image_points

undistort(image)

Undistort an image.

Parameters:

Name Type Description Default
image ndarray

Image to undistort.

required

Returns:

Type Description
ndarray

Undistorted image.

Source code in src/compas_camcal/models/camera.py
112
113
114
115
116
117
118
119
120
121
122
123
124
def undistort(self, image: np.ndarray) -> np.ndarray:
    """
    Undistort an image.

    Args:
        image: Image to undistort.

    Returns:
        Undistorted image.
    """
    return cv2.undistort(
        image, self.intrinsics.to_matrix(), self.distortion.to_vector().ravel()
    )

CameraBase(intrinsics=CameraIntrinsics(), distortion=DistortionCoefficientsBase(), extrinsics=Pose3D()) dataclass

Bases: ABC

Abstract base class for cameras.

Attributes:

Name Type Description
intrinsics CameraIntrinsics

Intrinsic parameters.

distortion DistortionCoefficientsBase

Distortion coefficients.

extrinsics Pose3D

Extrinsic parameters.

project(points, points_frame=None) abstractmethod

Project 3D points into the image plane.

Parameters:

Name Type Description Default
points ndarray

3D points as a 3xN numpy array.

required
points_frame Optional[Pose3D]

Pose of the points frame relative to the camera frame. If None, the points are assumed to be in the camera frame.

None

Returns:

Type Description
ndarray

Projected points as a 2xN numpy array.

Source code in src/compas_camcal/models/camera.py
38
39
40
41
42
43
44
45
46
47
48
49
50
@abstractmethod
def project(self, points: np.ndarray, points_frame: Optional[Pose3D] = None) -> np.ndarray:
    """
    Project 3D points into the image plane.

    Args:
        points: 3D points as a 3xN numpy array.
        points_frame: Pose of the points frame relative to the camera frame. If None, the points are assumed to be in the camera frame.

    Returns:
        Projected points as a 2xN numpy array.
    """
    raise NotImplementedError

undistort(image) abstractmethod

Undistort an image.

Parameters:

Name Type Description Default
image ndarray

Image to undistort.

required

Returns:

Type Description
ndarray

Undistorted image.

Source code in src/compas_camcal/models/camera.py
52
53
54
55
56
57
58
59
60
61
62
63
@abstractmethod
def undistort(self, image: np.ndarray) -> np.ndarray:
    """
    Undistort an image.

    Args:
        image: Image to undistort.

    Returns:
        Undistorted image.
    """
    raise NotImplementedError

FisheyeCamera(intrinsics=CameraIntrinsics(), distortion=FisheyeDistortionCoefficients(), extrinsics=Pose3D()) dataclass

Bases: CameraBase

Fisheye camera.

Attributes:

Name Type Description
intrinsics CameraIntrinsics

Intrinsic parameters.

distortion FisheyeDistortionCoefficients

Distortion coefficients.

extrinsics Pose3D

Extrinsic parameters.

project(points, points_frame=None)

Project 3D points into the image plane.

Parameters:

Name Type Description Default
points ndarray

3D points as a 3xN numpy array.

required
points_frame Optional[Pose3D]

Pose of the points frame relative to the camera frame. If None, the points are assumed to be in the camera frame.

None

Returns:

Type Description
ndarray

Projected points as a 2xN numpy array.

Source code in src/compas_camcal/models/camera.py
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
def project(self, points: np.ndarray, points_frame: Optional[Pose3D] = None) -> np.ndarray:
    """
    Project 3D points into the image plane.

    Args:
        points: 3D points as a 3xN numpy array.
        points_frame: Pose of the points frame relative to the camera frame. If None, the points are assumed to be in the camera frame.

    Returns:
        Projected points as a 2xN numpy array.
    """
    if points_frame is not None:
        # Convert rotation/translation
        rodrigues_rotation, _ = cv2.Rodrigues(points_frame.rotation.to_matrix())
        translation = points_frame.translation.to_vector()
    else:
        rodrigues_rotation = np.zeros((3, 1))
        translation = np.zeros((3, 1))

    # Project points into image plane
    image_points, _ = cv2.fisheye.projectPoints(
        points,
        rodrigues_rotation,
        translation,
        self.intrinsics.to_matrix(),
        self.distortion.to_vector().ravel(),
    )

    image_points = image_points.reshape(-1, 2)

    return image_points

undistort(image)

Undistort an image.

Parameters:

Name Type Description Default
image ndarray

Image to undistort.

required

Returns:

Type Description
ndarray

Undistorted image.

Source code in src/compas_camcal/models/camera.py
173
174
175
176
177
178
179
180
181
182
183
184
185
def undistort(self, image: np.ndarray) -> np.ndarray:
    """
    Undistort an image.

    Args:
        image: Image to undistort.

    Returns:
        Undistorted image.
    """
    return cv2.fisheye.undistortImage(
        image, self.intrinsics.to_matrix(), self.distortion.to_vector().ravel()
    )

StereoCamera(camera_1, camera_2) dataclass

Stereo camera pair.

Attributes:

Name Type Description
camera_1 CameraBase

First camera. By convention, the left camera.

camera_2 CameraBase

Second camera. By convention, the right camera.

relative_pose property

Calculate the relative pose between the two cameras. This is the pose of camera_2 in camera_1's frame.

Returns:

Type Description
Pose3D

Relative pose between the two cameras.

project(points, points_frame=None)

Project a set of 3D points into both image planes.

Parameters:

Name Type Description Default
points ndarray

3D points as a 3xN numpy array.

required
points_frame Optional[Pose3D]

Pose of the points frame relative to the camera_1 frame. If None, the points are assumed to be in the camera frame.

None

Returns:

Type Description
ndarray

Projected points for camera_1 as a 2xN numpy array.

ndarray

Projected points for camera_2 as a 2xN numpy array.

Source code in src/compas_camcal/models/camera.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
def project(
    self, points: np.ndarray, points_frame: Optional[Pose3D] = None
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Project a set of 3D points into both image planes.

    Args:
        points: 3D points as a 3xN numpy array.
        points_frame: Pose of the points frame relative to the camera_1 frame. If None, the points are assumed to be in the camera frame.

    Returns:
        Projected points for camera_1 as a 2xN numpy array.
        Projected points for camera_2 as a 2xN numpy array.
    """
    # Project points into each image plane
    image_points_1 = self.camera_1.project(points, points_frame=points_frame)
    image_points_2 = self.camera_2.project(
        points, points_frame=self.relative_pose @ points_frame
    )

    return image_points_1, image_points_2