leaf_segmentation_alg_node.cpp
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   //init class attributes if necessary
00009   //this->loop_rate_ = 2;//in [Hz]
00010 
00011   // [init publishers]
00012   output_image_publisher_ = public_node_handle_.advertise<sensor_msgs::Image>("output_image", 1);
00013   
00014   // [init subscribers]
00015 
00016   // [init services]
00017   get_clustered_image_srv_ = public_node_handle_.advertiseService("get_clustered_image_srv", &LeafSegmentationAlgNode::get_clustered_image_srv_callback, this);
00018   
00019   // [init clients]
00020   
00021   // [init action servers]
00022   
00023   // [init action clients]
00024 }
00025 
00026 LeafSegmentationAlgNode::~LeafSegmentationAlgNode(void)
00027 {
00028   // [free dynamic memory]
00029 }
00030 
00031 void LeafSegmentationAlgNode::mainNodeThread(void)
00032 {
00033   // [fill msg structures]
00034   // this->Image_msg_.data = my_var;
00035   
00036   // [fill srv structure and make request to the server]
00037   
00038   // [fill action structure and make request to the action server]
00039 
00040   // ROS_INFO("LeafSegmentationAlgNode::mainNodeThread"); 
00041 
00042   // [publish messages]
00043 //  output_image_publisher_.publish(cv_ptr_->toImageMsg());
00044 }
00045 
00046 /*  [subscriber callbacks] */
00047 
00048 /*  [service callbacks] */
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   // Convert from pcl_xyzrgb to image and point_cloud 2D images 
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   //savePPM(point_cloud, "/home/sfoix/temp/point_cloud.ppm");
00072 
00073   //use appropiate mutex to shared variables if necessary 
00074   this->alg_.lock(); 
00075 
00076   //std::cout << msg->data << std::endl; 
00077 
00078   // get parameters from dynamic reconfigure
00079   float sigma = alg_.get_sigma();
00080   float k = alg_.get_k();
00081   int min_size = alg_.get_min_size();
00082   // outputs
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   //savePPM(input, "/home/sfoix/temp/input_c.ppm");
00089   //savePPM(seg, "/home/sfoix/temp/segment_c.ppm");
00090 
00091   //unlock previously blocked shared variables 
00092   this->alg_.unlock(); 
00093 
00094   // Create publisher messages
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   // Create segmented rgb image for publishing it afterwards
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 //  imwrite("/home/sfoix/temp/input_cv.jpg", image);
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   // [publish messages]
00120   output_image_publisher_.publish(cv_ptr_->toImageMsg());
00121 
00122   return true;
00123 }
00124 /*  [action callbacks] */
00125 
00126 /*  [action requests] */
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 /* main function */
00140 int main(int argc,char *argv[])
00141 {
00142   return algorithm_base::main<LeafSegmentationAlgNode>(argc, argv, "leaf_segmentation_alg_node");
00143 }


iri_leaf_segmentation
Author(s): Sergi Foix
autogenerated on Fri Dec 6 2013 20:27:24