我正在尝试通过python中的open3d可视化来自rostopic的pointcloud2流。
这是我的代码:
import sensor_msgs.point_cloud2 as pc2
import open3d
...
def callback(self, points):
#self.pc = pcl.VoxelGridFilter(self.pc)
self.pc = self.convertCloudFromRosToOpen3d(points)
if self.first:
self.first = False
self.vis.create_window()
rospy.loginfo('plot')
self.vis.add_geometry(self.pc)
self.vis.update_geometry()
self.vis.poll_events()
self.vis.update_renderer()
self.vis.run()
else:
rospy.loginfo('update')
self.vis.update_geometry()
self.vis.poll_events()
self.vis.update_renderer()
self.vis.run()
def listener(self):
rospy.init_node('ui_config_node', anonymous=True)
rospy.Subscriber('/kinect2/sd/points', PointCloud2, self.callback)
rospy.spin()
如果我启动此代码,只会得到冻结的图片。
我使用this脚本将pointCloud2转换为open3d格式。
如果有人有另一个想法可以使rospy中的pointcloud2可视化,我将很高兴听到它。
感谢您的帮助和建议!
答案 0 :(得分:0)
我知道了。
rospy.spin()
阻止可视化。
所以我的最终代码
罗斯节点:
if __name__ == '__main__':
listener = CameraListner()
updater = Viewer(listener)
rospy.spin()
CameraListner
class CameraListner():
def __init__(self):
self.pc = None
self.n = 0
self.listener()
############################################################################
def callback(self, points):
self.pc = points
self.n = self.n + 1
def listener(self):
rospy.init_node('ui_config_node', anonymous=True)
rospy.Subscriber('/kinect2/sd/points', PointCloud2, self.callback)
以及至少Viewer类
class ViewerWidget(QtWidgets.QWidget):
def __init__(self, subscriber, parent=None):
self.subscriber = subscriber
rospy.loginfo('initialization')
self.vis = open3d.visualization.Visualizer()
self.point_cloud = None
self.updater()
############################################################################
def updater(self):
rospy.loginfo('start')
self.first = False
while (self.subscriber.pc is None):
time.sleep(2)
self.point_cloud = open3d.geometry.PointCloud()
self.point_cloud.points = open3d.utility.Vector3dVector(ros_numpy.point_cloud2.pointcloud2_to_xyz_array(self.subscriber.pc))
self.vis.create_window()
print('get points')
self.vis.add_geometry(self.point_cloud)
print ('add points')
self.vis.update_geometry()
self.vis.poll_events()
self.vis.update_renderer()
while not rospy.is_shutdown():
self.point_cloud.points = open3d.utility.Vector3dVector(ros_numpy.point_cloud2.pointcloud2_to_xyz_array(self.subscriber.pc))
self.vis.update_geometry()
self.vis.poll_events()
self.vis.update_renderer()
我还更改了将PointCloud2转换为o3d pointcloud的方法