""" Visualize 6D pose annotations by projecting local frame axes onto RGB images. Coordinate conventions ---------------------- - All matrices are **row-major** (Isaac Sim / USD convention). Transforms are applied as: p_out = p_in @ M (row vector on the left) - World / object space uses a **right-handed, Y-up** frame (OpenGL convention). +X : right +Y : up -Z : into the scene (camera looks toward -Z in camera space) - Camera space also follows **OpenGL**: +X : right, +Y : up, -Z : into the scene - Screen space follows **OpenCV / image** convention: +u : right, +v : down, origin at top-left Conversion from OpenGL camera space → OpenCV screen space: x_cv = x_gl y_cv = -y_gl (flip Y) z_cv = -z_gl (flip Z; positive depth is in front) Matrix fields in the JSON -------------------------- - camera_view_matrix (4x4, row-major) : world → camera - local_to_world_transform (4x4, row-major) : object-local → world - intrinsics : {fx, fy, cx, cy} in pixels """ import json import numpy as np from PIL import Image, ImageDraw import argparse def project_world_point_to_screen(world_point, view_matrix, intrinsics): """Project a homogeneous world-space point to pixel coordinates. Uses row-major convention: cam = world_point @ view_matrix. Converts OpenGL camera space (y-up, -z forward) to OpenCV screen space (y-down, +z forward). Returns None if the point is behind the camera (z_cv <= 0). """ p = np.array([*world_point[:3], 1.0]) if len(world_point) == 3 else np.array(world_point) cam = p @ view_matrix # row-major: point on the left x, y, z = cam[0], -cam[1], -cam[2] # OpenGL → OpenCV axis flip if z <= 0: return None u = intrinsics['fx'] * x / z + intrinsics['cx'] v = intrinsics['fy'] * y / z + intrinsics['cy'] return round(u), round(v) def draw_local_frame_axes(draw, local_to_world_transform, camera_view_matrix, intrinsics, size_local, origin_local, axes_length_perc=1.5): """Draw X/Y/Z axes of the object's local frame onto the image. Axis length is scaled by the mean object size * axes_length_perc. Pipeline: local → world (@ L), world → screen (project_world_point_to_screen). """ L = np.array(local_to_world_transform) # row-major local→world (4x4) V = np.array(camera_view_matrix) # row-major world→camera (4x4) ax_len = np.mean(size_local) * axes_length_perc ox, oy, oz = origin_local points_local = { 'origin': [ox, oy, oz, 1], 'x': [ox + ax_len, oy, oz, 1], 'y': [ox, oy + ax_len, oz, 1], 'z': [ox, oy, oz + ax_len, 1], } pts2d = {k: project_world_point_to_screen(np.array(v) @ L, V, intrinsics) for k, v in points_local.items()} o = pts2d['origin'] for key, color in [('x', 'red'), ('y', 'green'), ('z', 'blue')]: if o is not None and pts2d[key] is not None: draw.line([o, pts2d[key]], fill=color, width=5) def draw_world_frame_axes_bottom_left(draw, camera_view_matrix, intrinsics, screen_size, axes_scale=0.1, margin_percentage=0.05): """Draw world-frame X/Y/Z axes in the bottom-left corner as a reference gizmo. Places a virtual origin 1 unit in front of the camera (OpenGL: z=-1 in camera space), converts it to world space via the inverse view matrix, then re-projects to screen. The axes are offset so they appear anchored to the bottom-left corner. """ V = np.array(camera_view_matrix) V_inv = np.linalg.inv(V) # z=-1 in OpenGL camera space = 1 unit in front of the camera origin_world = np.array([0, 0, -1.0, 1]) @ V_inv # camera → world pts2d = {} pts2d['origin'] = project_world_point_to_screen(origin_world, V, intrinsics) for key, delta in [('x', [axes_scale, 0, 0, 0]), ('y', [0, axes_scale, 0, 0]), ('z', [0, 0, axes_scale, 0])]: pts2d[key] = project_world_point_to_screen(origin_world + np.array(delta), V, intrinsics) if any(v is None for v in pts2d.values()): return # Shift projected axes to bottom-left corner margin = int(margin_percentage * min(screen_size)) all_x = [pts2d[k][0] for k in pts2d] all_y = [pts2d[k][1] for k in pts2d] ox = margin - min(all_x) oy = screen_size[1] - margin - max(all_y) o = (pts2d['origin'][0] + ox, pts2d['origin'][1] + oy) for key, color in [('x', 'red'), ('y', 'green'), ('z', 'blue')]: end = (pts2d[key][0] + ox, pts2d[key][1] + oy) draw.line([o, end], fill=color, width=3) def main(image_path, json_path, output_path): rgb_img = Image.open(image_path) draw = ImageDraw.Draw(rgb_img) with open(json_path, 'r') as f: data = json.load(f) camera_data = data["camera_data"] V = camera_data["camera_view_matrix"] # row-major world→camera (4x4) intrinsics = camera_data["intrinsics"] # {fx, fy, cx, cy} in pixels screen_size = tuple(camera_data["resolution"]) # (width, height) for obj in data["objects"]: b = obj["bbox_3d_local"] size_local = [b["x_max"] - b["x_min"], b["y_max"] - b["y_min"], b["z_max"] - b["z_min"]] center_local = [(b["x_min"] + b["x_max"]) / 2, (b["y_min"] + b["y_max"]) / 2, (b["z_min"] + b["z_max"]) / 2] draw_local_frame_axes(draw, obj["local_to_world_transform"], V, intrinsics, size_local, center_local) draw_world_frame_axes_bottom_left(draw, V, intrinsics, screen_size) rgb_img.save(output_path) print(f"Overlay image saved to: {output_path}") if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--image", type=str, required=True) parser.add_argument("--json", type=str, required=True) parser.add_argument("--output", type=str, required=True) args = parser.parse_args() main(args.image, args.json, args.output)