plane_segmentation_pcl_rgb_alg_node.cpp
Go to the documentation of this file.
00001 #include "plane_segmentation_pcl_rgb_alg_node.h"
00002 
00003 PlaneSegmentationPclRgbAlgNode::PlaneSegmentationPclRgbAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<PlaneSegmentationPclRgbAlgorithm>()
00005 {
00006         //init class attributes if necessary
00007         //this->loop_rate_ = 2;//in [Hz]
00008 
00009         // [init publishers]
00010         this->segmented_image_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("segmented_image", 1);
00011         this->plane_coefficients_publisher_ = this->public_node_handle_.advertise<pcl_msgs::ModelCoefficients>("plane_coefficients", 1);
00012         this->pointcloud_out_publisher_ = this->public_node_handle_.advertise<sensor_msgs::PointCloud2>("pointcloud_out", 1);
00013         
00014         // [init subscribers]
00015         this->pointcloud_in_subscriber_ = this->public_node_handle_.subscribe("pointcloud_in", 1, &PlaneSegmentationPclRgbAlgNode::pointcloud_in_callback, this);
00016         
00017         // [init services]
00018   //this->segment_server_ = this->public_node_handle_.advertiseService("segment", &PlaneSegmentationPclRgbAlgNode::segmentCallback, this); UNUSED
00019         
00020         // [init clients]
00021         
00022         // [init action servers]
00023         
00024         // [init action clients]
00025         
00026 }
00027 
00028 PlaneSegmentationPclRgbAlgNode::~PlaneSegmentationPclRgbAlgNode(void)
00029 {
00030         // [free dynamic memory]
00031 }
00032 
00033 void PlaneSegmentationPclRgbAlgNode::mainNodeThread(void)
00034 {
00035         // [fill msg structures]
00036         //this->Image_msg_.data = my_var;
00037         //this->ModelCoefficients_msg_.data = my_var;
00038         //this->PointCloud2_msg_.data = my_var;
00039         
00040         // [fill srv structure and make request to the server]
00041         
00042         // [fill action structure and make request to the action server]
00043 
00044         // [publish messages]
00045         //this->segmented_image_publisher_.publish(this->Image_msg_);
00046         //this->plane_coefficients_publisher_.publish(this->ModelCoefficients_msg_);
00047         //this->pointcloud_out_publisher_.publish(this->PointCloud2_msg_);
00048 }
00049 
00050 /*  [subscriber callbacks] */
00051 void PlaneSegmentationPclRgbAlgNode::pointcloud_in_callback(const sensor_msgs::PointCloud2::ConstPtr& msg) 
00052 { 
00053         
00054         pcl::PointCloud<pcl::PointXYZRGB> cloud_rgb;
00055         pcl::PointCloud<pcl::PointXYZ> cloud;
00056         pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_rgb_segmented;
00057         pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);
00058         cv::Mat boardImage;
00059         
00060         pcl::fromROSMsg (*msg, cloud_rgb);
00061         pcl::fromROSMsg (*msg, cloud);
00062         
00063         ROS_DEBUG_STREAM("Segmenting plane");
00064         // Get segmented pointcloud
00065         cloud_rgb_segmented = this->alg_.segmentBiggestPlane(cloud_rgb.makeShared(), cloud.makeShared(), coefficients);
00066         boardImage = pointcloud_to_image(cloud_rgb_segmented);
00067         
00068         // PCL and image to ROS msg
00069         pcl::toROSMsg(*cloud_rgb_segmented, this->PointCloud2_msg_);
00070         std_msgs::Header header = this->PointCloud2_msg_.header;
00071         this->Image_msg_ = *cvimage_to_rosimage(boardImage, header);
00072 
00073         pcl_conversions::fromPCL(*coefficients, this->ModelCoefficients_msg_);
00074         
00075         
00076         ROS_DEBUG_STREAM("Publishing segmented plane");
00077         // Publish
00078         
00079         
00080         if (this->pointcloud_out_publisher_.getNumSubscribers())
00081                 this->pointcloud_out_publisher_.publish(this->PointCloud2_msg_);
00082         this->segmented_image_publisher_.publish(this->Image_msg_);
00083         this->plane_coefficients_publisher_.publish(this->ModelCoefficients_msg_);
00084 
00085         //use appropiate mutex to shared variables if necessary 
00086         //this->alg_.lock(); 
00087         //this->pointcloud_in_mutex_.enter(); 
00088 
00089         //std::cout << msg->data << std::endl; 
00090 
00091         //unlock previously blocked shared variables 
00092         //this->alg_.unlock(); 
00093         //this->pointcloud_in_mutex_.exit(); 
00094 }
00095 
00096 /*  [service callbacks] */
00097 /* To recover replace "$$n" for "\n".
00098 bool PlaneSegmentationPclRgbAlgNode::segmentCallback(iri_plane_segmentation_pcl_rgb::segment::Request &req, iri_plane_segmentation_pcl_rgb::segment::Response &res) $$n{ $$n    ROS_INFO("PlaneSegmentationPclRgbAlgNode::segmentCallback: New Request Received!"); $$n$$n      // Define data to be used.$$n   pcl::PointCloud<pcl::PointXYZRGB> cloud_rgb;$$n pcl::PointCloud<pcl::PointXYZ> cloud;$$n        pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_rgb_segmented;$$n  pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients);$$n$$n    // From ROS msg to pcl.$$n      pcl::fromROSMsg(req.inputPCL, cloud_rgb);$$n    pcl::fromROSMsg(req.inputPCL, cloud);$$n$$n     ROS_INFO("PlaneSegmentationPclRgbAlgNode::segmentCallback: Processing New Request!"); $$n$$n    // Call algorithm function: segmentBiggestPlane.$$n     cloud_rgb_segmented = this->alg_.segmentBiggestPlane(cloud_rgb.makeShared(), cloud.makeShared(), coefficients);$$n      cv::Mat segmented_img = pointcloud_to_image(cloud_rgb_segmented);$$n$$n // From image to ROS msg.$$n    res.segmentedImg = *cvimage_to_rosimage(segmented_img);$$n$$n   return true; $$n}
00099 */
00100 
00101 /*  [action callbacks] */
00102 
00103 /*  [action requests] */
00104 
00105 void PlaneSegmentationPclRgbAlgNode::node_config_update(Config &config, uint32_t level)
00106 {
00107         this->alg_.lock();
00108 
00109         this->alg_.unlock();
00110 }
00111 
00112 void PlaneSegmentationPclRgbAlgNode::addNodeDiagnostics(void)
00113 {
00114 }
00115 
00116 /* main function */
00117 int main(int argc,char *argv[])
00118 {
00119   return algorithm_base::main<PlaneSegmentationPclRgbAlgNode>(argc, argv, "plane_segmentation_pcl_rgb_alg_node");
00120 }
00121 
00122 
00123 /* Other functions */
00124 cv::Mat PlaneSegmentationPclRgbAlgNode::pointcloud_to_image(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud)
00125 {
00126         pcl::PointXYZRGB pixel;
00127         cv::Mat cvimage;
00128         uint32_t rgb;
00129         uint8_t r;
00130         uint8_t g;
00131         uint8_t b;
00132 
00133         
00134         cvimage = cv::Mat::zeros(cloud->height, cloud->width, CV_8UC3);//a black image 
00135         for (int i = 0; i < cvimage.rows; i++) {
00136                 //double* Mi = cvimage.ptr<double>(i);
00137                 
00138                 for (int j = 0; j< cvimage.cols; j++) {
00139                         pixel = cloud->at(j,i);
00140                         
00141                         rgb = *reinterpret_cast<int*>(&pixel.rgb);
00142                         r = (rgb >> 16) & 0x0000ff;
00143                         g = (rgb >> 8)  & 0x0000ff;
00144                         b = (rgb)       & 0x0000ff;
00145                         //ROS_INFO_STREAM("Color: " << rgb );
00146                         if (pixel.x == pixel.x) { // if x != NaN
00147                                 //Mi[j] = rgb;
00148                                 cvimage.at<cv::Vec3b>(i,j)[0] = b;
00149                                 cvimage.at<cv::Vec3b>(i,j)[1] = g;
00150                                 cvimage.at<cv::Vec3b>(i,j)[2] = r;
00151                         }
00152                 }
00153         }
00154         
00155         return cvimage;
00156 }
00157 
00158 sensor_msgs::ImagePtr PlaneSegmentationPclRgbAlgNode::cvimage_to_rosimage(cv::Mat cvimage, std_msgs::Header header)
00159 {
00160         cv_bridge::CvImage out_msg;
00161         out_msg.encoding = sensor_msgs::image_encodings::BGR8;
00162         out_msg.image    = cvimage;
00163         out_msg.header.stamp = header.stamp; // This fails.
00164         return out_msg.toImageMsg();
00165 }


iri_plane_segmentation_pcl_rgb
Author(s): David Martinez
autogenerated on Fri Dec 6 2013 21:27:02