import sys
sys.path.append("/workspace/include")
from ddynamic_reconfigure import DDynamicReconfigure
import rospy
from sensor_msgs.msg import PointCloud2
import numpy as np
import pcl
import pcl_msg
import pcl_helper
import argparse
import pickle
class DyConfigure(object):
def __init__(self):
self.ddr = DDynamicReconfigure("BgRemoval")
self.ddr.add_variable("resolution", "float/double variable", 0.1, 0.0, 1.0)
self.add_variables_to_self()
self.ddr.start(self.dyn_rec_callback)
def add_variables_to_self(self):
var_names = self.ddr.get_variable_names()
for var_name in var_names:
self.__setattr__(var_name, None)
def dyn_rec_callback(self, config, level):
rospy.loginfo("Received reconf call: " + str(config))
var_names = self.ddr.get_variable_names()
for var_name in var_names:
self.__dict__[var_name] = config[var_name]
return config
class BgRemoval:
def __init__(self):
self.lidar = rospy.Subscriber(args.topic, PointCloud2, self.callback)
self.pub_bg = rospy.Publisher("/velodyne_bg_201", PointCloud2, queue_size=1)
self.searchPoint = pcl.PointCloud(pickle.load(open(args.baseline, 'rb')))
self.E = DyConfigure()
def background_removal(self, daytime, nighttime):
resolution = self.E.resolution
octree = nighttime.make_octreeChangeDetector(resolution)
octree.add_points_from_input_cloud ()
octree.switchBuffers ()
daytime = pcl_helper.XYZRGB_to_XYZ(daytime)
octree.set_input_cloud(daytime)
octree.add_points_from_input_cloud ()
newPointIdxVector = octree.get_PointIndicesFromNewVoxels ()
daytime.extract(newPointIdxVector)
result = np.zeros((len(newPointIdxVector)+1, 3), dtype=np.float32)
for i in range(0, len(newPointIdxVector)):
result[i][0] = daytime[newPointIdxVector[i]][0]
result[i][1] = daytime[newPointIdxVector[i]][1]
result[i][2] = daytime[newPointIdxVector[i]][2]
pc = pcl.PointCloud(result)
cloud = pcl_helper.XYZ_to_XYZRGB(pc,[255,255,255])
return cloud
def callback(self, input_ros_msg):
pcl_xyzrgb = pcl_helper.ros_to_pcl(input_ros_msg)
pcl_xyz = pcl_helper.XYZRGB_to_XYZ(pcl_xyzrgb)
pcl_xyzrgb = self.background_removal(pcl_xyz, self.searchPoint)
bg_ros_msg = pcl_helper.pcl_to_ros(pcl_xyzrgb)
self.pub_bg.publish(bg_ros_msg)
if __name__ == '__main__':
rospy.init_node('BackgroundRemoval', anonymous=True)
parser = argparse.ArgumentParser()
parser.add_argument("--baseline", default="./baseline_201.pkl", help="Use baseline rosbag to remove background")
parser.add_argument("--topic", default="/lidar_201/velodyne_points", help="Input Topic")
args = parser.parse_args()
print("\nLoad baseline(--baseline) : {}".format(args.baseline))
print("Input Topic(--topic) : {}".format(args.topic))
print("Ouput Topic(FIXED) : /velodyne_bg_201")
bg = BgRemoval()
rospy.spin()