bumblebee2/dispToPointcloud/main.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <sensor_msgs/Image.h>
3 #include <stereo_msgs/DisparityImage.h>
4 #include <ros/publisher.h>
5 #include <image_transport/image_transport.h>
6 #include <cmath>
7 #include <pcl/point_types.h>
8 #include <pcl_ros/point_cloud.h>
9 
10 
11 image_transport::Publisher _depth_pub;
12 ros::Publisher _pointcloud_pub;
13 typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
14 
15 const double fx = 855.667983;
16 const double fy = 857.549007;
17 
18 void dispCallback(const stereo_msgs::DisparityImage& msg)
19 {
20  auto focalLength = msg.f;
21  auto basline = 0.102;
22  std::vector<unsigned char> data = msg.image.data;
23  sensor_msgs::Image depthImage = msg.image;
24  depthImage.header.frame_id = "/camera_left";
25 
26  PointCloud::Ptr cloud (new PointCloud);
27  cloud->header.frame_id = "/camera_left";
28  cloud->height = msg.image.height;
29  cloud->width = msg.image.width;
30 
31  int counter = 0;
32  for(int i=0;i<msg.image.step*msg.image.height;i+=4)
33  {
34  float* t = (float*)&data[i];
35  float d = *t;
36  float Z = focalLength*basline/abs(d);
37  unsigned char c[sizeof Z];
38  memcpy(c, &Z, sizeof Z);
39  depthImage.data[i] = c[0];
40  depthImage.data[i+1] = c[1];
41  depthImage.data[i+2] = c[2];
42  depthImage.data[i+3] = c[3];
43 
44  if(d<msg.min_disparity || d>msg.max_disparity)
45  Z=0;
46 
47  int x = counter%depthImage.width;
48  int y = counter/depthImage.width;
49  pcl::PointXYZ p;
50  p.x = (x-depthImage.width/2)*(Z/fx);
51  p.y = (y-depthImage.height/2)*(Z/fy);
52  p.z = Z;
53  cloud->points.push_back(p);
54  counter++;
55  }
56 
57  _depth_pub.publish(depthImage);
58  _pointcloud_pub.publish(cloud);
59 }
60 
61 int main(int argc, char** argv)
62 {
63  ros::init(argc, argv, "dispToPointcloud");
64 
65  ros::NodeHandle nh;
66 
67  image_transport::ImageTransport _it(nh);
68 
69  _depth_pub = _it.advertise("/depthImage", 1);
70  _pointcloud_pub = nh.advertise<PointCloud>("/pointcloud/camera", 1);
71 
72  ros::Subscriber disp_sub = nh.subscribe("/disparity", 1, dispCallback);
73 
74  ros::spin();
75 }
76 
77 
sensor_msgs::PointCloud2 cloud
Definition: map/main.cpp:12
ros::Publisher _pointcloud_pub
pcl::PointCloud< pcl::PointXYZ > PointCloud
void dispCallback(const stereo_msgs::DisparityImage &msg)
image_transport::Publisher _depth_pub
int main(int argc, char **argv)


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45