Skip to content

Commit eee2a3a

Browse files
authored
feat(realsense): align depth and rgb option (#257)
1 parent 7ae3cc3 commit eee2a3a

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

  • extensions/rcs_realsense/src/rcs_realsense

extensions/rcs_realsense/src/rcs_realsense/camera.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@ def __init__(
4141
enable_ir: bool = False,
4242
laser_power: int = 330,
4343
enable_imu: bool = False,
44+
align_depth_to_color: bool = False,
4445
) -> None:
4546
self.enable_ir_emitter = enable_ir_emitter
4647
self.enable_ir = enable_ir
4748
self.laser_power = laser_power
4849
self.enable_imu = enable_imu
4950
self.cameras = cameras
51+
self.align_depth_to_color = align_depth_to_color
5052
if calibration_strategy is None:
5153
calibration_strategy = {camera_name: DummyCalibrationStrategy() for camera_name in cameras}
5254
self.calibration_strategy = calibration_strategy
@@ -241,6 +243,11 @@ def poll_frame(self, camera_name: str) -> Frame:
241243
streams = device.pipeline_profile.get_streams()
242244
frameset = device.pipeline.wait_for_frames()
243245

246+
if self.align_depth_to_color:
247+
# replaces the frameset with a composite frameset containing the aligned depth
248+
align = rs.align(rs.stream.color)
249+
frameset = align.process(frameset)
250+
244251
color: DataFrame | None = None
245252
ir: DataFrame | None = None
246253
depth: DataFrame | None = None
@@ -255,11 +262,18 @@ def to_ts(frame: rs.frame) -> float:
255262
return frame.get_timestamp() * RealSenseCameraSet.TIMESTAMP_FACTOR
256263

257264
color_extrinsics = self.calibration_strategy[camera_name].get_extrinsics()
258-
depth_to_color = device.depth_to_color
259-
assert depth_to_color is not None, "Depth to color extrinsics not found"
260-
depth_extrinsics = (
261-
color_extrinsics @ depth_to_color.inverse().pose_matrix() if color_extrinsics is not None else None
262-
)
265+
266+
if self.align_depth_to_color:
267+
# if aligned, depth acts as if it was shot from the color sensor
268+
depth_extrinsics = color_extrinsics
269+
active_depth_intrinsics = device.color_intrinsics
270+
else:
271+
depth_to_color = device.depth_to_color
272+
assert depth_to_color is not None, "Depth to color extrinsics not found"
273+
depth_extrinsics = (
274+
color_extrinsics @ depth_to_color.inverse().pose_matrix() if color_extrinsics is not None else None
275+
)
276+
active_depth_intrinsics = device.depth_intrinsics
263277

264278
timestamps = []
265279
for stream in streams:
@@ -282,7 +296,7 @@ def to_ts(frame: rs.frame) -> float:
282296
np.uint16
283297
),
284298
timestamp=to_ts(frame),
285-
intrinsics=device.depth_intrinsics,
299+
intrinsics=active_depth_intrinsics,
286300
extrinsics=depth_extrinsics, # type: ignore
287301
)
288302
elif rs.stream.accel == stream.stream_type():

0 commit comments

Comments
 (0)