Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

PinholeCamera

Often we optimize a camera’s location and orientation (extrinsics) together with its calibration parameters (intrinsics). PinholeCamera<CALIBRATION> combines both in one value and provides projection, backprojection, reprojection error, and camera geometry; Python exposes one concrete class per wrapped calibration model.

Open In Colab
import gtsam
import numpy as np

Creating a calibrated pinhole camera

This example uses PinholeCameraCal3_S2; the Cal3DS2, Cal3Unified, Cal3Bundler, Cal3f, and Cal3Fisheye specializations have the same camera interface.

K = gtsam.Cal3_S2(800.0, 790.0, 0.0, 320.0, 240.0)
pose = gtsam.Pose3(gtsam.Rot3.Yaw(0.1), np.array([1.0, 0.0, 0.0]))
camera = gtsam.PinholeCameraCal3_S2(pose, K)
print("camera center:", camera.pose().translation())
print("focal length:", camera.calibration().fx())

Projection and visibility

project() maps a world point to pixels. projectSafe() additionally reports whether the point is in front of the camera, and reprojectionError() compares a point with an observed pixel measurement.

point = np.array([1.5, 0.2, 5.0])
pixel = camera.project(point)
safe_pixel, visible = camera.projectSafe(point)
print("pixel:", pixel, "visible:", visible)
np.testing.assert_allclose(safe_pixel, pixel)
np.testing.assert_allclose(camera.reprojectionError(point, pixel), np.zeros(2))

Backprojection and range

backproject(pixel, depth) recovers a world point at a specified camera-frame depth. range() reports Euclidean distance instead, so depth and range differ away from the optical axis.

depth = camera.pose().transformTo(point)[2]
recovered = camera.backproject(pixel, depth)
np.testing.assert_allclose(recovered, point, atol=1e-9)
print("depth:", depth, "range:", camera.range(point))

Source

PinholeCamera.h

AI assistance caveat

AI was used to help draft this documentation, and inaccuracies could be present.