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
00057 #include <ros/ros.h>
00058 #include <nodelet/nodelet.h>
00059
00060
00061 #include <sensor_msgs/Image.h>
00062 #include <sensor_msgs/PointCloud2.h>
00063 #include <tf/transform_listener.h>
00064
00065
00066 #include <message_filters/subscriber.h>
00067 #include <message_filters/synchronizer.h>
00068 #include <message_filters/sync_policies/approximate_time.h>
00069 #include <image_transport/image_transport.h>
00070 #include <image_transport/subscriber_filter.h>
00071
00072
00073 #include <opencv/cv.h>
00074 #include <opencv/highgui.h>
00075 #include <cv_bridge/cv_bridge.h>
00076 #include <sensor_msgs/image_encodings.h>
00077
00078
00079 #include <pcl/point_types.h>
00080 #include <pcl_ros/point_cloud.h>
00081
00082
00083 #include <pluginlib/class_list_macros.h>
00084
00085
00086 #include <iostream>
00087
00088 #include <cob_vision_utils/GlobalDefines.h>
00089
00090
00091 namespace ipa_CameraSensors
00092 {
00093 class CobKinectImageFlipNodelet : public nodelet::Nodelet
00094 {
00095 protected:
00096 message_filters::Subscriber<sensor_msgs::PointCloud2> point_cloud_sub_;
00097 ros::Publisher point_cloud_pub_;
00098 image_transport::ImageTransport* it_;
00099 image_transport::SubscriberFilter color_camera_image_sub_;
00100 image_transport::Publisher color_camera_image_pub_;
00101 message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::Image> >* sync_pointcloud_;
00102 message_filters::Connection sync_pointcloud_callback_connection_;
00103
00104 tf::TransformListener* transform_listener_;
00105
00106 ros::NodeHandle node_handle_;
00107
00108 public:
00109
00110 CobKinectImageFlipNodelet()
00111 {
00112 it_ = 0;
00113 sync_pointcloud_ = 0;
00114 transform_listener_ = 0;
00115 }
00116
00117 ~CobKinectImageFlipNodelet()
00118 {
00119 if (it_ != 0) delete it_;
00120 if (sync_pointcloud_ != 0) delete sync_pointcloud_;
00121 if (transform_listener_ != 0) delete transform_listener_;
00122 }
00123
00124 void onInit()
00125 {
00126 node_handle_ = getNodeHandle();
00127 it_ = new image_transport::ImageTransport(node_handle_);
00128 sync_pointcloud_ = new message_filters::Synchronizer<message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::Image> >(1);
00129 color_camera_image_pub_ = it_->advertise("rgb/upright/image_color", 1);
00130 point_cloud_pub_ = node_handle_.advertise<sensor_msgs::PointCloud2>("depth/upright/points", 1);
00131
00132 transform_listener_ = new tf::TransformListener(node_handle_);
00133
00134
00135 init();
00136
00137 std::cout << "CobKinectImageFlipNodelet initilized.\n";
00138 }
00139
00140 unsigned long init()
00141 {
00142 color_camera_image_sub_.subscribe(*it_, "colorimage", 1);
00143 point_cloud_sub_.subscribe(node_handle_, "pointcloud", 1);
00144
00145 sync_pointcloud_->connectInput(point_cloud_sub_, color_camera_image_sub_);
00146 sync_pointcloud_callback_connection_ = sync_pointcloud_->registerCallback(boost::bind(&CobKinectImageFlipNodelet::inputCallback, this, _1, _2));
00147
00148 return ipa_Utils::RET_OK;
00149 }
00150
00151 unsigned long convertColorImageMessageToMat(const sensor_msgs::Image::ConstPtr& color_image_msg, cv_bridge::CvImageConstPtr& color_image_ptr, cv::Mat& color_image)
00152 {
00153 try
00154 {
00155 color_image_ptr = cv_bridge::toCvShare(color_image_msg, sensor_msgs::image_encodings::BGR8);
00156 }
00157 catch (cv_bridge::Exception& e)
00158 {
00159 ROS_ERROR("PeopleDetection: cv_bridge exception: %s", e.what());
00160 return ipa_Utils::RET_FAILED;
00161 }
00162 color_image = color_image_ptr->image;
00163
00164 return ipa_Utils::RET_OK;
00165 }
00166
00167 void inputCallback(const sensor_msgs::PointCloud2::ConstPtr& point_cloud_msg, const sensor_msgs::Image::ConstPtr& color_image_msg)
00168 {
00169
00170 bool turnAround = false;
00171 tf::StampedTransform transform;
00172 try
00173 {
00174 transform_listener_->lookupTransform("/base_link", "/head_cam3d_link", ros::Time(0), transform);
00175 btScalar roll, pitch, yaw;
00176 transform.getBasis().getRPY(roll, pitch, yaw, 1);
00177 if (roll > 0.0) turnAround = true;
00178
00179
00180
00181 }
00182 catch (tf::TransformException ex)
00183 {
00184 ROS_WARN("%s",ex.what());
00185 }
00186
00187 if (turnAround==false)
00188 {
00189
00190 sensor_msgs::Image color_image_turned_msg = *color_image_msg;
00191 color_image_turned_msg.header.stamp = ros::Time::now();
00192 color_camera_image_pub_.publish(color_image_turned_msg);
00193 sensor_msgs::PointCloud2 point_cloud_turned_msg = *point_cloud_msg;
00194 point_cloud_turned_msg.header.stamp = ros::Time::now();
00195 point_cloud_pub_.publish(point_cloud_turned_msg);
00196 }
00197 else
00198 {
00199
00200
00201
00202 cv_bridge::CvImageConstPtr color_image_ptr;
00203 cv::Mat color_image;
00204 convertColorImageMessageToMat(color_image_msg, color_image_ptr, color_image);
00205
00206
00207 pcl::PointCloud<pcl::PointXYZ> point_cloud_src;
00208 pcl::fromROSMsg(*point_cloud_msg, point_cloud_src);
00209
00210
00211
00212 cv::Mat color_image_turned(color_image.rows, color_image.cols, color_image.type());
00213 if (color_image.type() != CV_8UC3)
00214 {
00215 std::cout << "CobKinectImageFlipNodelet::inputCallback: Error: The image format of the color image is not CV_8UC3.\n";
00216 return;
00217 }
00218 for (int v=0; v<color_image.rows; v++)
00219 {
00220 uchar* src = color_image.ptr(v);
00221 uchar* dst = color_image_turned.ptr(color_image.rows-v-1);
00222 dst += 3*(color_image.cols-1);
00223 for (int u=0; u<color_image.cols; u++)
00224 {
00225 for (int k=0; k<3; k++)
00226 {
00227 *dst = *src;
00228 src++; dst++;
00229 }
00230 dst -= 6;
00231 }
00232 }
00233
00234
00235 pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_turned(new pcl::PointCloud<pcl::PointXYZ>);
00236 for (int v=(int)point_cloud_src.height-1; v>=0; v--)
00237 {
00238 for (int u=(int)point_cloud_src.width-1; u>=0; u--)
00239 {
00240 point_cloud_turned->push_back(point_cloud_src(u,v));
00241 }
00242 }
00243
00244
00245
00246 cv_bridge::CvImage cv_ptr;
00247 cv_ptr.image = color_image_turned;
00248 cv_ptr.encoding = "bgr8";
00249 sensor_msgs::Image::Ptr color_image_turned_msg = cv_ptr.toImageMsg();
00250 color_image_turned_msg->header.stamp = ros::Time::now();
00251 color_camera_image_pub_.publish(color_image_turned_msg);
00252
00253
00254
00255 point_cloud_turned->header = point_cloud_msg->header;
00256 point_cloud_turned->height = point_cloud_msg->height;
00257 point_cloud_turned->width = point_cloud_msg->width;
00258
00259
00260 point_cloud_turned->is_dense = point_cloud_msg->is_dense;
00261 sensor_msgs::PointCloud2::Ptr point_cloud_turned_msg(new sensor_msgs::PointCloud2);
00262 pcl::toROSMsg(*point_cloud_turned, *point_cloud_turned_msg);
00263 point_cloud_turned_msg->header.stamp = ros::Time::now();
00264 point_cloud_pub_.publish(point_cloud_turned_msg);
00265
00266
00267
00268
00269 }
00270 }
00271 };
00272
00273 }
00274
00275 PLUGINLIB_DECLARE_CLASS(ipa_CameraSensors, CobKinectImageFlipNodelet, ipa_CameraSensors::CobKinectImageFlipNodelet, nodelet::Nodelet)