2 #include <sensor_msgs/Image.h>
3 #include <stereo_msgs/DisparityImage.h>
4 #include <ros/publisher.h>
5 #include <image_transport/image_transport.h>
7 #include <pcl/point_types.h>
8 #include <pcl_ros/point_cloud.h>
15 const double fx = 855.667983;
16 const double fy = 857.549007;
20 auto focalLength = msg.f;
22 std::vector<unsigned char> data = msg.image.data;
23 sensor_msgs::Image depthImage = msg.image;
24 depthImage.header.frame_id =
"/camera_left";
27 cloud->header.frame_id =
"/camera_left";
28 cloud->height = msg.image.height;
29 cloud->width = msg.image.width;
32 for(
int i=0;i<msg.image.step*msg.image.height;i+=4)
34 float* t = (
float*)&data[i];
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];
44 if(d<msg.min_disparity || d>msg.max_disparity)
47 int x = counter%depthImage.width;
48 int y = counter/depthImage.width;
50 p.x = (x-depthImage.width/2)*(Z/
fx);
51 p.y = (y-depthImage.height/2)*(Z/
fy);
53 cloud->points.push_back(p);
61 int main(
int argc,
char** argv)
63 ros::init(argc, argv,
"dispToPointcloud");
67 image_transport::ImageTransport _it(nh);
72 ros::Subscriber disp_sub = nh.subscribe(
"/disparity", 1,
dispCallback);
sensor_msgs::PointCloud2 cloud
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)