00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <cob_image_flip/kinect_image_flip.h>
00057
00058 namespace cob_image_flip
00059 {
00060 CobKinectImageFlip::CobKinectImageFlip(ros::NodeHandle nh)
00061 {
00062 node_handle_ = nh;
00063
00064
00065 std::string robot;
00066 flip_color_image_ = false;
00067 flip_pointcloud_ = false;
00068 pointcloud_data_format_ = "xyz";
00069
00070 img_sub_counter_ = 0;
00071 pc_sub_counter_ = 0;
00072
00073 std::cout << "\n--------------------------\nKinect Image Flip Parameters:\n--------------------------" << std::endl;
00074 node_handle_.param("flip_color_image", flip_color_image_, false);
00075 std::cout << "flip_color_image = " << flip_color_image_ << std::endl;
00076 node_handle_.param("flip_pointcloud", flip_pointcloud_, false);
00077 std::cout << "flip_pointcloud = " << flip_pointcloud_ << std::endl;
00078 node_handle_.param<std::string>("pointcloud_data_format", pointcloud_data_format_, "xyz");
00079 std::cout << "pointcloud_data_format = " << pointcloud_data_format_ << std::endl;
00080 node_handle_.param("display_warnings", display_warnings_, false);
00081 std::cout << "display_warnings = " << display_warnings_ << std::endl;
00082 node_handle_.param("display_timing", display_timing_, false);
00083 std::cout << "display_timing = " << display_timing_ << std::endl;
00084 node_handle_.param<std::string>("robot", robot, "cob3-3");
00085 std::cout << "robot = " << robot << std::endl;
00086
00087
00088 cob3Number_ = 0;
00089
00090 std::stringstream ss;
00091 ss << robot.substr(robot.find("cob3-")+5);
00092 ss >> cob3Number_;
00093 std::cout << "CobKinectImageFlip: Robot number is cob3-" << cob3Number_ << "." << std::endl;
00094
00095
00096
00097 transform_listener_ = 0;
00098
00099
00100
00101 if (flip_color_image_ == true)
00102 {
00103 it_ = new image_transport::ImageTransport(node_handle_);
00104
00105
00106
00107
00108
00109
00110 color_camera_image_sub_.registerCallback(boost::bind(&CobKinectImageFlip::imageCallback, this, _1));
00111
00112
00113
00114 color_camera_image_pub_ = it_->advertise("colorimage_out", 1, boost::bind(&CobKinectImageFlip::imgConnectCB, this, _1), boost::bind(&CobKinectImageFlip::imgDisconnectCB, this, _1));
00115 }
00116 else
00117 {
00118 it_ = 0;
00119 }
00120
00121
00122
00123
00124 if (flip_pointcloud_ == true)
00125 {
00126 point_cloud_pub_ = node_handle_.advertise<sensor_msgs::PointCloud2>("pointcloud_out", 1, boost::bind(&CobKinectImageFlip::pcConnectCB, this, _1), boost::bind(&CobKinectImageFlip::pcDisconnectCB, this, _1));
00127 }
00128
00129
00130
00131 transform_listener_ = new tf::TransformListener(node_handle_);
00132
00133 std::cout << "CobKinectImageFlip initilized." << std::endl;
00134 }
00135
00136 CobKinectImageFlip::~CobKinectImageFlip()
00137 {
00138 if (it_ != 0)
00139 delete it_;
00140
00141
00142 if (transform_listener_ != 0)
00143 delete transform_listener_;
00144 }
00145
00146 unsigned long CobKinectImageFlip::init()
00147 {
00148
00149
00150
00151
00152
00153
00154 return ipa_Utils::RET_OK;
00155 }
00156
00157 unsigned long CobKinectImageFlip::convertColorImageMessageToMat(const sensor_msgs::Image::ConstPtr& color_image_msg, cv_bridge::CvImageConstPtr& color_image_ptr, cv::Mat& color_image)
00158 {
00159 try
00160 {
00161 color_image_ptr = cv_bridge::toCvShare(color_image_msg, sensor_msgs::image_encodings::BGR8);
00162 } catch (cv_bridge::Exception& e)
00163 {
00164 ROS_ERROR("PeopleDetection: cv_bridge exception: %s", e.what());
00165 return ipa_Utils::RET_FAILED;
00166 }
00167 color_image = color_image_ptr->image;
00168
00169 return ipa_Utils::RET_OK;
00170 }
00171
00172 template <typename T>
00173 void CobKinectImageFlip::inputCallback(const sensor_msgs::PointCloud2::ConstPtr& point_cloud_msg)
00174 {
00175
00176
00177
00178
00179 bool turnAround = false;
00180 tf::StampedTransform transform;
00181 try
00182 {
00183 transform_listener_->lookupTransform("/base_link", "/head_cam3d_link", ros::Time(0), transform);
00184 btScalar roll, pitch, yaw;
00185 transform.getBasis().getRPY(roll, pitch, yaw, 1);
00186 if (cob3Number_ == 2)
00187 roll *= -1.;
00188 if (roll > 0.0)
00189 turnAround = true;
00190
00191
00192
00193 } catch (tf::TransformException ex)
00194 {
00195 if (display_warnings_ == true)
00196 ROS_WARN("%s",ex.what());
00197 }
00198
00199 if (turnAround == false)
00200 {
00201
00202
00203
00204
00205
00206 point_cloud_pub_.publish(point_cloud_msg);
00207 }
00208 else
00209 {
00210
00211
00212 pcl::PointCloud<T> point_cloud_src;
00213 pcl::fromROSMsg(*point_cloud_msg, point_cloud_src);
00214
00215 boost::shared_ptr<pcl::PointCloud<T> > point_cloud_turned(new pcl::PointCloud<T>);
00216 point_cloud_turned->header = point_cloud_msg->header;
00217 point_cloud_turned->height = point_cloud_msg->height;
00218 point_cloud_turned->width = point_cloud_msg->width;
00219
00220
00221 point_cloud_turned->is_dense = true;
00222 point_cloud_turned->resize(point_cloud_src.height*point_cloud_src.width);
00223 for (int v = (int)point_cloud_src.height - 1; v >= 0; v--)
00224 {
00225 for (int u = (int)point_cloud_src.width - 1; u >= 0; u--)
00226 {
00227 (*point_cloud_turned)(point_cloud_src.width-1 - u, point_cloud_src.height-1 - v) = point_cloud_src(u, v);
00228 }
00229 }
00230
00231
00232 sensor_msgs::PointCloud2::Ptr point_cloud_turned_msg(new sensor_msgs::PointCloud2);
00233 pcl::toROSMsg(*point_cloud_turned, *point_cloud_turned_msg);
00234
00235 point_cloud_pub_.publish(point_cloud_turned_msg);
00236
00237
00238
00239
00240 }
00241
00242 if (display_timing_ == true)
00243 ROS_INFO("%d ImageFlip: Time stamp of pointcloud message: %f. Delay: %f.", point_cloud_msg->header.seq, point_cloud_msg->header.stamp.toSec(), ros::Time::now().toSec()-point_cloud_msg->header.stamp.toSec());
00244
00245 }
00246
00247 void CobKinectImageFlip::imageCallback(const sensor_msgs::ImageConstPtr& color_image_msg)
00248 {
00249
00250
00251
00252
00253 bool turnAround = false;
00254 tf::StampedTransform transform;
00255 try
00256 {
00257 transform_listener_->lookupTransform("/base_link", "/head_cam3d_link", ros::Time(0), transform);
00258 btScalar roll, pitch, yaw;
00259 transform.getBasis().getRPY(roll, pitch, yaw, 1);
00260 if (cob3Number_ == 2)
00261 roll *= -1.;
00262 if (roll > 0.0)
00263 turnAround = true;
00264
00265
00266
00267 } catch (tf::TransformException ex)
00268 {
00269 if (display_warnings_ == true)
00270 ROS_WARN("%s",ex.what());
00271 }
00272
00273
00274 if (turnAround == false)
00275 {
00276
00277 sensor_msgs::Image color_image_turned_msg = *color_image_msg;
00278 color_camera_image_pub_.publish(color_image_turned_msg);
00279 }
00280 else
00281 {
00282
00283 cv_bridge::CvImageConstPtr color_image_ptr;
00284 cv::Mat color_image;
00285 convertColorImageMessageToMat(color_image_msg, color_image_ptr, color_image);
00286
00287
00288 cv::Mat color_image_turned(color_image.rows, color_image.cols, color_image.type());
00289 if (color_image.type() != CV_8UC3)
00290 {
00291 std::cout << "CobKinectImageFlipNodelet::inputCallback: Error: The image format of the color image is not CV_8UC3.\n";
00292 return;
00293 }
00294 for (int v = 0; v < color_image.rows; v++)
00295 {
00296 uchar* src = color_image.ptr(v);
00297 uchar* dst = color_image_turned.ptr(color_image.rows - v - 1);
00298 dst += 3 * (color_image.cols - 1);
00299 for (int u = 0; u < color_image.cols; u++)
00300 {
00301 for (int k = 0; k < 3; k++)
00302 {
00303 *dst = *src;
00304 src++;
00305 dst++;
00306 }
00307 dst -= 6;
00308 }
00309 }
00310 cv_bridge::CvImage cv_ptr;
00311 cv_ptr.image = color_image_turned;
00312 cv_ptr.encoding = "bgr8";
00313 sensor_msgs::Image::Ptr color_image_turned_msg = cv_ptr.toImageMsg();
00314 color_image_turned_msg->header = color_image_msg->header;
00315 color_camera_image_pub_.publish(color_image_turned_msg);
00316 }
00317
00318
00319 }
00320
00321 void CobKinectImageFlip::imgConnectCB(const image_transport::SingleSubscriberPublisher& pub)
00322 {
00323 img_sub_counter_++;
00324 if(img_sub_counter_ == 1)
00325 {
00326 ROS_DEBUG("connecting");
00327 color_camera_image_sub_.subscribe(*it_, "colorimage_in", 1);
00328 }
00329 }
00330
00331 void CobKinectImageFlip::imgDisconnectCB(const image_transport::SingleSubscriberPublisher& pub)
00332 {
00333 img_sub_counter_--;
00334 if(img_sub_counter_ == 0)
00335 {
00336 ROS_DEBUG("disconnecting");
00337 color_camera_image_sub_.unsubscribe();
00338 }
00339 }
00340
00341 void CobKinectImageFlip::pcConnectCB(const ros::SingleSubscriberPublisher& pub)
00342 {
00343 pc_sub_counter_++;
00344 if(pc_sub_counter_ == 1)
00345 {
00346 ROS_DEBUG("connecting");
00347 if (pointcloud_data_format_.compare("xyz") == 0)
00348 point_cloud_sub_ = node_handle_.subscribe<sensor_msgs::PointCloud2>("pointcloud_in", 1, &CobKinectImageFlip::inputCallback<pcl::PointXYZ>, this);
00349 else if (pointcloud_data_format_.compare("xyzrgb") == 0)
00350 point_cloud_sub_ = node_handle_.subscribe<sensor_msgs::PointCloud2>("pointcloud_in", 1, &CobKinectImageFlip::inputCallback<pcl::PointXYZRGB>, this);
00351 else
00352 {
00353 ROS_ERROR("Unknown pointcloud format specified in the paramter file.");
00354 pc_sub_counter_ = 0;
00355 }
00356 }
00357 }
00358
00359 void CobKinectImageFlip::pcDisconnectCB(const ros::SingleSubscriberPublisher& pub)
00360 {
00361 pc_sub_counter_--;
00362 if(pc_sub_counter_ == 0)
00363 {
00364 ROS_DEBUG("disconnecting");
00365 point_cloud_sub_.shutdown();
00366 }
00367 }
00368
00369 }