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
00007
00008
00009
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
00015 this->pointcloud_in_subscriber_ = this->public_node_handle_.subscribe("pointcloud_in", 1, &PlaneSegmentationPclRgbAlgNode::pointcloud_in_callback, this);
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 }
00027
00028 PlaneSegmentationPclRgbAlgNode::~PlaneSegmentationPclRgbAlgNode(void)
00029 {
00030
00031 }
00032
00033 void PlaneSegmentationPclRgbAlgNode::mainNodeThread(void)
00034 {
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 }
00049
00050
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
00065 cloud_rgb_segmented = this->alg_.segmentBiggestPlane(cloud_rgb.makeShared(), cloud.makeShared(), coefficients);
00066 boardImage = pointcloud_to_image(cloud_rgb_segmented);
00067
00068
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
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
00086
00087
00088
00089
00090
00091
00092
00093
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
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
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
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);
00135 for (int i = 0; i < cvimage.rows; i++) {
00136
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
00146 if (pixel.x == pixel.x) {
00147
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;
00164 return out_msg.toImageMsg();
00165 }