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
00010
00011
00012 has_background_ = false;
00013
00014
00015 this->image_out_publisher_ = this->public_node_handle_.advertise<sensor_msgs::Image>("image_out", 1);
00016
00017
00018 this->image_in_subscriber_ = this->public_node_handle_.subscribe("image_in", 1, &RemoveBackgroundAlgNode::image_in_callback, this);
00019
00020
00021 this->set_background_image_server_ = this->public_node_handle_.advertiseService("set_background_image", &RemoveBackgroundAlgNode::set_background_imageCallback, this);
00022
00023
00024
00025
00026
00027
00028
00029 }
00030
00031 RemoveBackgroundAlgNode::~RemoveBackgroundAlgNode(void)
00032 {
00033
00034 }
00035
00036 void RemoveBackgroundAlgNode::mainNodeThread(void)
00037 {
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 }
00048
00049
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
00071
00072
00073
00074
00075
00076
00077 }
00078
00079
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
00097 background_image_ = cv_ptr->image;
00098 has_background_ = true;
00099
00100 res.success = true;
00101
00102 return true;
00103 }
00104
00105
00106
00107
00108
00109
00110 cv::Mat RemoveBackgroundAlgNode::processImage(cv::Mat image)
00111 {
00112 Mat mask_bgr;
00113
00114
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
00123 cv::absdiff(image, background_image_, differences);
00124
00125
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
00142 this->alg_.config_=config;
00143
00144 this->alg_.unlock();
00145 }
00146
00147 void RemoveBackgroundAlgNode::addNodeDiagnostics(void)
00148 {
00149 }
00150
00151
00152 int main(int argc,char *argv[])
00153 {
00154 return algorithm_base::main<RemoveBackgroundAlgNode>(argc, argv, "remove_background_alg_node");
00155 }