Go to the documentation of this file.00001 #include "leaf_segmentation_alg_node.h"
00002 #include "segment-image.h"
00003 #include "pnmfile.h"
00004
00005 LeafSegmentationAlgNode::LeafSegmentationAlgNode(void) :
00006 algorithm_base::IriBaseAlgorithm<LeafSegmentationAlgorithm>()
00007 {
00008
00009
00010
00011
00012 output_image_publisher_ = public_node_handle_.advertise<sensor_msgs::Image>("output_image", 1);
00013
00014
00015
00016
00017 get_clustered_image_srv_ = public_node_handle_.advertiseService("get_clustered_image_srv", &LeafSegmentationAlgNode::get_clustered_image_srv_callback, this);
00018
00019
00020
00021
00022
00023
00024 }
00025
00026 LeafSegmentationAlgNode::~LeafSegmentationAlgNode(void)
00027 {
00028
00029 }
00030
00031 void LeafSegmentationAlgNode::mainNodeThread(void)
00032 {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 }
00045
00046
00047
00048
00049 bool LeafSegmentationAlgNode::get_clustered_image_srv_callback(iri_leaf_segmentation::GetClusteredImage::Request &req, iri_leaf_segmentation::GetClusteredImage::Response &res)
00050 {
00051 ROS_INFO("LeafSegmentationAlgNode::get_clustered_image_srv_callback: New Service Request Received");
00052
00053 image<rgb> *input;
00054 image<rgb> *point_cloud;
00055
00056 pcl::fromROSMsg(req.point_cloud, pcl_xyzrgb_);
00057
00058 input = new image<rgb>(pcl_xyzrgb_.height, pcl_xyzrgb_.width);
00059 point_cloud = new image<rgb>(pcl_xyzrgb_.height, pcl_xyzrgb_.width);
00060 pcl::PointCloud<pcl::PointXYZRGB>::iterator pcl_iter = pcl_xyzrgb_.begin();
00061 for (int rr = 0; rr < pcl_xyzrgb_.height; ++rr) {
00062 for (int cc = 0; cc < pcl_xyzrgb_.width; ++cc, ++pcl_iter) {
00063 input->access[rr][cc].r = pcl_iter->r;
00064 input->access[rr][cc].g = pcl_iter->g;
00065 input->access[rr][cc].b = pcl_iter->b;
00066 point_cloud->access[rr][cc].r = pcl_iter->x*100;
00067 point_cloud->access[rr][cc].g = pcl_iter->y*100;
00068 point_cloud->access[rr][cc].b = pcl_iter->z*100;
00069 }
00070 }
00071
00072
00073
00074 this->alg_.lock();
00075
00076
00077
00078
00079 float sigma = alg_.get_sigma();
00080 float k = alg_.get_k();
00081 int min_size = alg_.get_min_size();
00082
00083 int ** cluster_labels;
00084 int num_ccs;
00085 int num_ccs_fin;
00086
00087 image<rgb> *seg = segment_image(input, point_cloud, sigma, k, min_size, &num_ccs, &num_ccs_fin, cluster_labels);
00088
00089
00090
00091
00092 this->alg_.unlock();
00093
00094
00095 res.num_clusters.data = num_ccs_fin;
00096 res.cluster_labels.data.resize(pcl_xyzrgb_.width*pcl_xyzrgb_.width);
00097 int ii = 0;
00098 for (int rr=0; rr < pcl_xyzrgb_.height; ++rr)
00099 for (int cc=0; cc < pcl_xyzrgb_.width; ++cc, ++ii)
00100 res.cluster_labels.data[ii] = cluster_labels[rr][cc];
00101
00102
00103 cv::Mat image(pcl_xyzrgb_.height, pcl_xyzrgb_.width, CV_8UC3);
00104 cv::Mat_<cv::Vec3b>::iterator img_iter = image.begin<cv::Vec3b>();
00105 for (int rr = 0; rr < image.rows; ++rr) {
00106 for (int cc = 0; cc < image.cols; ++cc, ++img_iter ) {
00107 (*img_iter)[0] = seg->access[rr][cc].r;
00108 (*img_iter)[1] = seg->access[rr][cc].g;
00109 (*img_iter)[2] = seg->access[rr][cc].b;
00110 }
00111 }
00112
00113
00114 cv_ptr_ = boost::make_shared<cv_bridge::CvImage>();
00115 cv_ptr_->header = pcl_xyzrgb_.header;
00116 cv_ptr_->encoding = sensor_msgs::image_encodings::RGB8;
00117 cv_ptr_->image = image;
00118
00119
00120 output_image_publisher_.publish(cv_ptr_->toImageMsg());
00121
00122 return true;
00123 }
00124
00125
00126
00127
00128 void LeafSegmentationAlgNode::node_config_update(Config &config, uint32_t level)
00129 {
00130 this->alg_.lock();
00131
00132 this->alg_.unlock();
00133 }
00134
00135 void LeafSegmentationAlgNode::addNodeDiagnostics(void)
00136 {
00137 }
00138
00139
00140 int main(int argc,char *argv[])
00141 {
00142 return algorithm_base::main<LeafSegmentationAlgNode>(argc, argv, "leaf_segmentation_alg_node");
00143 }