13 if __name__ == 
'__main__':
    16     parser = argparse.ArgumentParser(description=
'''    17     This scripts reads a bag file containing RGBD data,     18     adds the corresponding PointCloud2 messages, and saves it again into a bag file.     19     Optional arguments allow to select only a portion of the original bag file.      21     parser.add_argument(
'--start', help=
'skip the first N seconds of  input bag file (default: 0.0)',default=0.00)
    22     parser.add_argument(
'--duration', help=
'only process N seconds of input bag file (default: off)')
    23     parser.add_argument(
'--nth', help=
'only process every N-th frame of input bag file (default: 15)',default=15)
    24     parser.add_argument(
'--skip', help=
'skip N blocks in the beginning (default: 1)', default=1)
    25     parser.add_argument(
'--compress', help=
'compress output bag file', action=
'store_true')
    26     parser.add_argument(
'inputbag', help=
'input bag file')
    27     parser.add_argument(
'outputbag', nargs=
'?',help=
'output bag file')
    28     args = parser.parse_args()
    30     import roslib; roslib.load_manifest(
'rgbd_rosbag_tools')
    33     import sensor_msgs.msg
    35     from cv_bridge 
import CvBridge, CvBridgeError
    38     if not args.outputbag:
    39         args.outputbag = os.path.splitext(args.inputbag)[0] + 
"-points.bag"    41     print "Processing bag file:"    42     print "  in:",args.inputbag
    43     print "  out:",args.outputbag
    44     print "  starting from: %s seconds"%(args.start)
    47         print "  duration: %s seconds"%(args.duration)
    49     print "  saving every %s-th frame"%(args.nth)
    50     args.skip = float(args.skip)
    51     print "  skipping %s blocks"%(args.skip)
    55         param_compression = rosbag.bag.Compression.BZ2
    57         param_compression = rosbag.bag.Compression.NONE
    59     outbag = 
rosbag.Bag(args.outputbag, 
'w', compression=param_compression)
    61     depth_camera_info = 
None    62     rgb_camera_info = 
None    64     rgb_image_color = 
None    72     for topic, msg, t 
in inbag.read_messages():
    75         if t - time_start < rospy.Duration.from_sec(float(args.start)):
    77         if args.duration 
and (t - time_start > rospy.Duration.from_sec(float(args.start) + float(args.duration))):
    79         print "t=%f\r"%(t-time_start).to_sec(),
    80         if topic == 
"/camera/depth_registered/camera_info":
    81             depth_camera_info = msg
    83         if topic == 
"/camera/rgb/camera_info":
    86         if topic == 
"/camera/rgb/image_color" and rgb_camera_info:
    89         if topic == 
"/camera/depth_registered/image" and depth_camera_info 
and rgb_image_color 
and rgb_camera_info:
    93             if depth_image.header.stamp - rgb_image_color.header.stamp > rospy.Duration.from_sec(1/30.0):
    97             if frame % float(args.nth) == 0:
   102                     outbag.write(
"/camera/depth_registered/camera_info",depth_camera_info,t)
   103                     outbag.write(
"/camera/depth_registered/image",depth_image,t)
   104                     outbag.write(
"/camera/rgb/camera_info",rgb_camera_info,t)
   105                     outbag.write(
"/camera/rgb/image_color",rgb_image_color,t)
   108                     cv_rgb_image_mono = bridge.imgmsg_to_cv(rgb_image_color, 
"mono8")
   109                     rgb_image_mono = bridge.cv_to_imgmsg(cv_rgb_image_mono)
   110                     rgb_image_mono.header = rgb_image_color.header
   111                     outbag.write(
"/camera/rgb/image_mono",rgb_image_mono,t)
   114                     cv_depth_image = bridge.imgmsg_to_cv(depth_image, 
"passthrough")
   115                     cv_rgb_image_color = bridge.imgmsg_to_cv(rgb_image_color, 
"bgr8")
   117                     centerX = depth_camera_info.K[2]
   118                     centerY = depth_camera_info.K[5]
   119                     depthFocalLength = depth_camera_info.K[0]
   120                     depth_points = sensor_msgs.msg.PointCloud2()
   121                     depth_points.header = depth_image.header
   122                     depth_points.width = depth_image.width
   123                     depth_points.height  = depth_image.height
   124                     depth_points.fields.append(sensor_msgs.msg.PointField(
   125                         name = 
"x",offset = 0,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   126                     depth_points.fields.append(sensor_msgs.msg.PointField(
   127                         name = 
"y",offset = 4,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   128                     depth_points.fields.append(sensor_msgs.msg.PointField(
   129                         name = 
"z",offset = 8,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   130                     depth_points.point_step = 16 
   131                     depth_points.row_step = depth_points.point_step * depth_points.width
   134                     for v 
in range(depth_image.height):
   135                         for u 
in range(depth_image.width):
   136                             d = cv_depth_image[v,u]
   137                             ptx = (u - centerX) * d / depthFocalLength;
   138                             pty = (v - centerY) * d / depthFocalLength;
   140                             buffer.append(struct.pack(
'ffff',ptx,pty,ptz,1.0))
   141                     depth_points.data = 
"".join(buffer)
   142                     outbag.write(
"/camera/depth/points", depth_points, t)
   144                     centerX = depth_camera_info.K[2]
   145                     centerY = depth_camera_info.K[5]
   146                     depthFocalLength = depth_camera_info.K[0]
   147                     rgb_points = sensor_msgs.msg.PointCloud2()
   148                     rgb_points.header = rgb_image_color.header
   149                     rgb_points.width = depth_image.width
   150                     rgb_points.height  = depth_image.height
   151                     rgb_points.fields.append(sensor_msgs.msg.PointField(
   152                         name = 
"x",offset = 0,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   153                     rgb_points.fields.append(sensor_msgs.msg.PointField(
   154                         name = 
"y",offset = 4,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   155                     rgb_points.fields.append(sensor_msgs.msg.PointField(
   156                         name = 
"z",offset = 8,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   157                     rgb_points.fields.append(sensor_msgs.msg.PointField(
   158                         name = 
"rgb",offset = 16,datatype = sensor_msgs.msg.PointField.FLOAT32,count = 1 ))
   159                     rgb_points.point_step = 32 
   160                     rgb_points.row_step = rgb_points.point_step * rgb_points.width
   162                     for v 
in range(depth_image.height):
   163                         for u 
in range(depth_image.width):
   164                             d = cv_depth_image[v,u]
   165                             rgb = cv_rgb_image_color[v,u]
   166                             ptx = (u - centerX) * d / depthFocalLength;
   167                             pty = (v - centerY) * d / depthFocalLength;
   169                             buffer.append(struct.pack(
'ffffBBBBIII',
   171                                 rgb[0],rgb[1],rgb[2],0,
   173                     rgb_points.data = 
"".join(buffer)
   174                     outbag.write(
"/camera/depth_registered/points", rgb_points, t)
   177             rgb_image_color = 
None   179         if topic 
not in [
"/camera/depth_registered/camera_info",
"/camera/rgb/camera_info",
   180                          "/camera/rgb/image_color",
"/camera/depth_registered/image"]:
   182             outbag.write(topic,msg,t)