remove_background_alg_node.cpp
Go to the documentation of this file.
00001 #include "remove_background_alg_node.h"
00002 
00003 
00004 using namespace cv;
00005 
00006 RemoveBackgroundAlgNode::RemoveBackgroundAlgNode(void) :
00007   algorithm_base::IriBaseAlgorithm<RemoveBackgroundAlgorithm>()
00008 {
00009         //init class attributes if necessary
00010         //this->loop_rate_ = 2;//in [Hz]
00011 //      background_image_ = imread("/tmp/image_background.png");
00012         has_background_ = false;
00013 
00014         // [init publishers]
00015         this->image_out_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("image_out", 1);
00016         
00017         // [init subscribers]
00018         this->image_in_subscriber_ = this->public_node_handle_.subscribe("image_in", 1, &RemoveBackgroundAlgNode::image_in_callback, this);
00019         
00020         // [init services]
00021         this->set_background_image_server_ = this->public_node_handle_.advertiseService("set_background_image", &RemoveBackgroundAlgNode::set_background_imageCallback, this);
00022         
00023         // [init clients]
00024         
00025         // [init action servers]
00026         
00027         // [init action clients]
00028         
00029 }
00030 
00031 RemoveBackgroundAlgNode::~RemoveBackgroundAlgNode(void)
00032 {
00033         // [free dynamic memory]
00034 }
00035 
00036 void RemoveBackgroundAlgNode::mainNodeThread(void)
00037 {
00038         // [fill msg structures]
00039         //this->Image_msg_.data = my_var;
00040         
00041         // [fill srv structure and make request to the server]
00042         
00043         // [fill action structure and make request to the action server]
00044 
00045         // [publish messages]
00046         //this->image_out_publisher_.publish(this->Image_msg_);
00047 }
00048 
00049 /*  [subscriber callbacks] */
00050 void RemoveBackgroundAlgNode::image_in_callback(const sensor_msgs::Image::ConstPtr& msg) 
00051 { 
00052         cv_bridge::CvImagePtr cv_ptr;
00053         try
00054         {
00055                 cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
00056         }
00057         catch (cv_bridge::Exception& e)
00058         {
00059                 ROS_ERROR("cv_bridge exception: %s", e.what());
00060                 return;
00061         }
00062         
00063         cv_ptr->image = processImage(cv_ptr->image);
00064         
00065         if (cv_ptr->image.cols != 0){
00066                 ROS_DEBUG_STREAM("Publishing background substraction mask");
00067                 this->image_out_publisher_.publish(cv_ptr->toImageMsg());
00068         }
00069 
00070         //use appropiate mutex to shared variables if necessary 
00071         //this->alg_.lock(); 
00072         //this->image_in_mutex_.enter(); 
00073 
00074         //unlock previously blocked shared variables 
00075         //this->alg_.unlock(); 
00076         //this->image_in_mutex_.exit(); 
00077 }
00078 
00079 /*  [service callbacks] */
00080 bool RemoveBackgroundAlgNode::set_background_imageCallback(iri_perception_msgs::SetImage::Request &req, iri_perception_msgs::SetImage::Response &res) 
00081 { 
00082         ROS_DEBUG("RemoveBackgroundAlgNode::set_background_imageCallback: New Request Received!"); 
00083         
00084         cv_bridge::CvImagePtr cv_ptr;
00085         try
00086         {
00087                 cv_ptr = cv_bridge::toCvCopy(req.image_in, sensor_msgs::image_encodings::BGR8);
00088         }
00089         catch (cv_bridge::Exception& e)
00090         {
00091                 ROS_ERROR("cv_bridge exception: %s", e.what());
00092                 res.success = false;
00093                 return false;
00094         }
00095         
00096         //use appropiate mutex to shared variables if necessary 
00097         background_image_ = cv_ptr->image;
00098         has_background_ = true;
00099         
00100         res.success = true;
00101         
00102         return true; 
00103 }
00104 
00105 /*  [action callbacks] */
00106 
00107 /*  [action requests] */
00108 
00109 /* Other */
00110 cv::Mat RemoveBackgroundAlgNode::processImage(cv::Mat image)
00111 {
00112         Mat mask_bgr;
00113         
00114         // check if has background image
00115         if (!has_background_) {
00116                 ROS_WARN("RemoveBackgroundAlgNode needs background image to be set.");
00117                 mask_bgr = Mat::zeros(0, 0, 0);
00118         }
00119         else {
00120                 cv::Mat differences, mask_gray;
00121                 
00122                 // differences
00123                 cv::absdiff(image, background_image_, differences);
00124                 
00125                 // threshold
00126                 cv::threshold(differences, differences, this->binarize_threshold_, 255, cv::THRESH_BINARY);
00127                 cv::cvtColor(differences, mask_gray, CV_BGR2GRAY);
00128                 cv::threshold(mask_gray, mask_gray, 10, 255, cv::THRESH_BINARY);
00129                 cv::cvtColor(mask_gray, mask_bgr, CV_GRAY2BGR);
00130         }
00131         
00132         return mask_bgr;
00133 }
00134 
00135 void RemoveBackgroundAlgNode::node_config_update(Config &config, uint32_t level)
00136 {
00137         this->alg_.lock();
00138         
00139         this->binarize_threshold_ = config.threshold;
00140 
00141         // save the current configuration
00142         this->alg_.config_=config;
00143         
00144         this->alg_.unlock();
00145 }
00146 
00147 void RemoveBackgroundAlgNode::addNodeDiagnostics(void)
00148 {
00149 }
00150 
00151 /* main function */
00152 int main(int argc,char *argv[])
00153 {
00154         return algorithm_base::main<RemoveBackgroundAlgNode>(argc, argv, "remove_background_alg_node");
00155 }


iri_opencv_filters
Author(s): David Martinez
autogenerated on Fri Dec 6 2013 22:43:25