1- from sensor_msgs .msg import PointCloud2
1+ from sensor_msgs .msg import PointCloud2 , PointField
22import numpy as np
33import sensor_msgs .point_cloud2 as pc2
44import open3d as o3d
55import cv2
66import json
7+ import rospy
8+ import numpy as np
9+ import struct
710
811
912def convert_pointcloud2_to_xyz (lidar_pc2_msg : PointCloud2 ):
@@ -85,7 +88,7 @@ def project_points(points_3d, K):
8588 if pt [2 ] > 0 : # only project points in front of the camera
8689 u = K [0 , 0 ] * (pt [0 ] / pt [2 ]) + K [0 , 2 ]
8790 v = K [1 , 1 ] * (pt [1 ] / pt [2 ]) + K [1 , 2 ]
88- proj_points .append ((int (u ), int (v )))
91+ proj_points .append ((int (u ), int (v ), pt [ 0 ], pt [ 1 ], pt [ 2 ] ))
8992 return proj_points
9093
9194
@@ -134,10 +137,16 @@ def visualize_point_cloud(points):
134137 Args:
135138 points (np.ndarray): Nx3 array of point cloud coordinates.
136139 """
140+ # Create a visualization window
141+ vis = o3d .visualization .Visualizer ()
142+ vis .create_window ()
143+
137144 pc = o3d .geometry .PointCloud ()
138145 pc .points = o3d .utility .Vector3dVector (points )
139- pc .paint_uniform_color ([0.1 , 0.7 , 0.9 ]) # Light blue color
140- o3d .visualization .draw_geometries ([pc ])
146+ pc .paint_uniform_color ([0.1 , 0.7 , 0.9 ])
147+
148+ vis .add_geometry (pc )
149+ vis .run ()
141150
142151
143152def visualize_plane (inlier_cloud , outlier_cloud , bounding_box_2d_points ):
@@ -168,4 +177,28 @@ def visualize_plane(inlier_cloud, outlier_cloud, bounding_box_2d_points):
168177 bounding_box_lines .paint_uniform_color ([0 , 1 , 0 ]) # Green for bounding box edges
169178
170179 # Visualize
171- o3d .visualization .draw_geometries ([inlier_cloud , outlier_cloud , bounding_box_pcd , bounding_box_lines ])
180+ o3d .visualization .draw_geometries ([inlier_cloud , outlier_cloud , bounding_box_pcd , bounding_box_lines ])
181+
182+
183+ def create_point_cloud (points , color = (255 , 0 , 0 )):
184+ """
185+ Converts a list of (x, y, z) points into a PointCloud2 message.
186+ """
187+ header = rospy .Header ()
188+ header .stamp = rospy .Time .now ()
189+ header .frame_id = "map" # Change to your TF frame
190+
191+ fields = [
192+ PointField (name = "x" , offset = 0 , datatype = PointField .FLOAT32 , count = 1 ),
193+ PointField (name = "y" , offset = 4 , datatype = PointField .FLOAT32 , count = 1 ),
194+ PointField (name = "z" , offset = 8 , datatype = PointField .FLOAT32 , count = 1 ),
195+ PointField (name = "rgb" , offset = 12 , datatype = PointField .FLOAT32 , count = 1 ),
196+ ]
197+
198+ # Convert RGB color to packed float32
199+ r , g , b = color
200+ packed_color = struct .unpack ('f' , struct .pack ('I' , (r << 16 ) | (g << 8 ) | b ))[0 ]
201+
202+ point_cloud_data = [(x , y , z , packed_color ) for x , y , z in points ]
203+
204+ return pc2 .create_cloud (header , fields , point_cloud_data )
0 commit comments