point_cloud_cropper.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2008, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of the Willow Garage nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 #
00034 # Revision $Id: add_two_ints_client 3357 2009-01-13 07:13:05Z jfaustwg $
00035 
00036 # Crop a point cloud to Z min/max, in a given frame.  This could be more
00037 # widely useful, but it turns out out to be really slow in Python.
00038 
00039 PKG = 'door_handle_detector' # this package name
00040 
00041 import roslib; roslib.load_manifest(PKG) 
00042 
00043 import sys
00044 from optparse import OptionParser
00045 import tf
00046 import bullet
00047 
00048 import rospy
00049 from sensor_msgs.msg import PointCloud, ChannelFloat32, Point32
00050 from geometry_msgs.msg import Point32
00051 
00052 NAME='point_cloud_cropper'
00053 
00054 class Cropper:
00055   def __init__(self, zmin, zmax, frame):
00056     self.zmin = zmin
00057     self.zmax = zmax
00058     self.frame = frame
00059     self.sub = rospy.Subscriber('full_cloud', PointCloud, self.cloud_cb)
00060     self.pub = rospy.Publisher('full_cloud_cropped', PointCloud)
00061     self.tf = tf.listener.TransformListener()
00062     rospy.init_node('cropper', anonymous=True)
00063 
00064   def cloud_cb(self, msg):
00065     print 'Received message with %d points'%(len(msg.points))
00066     newmsg = PointCloud(msg.header,[],[])
00067     for c in msg.chan:
00068       newmsg.channels.append(ChannelFloat32(c.name,[]))
00069     for i in range(0,len(msg.points)):
00070       if self.frame is None:
00071         p = msg.points[i]
00072       else:
00073         tfp = tf.PointStamped(bullet.Vector3(msg.points[i].x, msg.points[i].y, msg.points[i].z), 0, 0, msg.header.frame_id)
00074         tfpprime = self.tf.transform_point(self.frame, tfp)
00075         p = Point32(tfpprime.point.x(), tfpprime.point.y(), tfpprime.point.z())
00076       if p.z >= self.zmin and p.z <= self.zmax:
00077         newmsg.points.append(p)
00078         for j in range(0,len(msg.chan)):
00079           newmsg.channels[j].values.append(msg.channels[j].values[i])
00080     print 'Publishing message with %d points'%(len(newmsg.points))
00081     self.pub.publish(newmsg)
00082 
00083 if __name__ == "__main__":
00084 
00085   parser = OptionParser(usage="usage: %prog [options]", prog=NAME)
00086   parser.add_option("", "--zmin",
00087                     dest="zmin", default=None,
00088                     help="Minimum z value to allow through")
00089   parser.add_option("", "--zmax",
00090                     dest="zmax", default=None,
00091                     help="Maximum z value to allow through")
00092   parser.add_option("-f", "--frame",
00093                     dest="frame", default=None,
00094                     help="Frame to transform to before cropping")
00095 
00096   (options, args) = parser.parse_args(sys.argv)
00097   if options.zmin is None or options.zmax is None:
00098     parser.error("must specify both zmin and zmax")
00099     parser.print_help()
00100 
00101   c = Cropper(float(options.zmin), float(options.zmax), options.frame)
00102   rospy.spin()


door_handle_detector
Author(s): Radu Bogdan Rusu, Marius
autogenerated on Wed Dec 11 2013 14:17:01