34 #include <boost/version.hpp> 35 #if ((BOOST_VERSION / 100) % 1000) >= 53 36 #include <boost/thread/lock_guard.hpp> 49 #include <sensor_msgs/PointCloud2.h> 53 #include <opencv2/imgproc/imgproc.hpp> 82 virtual void onInit();
86 void imageCb(
const sensor_msgs::ImageConstPtr& depth_msg,
87 const sensor_msgs::ImageConstPtr& rgb_msg,
88 const sensor_msgs::CameraInfoConstPtr& info_msg);
91 void convert(
const sensor_msgs::ImageConstPtr& depth_msg,
92 const sensor_msgs::ImageConstPtr& rgb_msg,
93 const PointCloud::Ptr& cloud_msg,
94 int red_offset,
int green_offset,
int blue_offset,
int color_step);
108 private_nh.
param(
"queue_size", queue_size, 5);
110 private_nh.
param(
"exact_sync", use_exact_sync,
false);
127 boost::lock_guard<boost::mutex> lock(connect_mutex_);
134 boost::lock_guard<boost::mutex> lock(connect_mutex_);
135 if (pub_point_cloud_.getNumSubscribers() == 0)
137 sub_depth_.unsubscribe();
138 sub_rgb_ .unsubscribe();
139 sub_info_ .unsubscribe();
141 else if (!sub_depth_.getSubscriber())
145 std::string depth_image_transport_param =
"depth_image_transport";
149 sub_depth_.subscribe(*depth_it_,
"image_rect", 1, depth_hints);
153 sub_rgb_ .subscribe(*rgb_it_,
"image_rect_color", 1, hints);
154 sub_info_ .subscribe(*rgb_nh_,
"camera_info", 1);
159 const sensor_msgs::ImageConstPtr& rgb_msg_in,
160 const sensor_msgs::CameraInfoConstPtr& info_msg)
163 if (depth_msg->header.frame_id != rgb_msg_in->header.frame_id)
166 depth_msg->header.frame_id.c_str(), rgb_msg_in->header.frame_id.c_str());
171 model_.fromCameraInfo(info_msg);
174 sensor_msgs::ImageConstPtr rgb_msg = rgb_msg_in;
175 if (depth_msg->width != rgb_msg->width || depth_msg->height != rgb_msg->height)
177 sensor_msgs::CameraInfo info_msg_tmp = *info_msg;
178 info_msg_tmp.width = depth_msg->width;
179 info_msg_tmp.height = depth_msg->height;
180 float ratio = float(depth_msg->width)/float(rgb_msg->width);
181 info_msg_tmp.K[0] *= ratio;
182 info_msg_tmp.K[2] *= ratio;
183 info_msg_tmp.K[4] *= ratio;
184 info_msg_tmp.K[5] *= ratio;
185 info_msg_tmp.P[0] *= ratio;
186 info_msg_tmp.P[2] *= ratio;
187 info_msg_tmp.P[5] *= ratio;
188 info_msg_tmp.P[6] *= ratio;
189 model_.fromCameraInfo(info_msg_tmp);
198 ROS_ERROR(
"cv_bridge exception: %s", e.what());
202 cv_rsz.
header = cv_ptr->header;
204 cv::resize(cv_ptr->image.rowRange(0,depth_msg->height/ratio), cv_rsz.
image, cv::Size(depth_msg->width, depth_msg->height));
205 if ((rgb_msg->encoding == enc::RGB8) || (rgb_msg->encoding == enc::BGR8) || (rgb_msg->encoding == enc::MONO8))
214 rgb_msg = rgb_msg_in;
217 int red_offset, green_offset, blue_offset, color_step;
218 if (rgb_msg->encoding == enc::RGB8)
225 else if (rgb_msg->encoding == enc::BGR8)
232 else if (rgb_msg->encoding == enc::MONO8)
258 cloud_msg->header = depth_msg->header;
259 cloud_msg->height = depth_msg->height;
260 cloud_msg->width = depth_msg->width;
261 cloud_msg->is_dense =
false;
262 cloud_msg->is_bigendian =
false;
267 if (depth_msg->encoding == enc::TYPE_16UC1)
269 convert<uint16_t>(depth_msg, rgb_msg, cloud_msg, red_offset, green_offset, blue_offset, color_step);
271 else if (depth_msg->encoding == enc::TYPE_32FC1)
273 convert<float>(depth_msg, rgb_msg, cloud_msg, red_offset, green_offset, blue_offset, color_step);
281 pub_point_cloud_.publish (cloud_msg);
286 const sensor_msgs::ImageConstPtr& rgb_msg,
287 const PointCloud::Ptr& cloud_msg,
288 int red_offset,
int green_offset,
int blue_offset,
int color_step)
291 float center_x = model_.cx();
292 float center_y = model_.cy();
296 float constant_x = unit_scaling / model_.fx();
297 float constant_y = unit_scaling / model_.fy();
298 float bad_point = std::numeric_limits<float>::quiet_NaN ();
300 const T* depth_row =
reinterpret_cast<const T*
>(&depth_msg->data[0]);
301 int row_step = depth_msg->step /
sizeof(T);
302 const uint8_t* rgb = &rgb_msg->data[0];
303 int rgb_skip = rgb_msg->step - rgb_msg->width * color_step;
313 for (
int v = 0; v < int(cloud_msg->height); ++v, depth_row += row_step, rgb += rgb_skip)
315 for (
int u = 0; u < int(cloud_msg->width); ++u, rgb += color_step, ++iter_x, ++iter_y, ++iter_z, ++iter_a, ++iter_r, ++iter_g, ++iter_b)
317 T depth = depth_row[u];
322 *iter_x = *iter_y = *iter_z = bad_point;
327 *iter_x = (u - center_x) * depth * constant_x;
328 *iter_y = (v - center_y) * depth * constant_y;
334 *iter_r = rgb[red_offset];
335 *iter_g = rgb[green_offset];
336 *iter_b = rgb[blue_offset];
CvImageConstPtr toCvShare(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
boost::function< void(const SingleSubscriberPublisher &)> SubscriberStatusCallback
#define NODELET_ERROR_THROTTLE(rate,...)
image_transport::SubscriberFilter sub_rgb_
ExactTime< sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo > SyncPolicy
ros::Publisher pub_point_cloud_
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
sensor_msgs::PointCloud2 PointCloud
boost::shared_ptr< Synchronizer > sync_
ApproximateTime< sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo > SyncPolicy
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
boost::shared_ptr< image_transport::ImageTransport > rgb_it_
image_geometry::PinholeCameraModel model_
boost::mutex connect_mutex_
void convert(const sensor_msgs::ImageConstPtr &depth_msg, PointCloud::Ptr &cloud_msg, const image_geometry::PinholeCameraModel &model, double range_max=0.0)
message_filters::Synchronizer< SyncPolicy > Synchronizer
boost::shared_ptr< ExactSynchronizer > exact_sync_
message_filters::Subscriber< sensor_msgs::CameraInfo > sub_info_
PLUGINLIB_EXPORT_CLASS(xiaoqiang_depth_image_proc::PointCloudXyzrgbNodelet, nodelet::Nodelet)
void imageCb(const sensor_msgs::ImageConstPtr &depth_msg, const sensor_msgs::ImageConstPtr &rgb_msg, const sensor_msgs::CameraInfoConstPtr &info_msg)
void convert(const sensor_msgs::ImageConstPtr &depth_msg, const sensor_msgs::ImageConstPtr &rgb_msg, const PointCloud::Ptr &cloud_msg, int red_offset, int green_offset, int blue_offset, int color_step)
void setPointCloud2FieldsByString(int n_fields,...)
ros::NodeHandlePtr rgb_nh_
ExactTime< sensor_msgs::Image, sensor_msgs::Image, sensor_msgs::CameraInfo > ExactSyncPolicy
sensor_msgs::ImagePtr toImageMsg() const
message_filters::Synchronizer< ExactSyncPolicy > ExactSynchronizer