leaf_fitting_alg_node.cpp
Go to the documentation of this file.
00001 #include "leaf_fitting_alg_node.h"
00002 
00003 LeafFittingAlgNode::LeafFittingAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<LeafFittingAlgorithm>()
00005 {
00006   //init class attributes if necessary
00007   //this->loop_rate_ = 2;//in [Hz]
00008 
00009   // [init publishers]
00010   this->output_image_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("output_image", 100);
00011   
00012   // [init subscribers]
00013   
00014   // [init services]
00015   get_confidence_leaf_fitting_srv_ = public_node_handle_.advertiseService("get_confidence_leaf_fitting_srv", &LeafFittingAlgNode::get_confidence_leaf_fitting_srv_callback, this);
00016 
00017   // [init clients]
00018   
00019   // [init action servers]
00020   
00021   // [init action clients]
00022 }
00023 
00024 LeafFittingAlgNode::~LeafFittingAlgNode(void)
00025 {
00026   // [free dynamic memory]
00027 }
00028 
00029 void LeafFittingAlgNode::mainNodeThread(void)
00030 {
00031 
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 
00041   // [publish messages]
00042 }
00043 
00044 /*  [subscriber callbacks] */
00045 
00046 /*  [service callbacks] */
00047 bool LeafFittingAlgNode::get_confidence_leaf_fitting_srv_callback(iri_leaf_fitting::GetConfidenceLeafFitting::Request &req, iri_leaf_fitting::GetConfidenceLeafFitting::Response &res)
00048 {
00049   ROS_INFO("LeafSegmentationAlgNode::get_clustered_image_srv_callback: New Service Request Received");
00050 
00051   // Memory allocation
00052   cluster_labels_ = (int**)malloc(sizeof(int*)*200);
00053   for (int k=0; k<200; k++)
00054     cluster_labels_[k] = (int*)malloc(sizeof(int)*200);  
00055  
00056   //use appropiate mutex to shared variables if necessary 
00057   this->alg_.lock(); 
00058  
00059   // fill in the cluster_label array with the msg data
00060   int ii=0;
00061   for (int rr=0; rr < 200; ++rr)
00062     for (int cc=0; cc < 200; ++cc, ++ii) 
00063         cluster_labels_[rr][cc] = req.cluster_labels.data[ii];
00064 
00065   num_ccs_fin_ = req.num_clusters.data;
00066 
00067   //unlock previously blocked shared variables 
00068   this->alg_.unlock(); 
00069 
00070   ROS_INFO("1)Num of clusters: %d\n", num_ccs_fin_);
00071 
00072   //TODO: This is too hardcoded, try to get the images dynamically
00073   image<rgb> *leaf_model = loadPPM("/home/sfoix/programming/iri/iri-ros-pkg/iri_perception/iri_leaf_fitting/model/model_A.ppm");
00074   image<rgb> *leaf_model_area = loadPPM("/home/sfoix/programming/iri/iri-ros-pkg/iri_perception/iri_leaf_fitting/model/model_A_bound.ppm");
00075   
00076   // Main Algorithm
00077   double *fit_score_list;
00078   image<rgb> *leaf_confidence = leaf_fitting(cluster_labels_, leaf_model, leaf_model_area, num_ccs_fin_, fit_score_list);
00079   
00080   res.fit_score_list.data.resize(num_ccs_fin_);
00081   for (int ii=0; ii < num_ccs_fin_; ++ii)
00082     res.fit_score_list.data[ii] = fit_score_list[ii];
00083 
00084   // Create segmented rgb image for publishing it afterwards
00085   //TODO: This is too hardcoded, try to get the matrix dimensions from somewhere
00086   cv::Mat image(200, 200, CV_8UC3);
00087   cv::Mat_<cv::Vec3b>::iterator img_iter = image.begin<cv::Vec3b>();
00088   for (int rr = 0; rr < image.rows; ++rr) {                         
00089     for (int cc = 0; cc < image.cols; ++cc, ++img_iter ) {         
00090       (*img_iter)[0] = leaf_confidence->access[rr][cc].r;  
00091       (*img_iter)[1] = leaf_confidence->access[rr][cc].g; 
00092       (*img_iter)[2] = leaf_confidence->access[rr][cc].b;    
00093     }                                                                 
00094   }                                                                   
00095   imwrite("/home/sfoix/temp/input_cv.jpg", image);
00096 
00097   cv_ptr_ = boost::make_shared<cv_bridge::CvImage>();
00098   //TODO: This is too hardcoded, try to see if it can be done more elegantly
00099   cv_ptr_->header.seq = 1;
00100   cv_ptr_->header.stamp = ros::Time();
00101   cv_ptr_->header.frame_id = "camera_frame";
00102   cv_ptr_->encoding = sensor_msgs::image_encodings::RGB8;
00103   cv_ptr_->image = image;
00104 
00105   this->output_image_publisher_.publish(cv_ptr_->toImageMsg());
00106 
00107   return 1;
00108 }
00109 
00110 
00111 /*  [action callbacks] */
00112 
00113 /*  [action requests] */
00114 
00115 void LeafFittingAlgNode::node_config_update(Config &config, uint32_t level)
00116 {
00117   this->alg_.lock();
00118 
00119   this->alg_.unlock();
00120 }
00121 
00122 void LeafFittingAlgNode::addNodeDiagnostics(void)
00123 {
00124 }
00125 
00126 /* main function */
00127 int main(int argc,char *argv[])
00128 {
00129   return algorithm_base::main<LeafFittingAlgNode>(argc, argv, "leaf_fitting_alg_node");
00130 }


iri_leaf_fitting
Author(s): Sergi Foix
autogenerated on Fri Dec 6 2013 20:28:17