Transform¶
The charucal.transform module provides the classes responsible for transforming live camera frames into a top-down view.
Overview¶
CameraTransformer is the main class you interact
with at runtime. It loads a saved calibration, precomputes remap tables once during
initialization, and then applies them on every call to transform().
import cv2
from charucal import CalibrationResult, CameraTransformer, RenderDistance
calibration = CalibrationResult.load("calibration.json")
transformer = CameraTransformer(
calibration,
render_distance=RenderDistance(front=10.0, lateral=5.0),
output_shape=(1000, 1000),
interpolation=cv2.INTER_LINEAR, # or cv2.INTER_NEAREST for maximum speed
)
topdown = transformer.transform(frames) # list[np.ndarray] -> np.ndarray (H x W x 3 BGR)
Controlling the render area¶
RenderDistance defines the physical extent of the scene shown in the output image.
front— meters from the camera position to the top edge of the outputlateral— meters from the center line to the left and right edges of the output
The pixels-per-meter scale is chosen automatically to fill output_shape while preserving
the aspect ratio implied by front and lateral.
Measuring distances in the output¶
CameraTransformer provides helpers to convert between output pixel coordinates and meters:
# Forward distance to any output pixel
dist_m = transformer.get_distance_forward(y=300)
# Euclidean distance to any output pixel
dist_m = transformer.get_distance_to_point(x=500, y=200)
# Map a point from a source camera into the output image
out_x, out_y = transformer.transform_point(x=640, y=360, camera_idx=0)
# Distance to a point in a source camera image
dist_m = transformer.get_distance_to_camera_point(x=640, y=360, camera_idx=0)
Parallelism¶
By default (n_jobs=-1), all CPU threads are used to remap cameras in parallel. Set
n_jobs=1 to disable threading. This can reduce latency when the number of cameras is small and the output resolution
is large, since the overhead of threading can outweigh the benefits in that case.
Reference¶
RenderDistance
dataclass
¶
The physical extent of the scene captured in the top-down image.
Methods:
| Name | Description |
|---|---|
__post_init__ |
Initialize the render distance dataclass. |
Attributes:
| Name | Type | Description |
|---|---|---|
front |
float
|
The distance in meters from the nearest visible road point to the far edge. |
lateral |
float
|
The distance in meters from the center of the output image to the left and right edges. |
front
instance-attribute
¶
The distance in meters from the nearest visible road point to the far edge.
lateral
instance-attribute
¶
The distance in meters from the center of the output image to the left and right edges.
__post_init__
¶
Initialize the render distance dataclass.
Source code in src/charucal/transform.py
CameraTransformer
¶
CameraTransformer(
calibration: CalibrationResult,
*,
render_distance: RenderDistance,
output_shape: tuple[int, int],
input_shapes: Sequence[tuple[int, int]] | None = None,
interpolation: int = cv2.INTER_LINEAR,
n_jobs: int = -1,
)
Transforms multi-camera images into a single top-down view.
Initialize the transformer and precompute all remap operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calibration
|
CalibrationResult
|
The result of a prior multi-camera calibration. |
required |
render_distance
|
RenderDistance
|
The physical scene extents to render (in meters). |
required |
output_shape
|
tuple[int, int]
|
The |
required |
input_shapes
|
Sequence[tuple[int, int]] | None
|
The |
None
|
interpolation
|
int
|
The OpenCV interpolation flag passed to |
INTER_LINEAR
|
n_jobs
|
int
|
The number of worker threads. |
-1
|
Methods:
| Name | Description |
|---|---|
get_distance |
Convert a pixel distance in the output image to meters. |
get_pixels |
Convert a physical distance in meters to output pixels. |
get_distance_forward |
Return the forward distance in meters to a horizontal scanline. |
get_distance_to_point |
Return the Euclidean distance in meters from the camera to an output pixel. |
get_distance_forward_to_camera_point |
Return the forward (depth) distance in meters to a point in a camera image. |
get_distance_to_camera_point |
Return the Euclidean distance in meters from the camera to a point in a camera image. |
transform |
Transform a list of camera images into a single top-down view. |
transform_point |
Transform a point from a camera image into the top-down output image. |
Attributes:
| Name | Type | Description |
|---|---|---|
output_shape |
tuple[int, int]
|
The |
pixels_per_meter |
float
|
The number of output pixels corresponding to one meter in the scene. |
Source code in src/charucal/transform.py
output_shape
property
¶
The (width, height) of the output image in pixels.
pixels_per_meter
property
¶
The number of output pixels corresponding to one meter in the scene.
get_distance
¶
get_pixels
¶
get_distance_forward
¶
get_distance_to_point
¶
Return the Euclidean distance in meters from the camera to an output pixel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
int
|
The x-coordinate of the output pixel. |
required |
y
|
int
|
The y-coordinate of the output pixel. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The distance in meters. |
Source code in src/charucal/transform.py
get_distance_forward_to_camera_point
¶
Return the forward (depth) distance in meters to a point in a camera image.
Only the projected y-coordinate in the output image is used, so lateral offset is ignored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x-coordinate in the source camera image. |
required |
y
|
float
|
The y-coordinate in the source camera image. |
required |
camera_idx
|
int
|
The index of the source camera in the calibration. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The forward distance in meters. |
Source code in src/charucal/transform.py
get_distance_to_camera_point
¶
Return the Euclidean distance in meters from the camera to a point in a camera image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x-coordinate in the source camera image. |
required |
y
|
float
|
The y-coordinate in the source camera image. |
required |
camera_idx
|
int
|
The index of the source camera in the calibration. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The Euclidean distance in meters. |
Source code in src/charucal/transform.py
transform
¶
Transform a list of camera images into a single top-down view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
list[NDArray[uint8]]
|
The camera images ordered to match the calibration. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
The composited top-down image. |
Source code in src/charucal/transform.py
transform_point
¶
Transform a point from a camera image into the top-down output image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
The x-coordinate in the source camera image. |
required |
y
|
float
|
The y-coordinate in the source camera image. |
required |
camera_idx
|
int
|
The index of the source camera in the calibration. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
The |