sift_alg_node.cpp
Go to the documentation of this file.
00001 #include "sift_alg_node.h"
00002 
00003 SiftAlgNode::SiftAlgNode(void) :
00004   algorithm_base::IriBaseAlgorithm<SiftAlgorithm>()
00005 {
00006   //init class attributes if necessary
00007   //this->loop_rate_ = 2;//in [Hz]
00008   ROS_INFO("Enterning iri_sift.");
00009   // [init publishers]
00010   this->geo_vw_set_publisher_ = this->public_node_handle_.advertise<iri_perception_msgs::GeoVwSet>("geo_vw_set", 1);
00011   
00012   // [init subscribers]
00013   this->image_in_subscriber_ = this->public_node_handle_.subscribe("image_in", 1, &SiftAlgNode::image_in_callback, this);
00014   
00015   // [init services]
00016   this->descriptors_from_image_server_ = this->public_node_handle_.advertiseService("descriptors_from_image", &SiftAlgNode::descriptors_from_imageCallback, this);
00017   this->set_sift_centroids_server_ = this->public_node_handle_.advertiseService("set_sift_centroids", &SiftAlgNode::set_sift_centroidsCallback, this);
00018   this->get_geo_vw_set_server_ = this->public_node_handle_.advertiseService("get_geo_vw_set", &SiftAlgNode::get_geo_vw_setCallback, this);
00019   
00020   // [init clients]
00021   
00022   // [init action servers]
00023   
00024   // [init action clients]
00025 }
00026 
00027 SiftAlgNode::~SiftAlgNode(void)
00028 {
00029         // [free dynamic memory]
00030 }
00031 
00032 void SiftAlgNode::mainNodeThread(void)
00033 {
00034         // [fill msg structures]
00035         //this->BagVisualWords_msg_.data = my_var;
00036         
00037         // [fill srv structure and make request to the server]
00038         
00039         // [fill action structure and make request to the action server]
00040 
00041         // [publish messages]
00042 //      this->geo_vw_set_publisher_.publish(this->BagVisualWords_msg_);
00043 }
00044 
00045 /*  [subscriber callbacks] */
00046 void SiftAlgNode::image_in_callback(const sensor_msgs::Image::ConstPtr& msg) 
00047 { 
00048         cv_bridge::CvImagePtr cv_ptr;
00049         
00050         try
00051         {
00052                 cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::RGB8);
00053         }
00054         catch (cv_bridge::Exception& e)
00055         {
00056                 ROS_ERROR("cv_bridge exception: %s", e.what());
00057                 return;
00058         }
00059         
00060         ROS_INFO("SiftAlgNode::image_in_callback: Processing image."); 
00061         
00062 //      cv::Mat descriptors;
00063 //      std::vector<cv::KeyPoint> keypoints;
00064 //      this->alg_.get_sift_descriptors(cv_ptr->image, descriptors, keypoints);
00065         
00066 //      ROS_INFO_STREAM("Tipo es " << descriptors.type());
00067 //      ROS_INFO_STREAM("Size es " << descriptors.size().width << "," << descriptors.size().height);
00068         
00069 //      this->alg_.save_descriptors(descriptors);
00070         
00071         if (this->alg_.centroids_ready_) {
00072                 ROS_DEBUG("Getting geo_vw.");
00073                 this->GeoVwSet_msg_ = this->alg_.get_geovw_from_image(cv_ptr->image);
00074                 this->geo_vw_set_publisher_.publish(this->GeoVwSet_msg_);
00075         }
00076         else
00077                 ROS_ERROR("iri_sift: Need centroids!");
00078 }
00079 
00080 /*  [service callbacks] */
00081 bool SiftAlgNode::descriptors_from_imageCallback(iri_sift::DescriptorsFromImage::Request &req, iri_sift::DescriptorsFromImage::Response &res) 
00082 { 
00083         ROS_INFO("SiftAlgNode::descriptors_from_imageCallback: New Request Received!"); 
00084 
00085         // checks and get image
00086         if (not this->alg_.centroids_ready_) {
00087                 ROS_ERROR("iri_sift: Need centroids!");
00088                 return false;
00089         }
00090         
00091         cv_bridge::CvImagePtr cv_ptr;
00092         try
00093         {
00094                 cv_ptr = cv_bridge::toCvCopy(req.image, sensor_msgs::image_encodings::RGB8);
00095         }
00096         catch (cv_bridge::Exception& e)
00097         {
00098                 ROS_ERROR("cv_bridge exception: %s", e.what());
00099                 return false;
00100         }
00101 
00102         // actual processing
00103         res.descriptor_set = this->alg_.get_descriptors_from_image(cv_ptr->image);
00104 
00105         return true; 
00106 }
00107 bool SiftAlgNode::set_sift_centroidsCallback(iri_publish_params::classifier_update_service::Request &req, iri_publish_params::classifier_update_service::Response &res) 
00108 { 
00109         ROS_INFO("SiftAlgNode::set_sift_centroidsCallback: New Request Received!"); 
00110         
00111         //use appropiate mutex to shared variables if necessary 
00112         this->alg_.lock(); 
00113         
00114         // If something fails maybe size is incorrect
00115         ROS_INFO_STREAM("Size " << req.params.update_params.size() << " y "  << req.params.update_params[0].params.size());
00116         
00117         // each row is a descriptor
00118         this->alg_.sift_centroids_ = cv::Mat::zeros(req.params.update_params.size(), req.params.update_params[0].params.size(), CV_32FC1); // (rows, cols)
00119         for(size_t i=0; i < req.params.update_params.size(); i++) {
00120                 for(size_t j=0; j< req.params.update_params[i].params.size(); j++) {
00121                         this->alg_.sift_centroids_.at<float>(i,j) = (float)req.params.update_params[i].params[j];
00122                 }
00123         }
00124         
00125         this->alg_.centroids_ready_ = true;
00126         
00127         //unlock previously blocked shared variables 
00128         this->alg_.unlock(); 
00129         
00130         res.result = true;
00131         return true; 
00132 }
00133 bool SiftAlgNode::get_geo_vw_setCallback(iri_sift::GeoVwSetSrv::Request &req, iri_sift::GeoVwSetSrv::Response &res) 
00134 { 
00135         ROS_INFO("SiftAlgNode::get_geo_vw_setCallback: New Request Received!"); 
00136         
00137         // checks and get image
00138         if (not this->alg_.centroids_ready_) {
00139                 ROS_ERROR("iri_sift: Need centroids!");
00140                 return false;
00141         }
00142         
00143         cv_bridge::CvImagePtr cv_ptr;
00144         try
00145         {
00146                 cv_ptr = cv_bridge::toCvCopy(req.image, sensor_msgs::image_encodings::RGB8);
00147         }
00148         catch (cv_bridge::Exception& e)
00149         {
00150                 ROS_ERROR("cv_bridge exception: %s", e.what());
00151                 return false;
00152         }
00153         
00154         res.geo_vw_set = this->alg_.get_geovw_from_image(cv_ptr->image);
00155 
00156         return true; 
00157 }
00158 
00159 /*  [action callbacks] */
00160 
00161 /*  [action requests] */
00162 
00163 void SiftAlgNode::node_config_update(Config &config, uint32_t level)
00164 {
00165   this->alg_.lock();
00166   
00167   this->alg_.unlock();
00168 }
00169 
00170 void SiftAlgNode::addNodeDiagnostics(void)
00171 {
00172 }
00173 
00174 /* main function */
00175 int main(int argc,char *argv[])
00176 {
00177   return algorithm_base::main<SiftAlgNode>(argc, argv, "sift_alg_node");
00178 }


iri_sift
Author(s): dmartinez
autogenerated on Fri Dec 6 2013 22:44:31