Python

Rendering a 3D scene given 3D coordinate points using open3D

ddokkddokk 2025. 2. 13. 13:38
반응형
import open3d as o3d
import numpy as np

# Function to create a vehicle bounding box
def create_vehicle_bbox(center, size, color=[0, 0, 1]):
    """Create a vehicle bounding box in Open3D."""
    bbox = o3d.geometry.OrientedBoundingBox(center=center, R=np.eye(3), extent=size)
    bbox.color = color  # Blue for vehicles
    return bbox

# Function to create road lines (HD map elements)
def create_map_lines(map_points, color=[1, 0, 0]):
    """Create HD map road lines as Open3D LineSet."""
    lines = [[i, i + 1] for i in range(len(map_points) - 1)]  # Connect consecutive points
    line_set = o3d.geometry.LineSet()
    line_set.points = o3d.utility.Vector3dVector(map_points)
    line_set.lines = o3d.utility.Vector2iVector(lines)
    line_set.colors = o3d.utility.Vector3dVector([color] * len(lines))  # Red for roads
    return line_set

# Example data (Replace with actual dataset)
vehicle_positions = [
    ([5, 5, 0], [2, 1, 1]),  # (center, size)
    ([10, -5, 0], [2, 1, 1])
]

map_elements = np.array([
    [0, 0, 0], [2, 0, 0], [4, 1, 0], [6, 2, 0], [8, 3, 0], [10, 5, 0]
])  # Road lane points

# Create Open3D visualization objects
vis_objects = []
for pos, size in vehicle_positions:
    vis_objects.append(create_vehicle_bbox(np.array(pos), np.array(size)))

# Add road lines
vis_objects.append(create_map_lines(map_elements))

# Create an Open3D Visualizer with black background
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.get_render_option().background_color = np.array([0, 0, 0])  # Black background

# Add a coordinate frame (default size = 1.0, origin at [0, 0, 0])
coordinate_frame = o3d.geometry.TriangleMesh.create_coordinate_frame(size=1.0, origin=[0, 0, 0])
vis.add_geometry(coordinate_frame)

# Add all objects to the visualizer
for obj in vis_objects:
    vis.add_geometry(obj)

# # Set Bird’s-Eye View
# view_control = vis.get_view_control()
# view_control.set_lookat([0, 0, 0])   # Center of the scene (adjust as needed)
# view_control.set_front([0, 0, 1])   # Camera looking directly downward
# view_control.set_up([1, 0, 0])       # Y-axis as the up direction
# view_control.set_zoom(1.0)           # Adjust zoom for better framing

# Set Slightly Behind and Above View
view_control = vis.get_view_control()
view_control.set_lookat([0, 0, 0])   # Center of the scene
view_control.set_front([-1, 0, 1])  # Slightly behind and above
view_control.set_up([1, 0, 0])       # Keep Z-axis as up direction
view_control.set_zoom(1.0)           # Adjust zoom for better framing

# Render the scene (without opening a window)
vis.poll_events()
vis.update_renderer()

# Capture the scene as a NumPy array
image = vis.capture_screen_float_buffer(do_render=True)  # Capture image buffer
image_np = (np.asarray(image) * 255).astype(np.uint8)  # Convert to 8-bit NumPy array

vis.destroy_window()  # Close the offscreen renderer

import cv2
cv2.imshow("", image_np.astype('uint8'))
cv2.waitKey(0)