Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to visualize the PCD segemntation with open3d, pcl, cloudcompare #309

Open
yekruz opened this issue Dec 31, 2024 · 0 comments
Open
Assignees

Comments

@yekruz
Copy link

yekruz commented Dec 31, 2024

Hi,

I have tried several ways to viusalize the semantic segmentation annotation outputs in pcd format, but the annotations are not being shown. Any help is appreciated.

I am using the following code. I see the original pcd ,but the annotation are not shown. Only reading the PCD alone also didn't work.

import open3d as o3d
import numpy as np

# Read the original point cloud with coordinates
pcd = o3d.io.read_point_cloud("20220929_083807_525890.pcd")

# Read segmentation data
with open('20220929_083807_549617_lidar_point_cloud_0_segmentation.pcd', 'rb') as f:
    # Skip header
    for _ in range(11):
        f.readline()

    seg_data = np.frombuffer(f.read(), dtype=np.uint8)

# From the histogram, we can see major semantic classes
semantic_colors = {
    1: [1, 0, 0],     # Red (largest peak in histogram)
    128: [0, 1, 0],   # Green (second major peak)
    156: [0, 0, 1],   # Blue
    40: [1, 1, 0],    # Yellow
    122: [1, 0, 1],   # Magenta
    8: [0, 1, 1],     # Cyan
}

# Create colors array
colors = np.zeros((len(seg_data), 3))

print(seg_data)
# Assign colors based on semantic classes
for class_id, color in semantic_colors.items():
    mask = (seg_data == class_id)
    colors[mask] = color

# For unassigned classes, use gray
unassigned_mask = ~np.any([seg_data == key for key in semantic_colors.keys()], axis=0)
colors[unassigned_mask] = [0.5, 0.5, 0.5]  # Gray for unassigned points

# Assign colors to point cloud
pcd.colors = o3d.utility.Vector3dVector(colors)

# Visualization
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)

# Improve visualization settings
opt = vis.get_render_option()
opt.background_color = np.asarray([0, 0, 0])  # Black background
opt.point_size = 2.0

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

vis.run()
vis.destroy_window()

for reading the annotation PCD file alone:

import open3d as o3d
import numpy as np

# Read the PCD file
pcd = o3d.io.read_point_cloud("20220929_083807_549617_lidar_point_cloud_0_segmentation.pcd")

# Assuming semantic labels are stored in the 'labels' attribute
labels = np.asarray(pcd.labels)

# Create a color map for different semantic classes
color_map = {
    0: [1, 0, 0],  # Red for class 0
    1: [0, 1, 0],  # Green for class 1
    2: [0, 0, 1],  # Blue for class 2
    # Add more colors for additional classes
}

# Assign colors based on semantic labels
colors = np.array([color_map[label] for label in labels])
pcd.colors = o3d.utility.Vector3dVector(colors)

# Visualize the semantically colored point cloud
o3d.visualization.draw_geometries([pcd])

using the output with x,y,z,seg also does not work and the following code, colorizes the objects instance-wise and not semantic-wise

import open3d as o3d
import numpy as np
import struct

def read_binary_pcd(filename):
    with open(filename, 'rb') as f:
        # Skip header
        for _ in range(11):
            f.readline()
        binary_data = f.read()
        
        fmt = 'fffi'
        size = struct.calcsize(fmt)
        num_points = len(binary_data) // size
        points = []
        for i in range(num_points):
            offset = i * size
            pt = struct.unpack(fmt, binary_data[offset:offset + size])
            points.append(pt)
        
        points = np.array(points)
        return points[:, :3], points[:, 3].astype(np.int32)

# Define semantic class colors based on the plateaus in the visualization
semantic_colors = {
    0: [0.7, 0.7, 0.7],    # background
    10: [1.0, 0.0, 0.0],   # road
    40: [0.0, 1.0, 0.0],   # vegetation
    80: [0.0, 0.0, 1.0],   # building
    120: [1.0, 1.0, 0.0],  # car
    130: [1.0, 0.0, 1.0],  # pedestrian
    140: [0.0, 1.0, 1.0],  # pole
    150: [0.8, 0.4, 0.0],  # traffic sign
    160: [0.5, 0.5, 0.5]   # other
}

# Read point cloud
xyz, seg_labels = read_binary_pcd("20220929_084039_549664_lidar_point_cloud_0_segmentation.pcd")

# Create Open3D point cloud
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)

# Create colors array
colors = np.zeros((len(seg_labels), 3))

# Assign colors based on semantic classes
# Round the segmentation labels to the nearest semantic class
for label in np.unique(seg_labels):
    # Find the closest semantic class
    closest_class = min(semantic_colors.keys(), key=lambda x: abs(x - label))
    mask = (seg_labels == label)
    colors[mask] = semantic_colors[closest_class]

# Assign colors to point cloud
pcd.colors = o3d.utility.Vector3dVector(colors)

# Visualization
vis = o3d.visualization.Visualizer()
vis.create_window()
vis.add_geometry(pcd)

# Improve visualization settings
opt = vis.get_render_option()
opt.background_color = np.asarray([0, 0, 0])
opt.point_size = 2.0

vis.run()
vis.destroy_window()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants