00001 #include "get_board_information_alg.h"
00002
00003 using namespace cv;
00004
00005 GetBoardInformationAlgorithm::GetBoardInformationAlgorithm(void)
00006 {
00007 current_believe = cv::Mat::zeros(cv::Size(640,480), CV_8UC1);
00008 unreachable_zones = cv::Mat::zeros(cv::Size(640,480), CV_8UC1);
00009 }
00010
00011 GetBoardInformationAlgorithm::~GetBoardInformationAlgorithm(void)
00012 {
00013 }
00014
00015 void GetBoardInformationAlgorithm::config_update(Config& new_cfg, uint32_t level)
00016 {
00017 this->lock();
00018
00019 this->believe_increase = new_cfg.believe_increase;
00020 this->believe_decrease = new_cfg.believe_decrease;
00021 this->believe_threshold = new_cfg.believe_threshold;
00022
00023 this->remove_unreachable_points = new_cfg.remove_unreachable_points;
00024
00025
00026 this->config_=new_cfg;
00027
00028 this->unlock();
00029 }
00030
00031
00032 void GetBoardInformationAlgorithm::updateCurrentBelieve(const sensor_msgs::Image::ConstPtr& current_image_msg) {
00033 cv_bridge::CvImagePtr cv_ptr;
00034 cv::Mat image;
00035 int new_val;
00036
00037 try
00038 {
00039 cv_ptr = cv_bridge::toCvCopy(current_image_msg, sensor_msgs::image_encodings::BGR8);
00040 }
00041 catch (cv_bridge::Exception& e)
00042 {
00043 ROS_ERROR("cv_bridge exception: %s", e.what());
00044
00045 }
00046
00047 image = cv_ptr->image;
00048
00049 cvtColor(image, image, CV_BGR2GRAY);
00050 threshold( image, image, 10, 255, THRESH_BINARY );
00051
00052 MatConstIterator_<uchar> img_it = image.begin<uchar>(), img_it_end = image.end<uchar>();
00053 MatIterator_<uchar> bel_it = current_believe.begin<uchar>();
00054
00055 for( ; img_it != img_it_end; ++img_it, ++bel_it )
00056 {
00057 if (*img_it > 1) {
00058 new_val = *bel_it + believe_increase;
00059 if (new_val > 255)
00060 new_val = 255;
00061 }
00062 else {
00063 new_val = *bel_it + believe_decrease;
00064 if (new_val < 0)
00065 new_val = 0;
00066 }
00067
00068 *bel_it = (uchar)new_val;
00069 }
00070
00071
00072 threshold( current_believe, cv_ptr->image, believe_threshold, 255, THRESH_BINARY );
00073 cv_ptr->encoding = sensor_msgs::image_encodings::TYPE_8UC1;
00074
00075 if (this->remove_unreachable_points) {
00076 Mat substraction_mat(cv::Size(640,480), CV_8UC1, 3);
00077 subtract(unreachable_zones, substraction_mat, unreachable_zones);
00078 }
00079 }
00080
00081
00082 void GetBoardInformationAlgorithm::updateUnreachableZones(const estirabot_msgs::ImagePoint::ConstPtr& point)
00083 {
00084 if (this->remove_unreachable_points)
00085 cv::circle(unreachable_zones, cv::Point(point->x, point->y), 10, 255, -1);
00086 }
00087
00088
00089
00090 sensor_msgs::Image GetBoardInformationAlgorithm::joinImageAndBelieve(const sensor_msgs::Image current_image_msg) {
00091 cv_bridge::CvImagePtr cv_ptr;
00092 cv::Mat mask_believe, mask_unreachable, image_res;
00093
00094 try
00095 {
00096 cv_ptr = cv_bridge::toCvCopy(current_image_msg, sensor_msgs::image_encodings::BGR8);
00097 }
00098 catch (cv_bridge::Exception& e)
00099 {
00100 ROS_ERROR("cv_bridge exception: %s", e.what());
00101
00102 }
00103
00104
00105 cvtColor(cv_ptr->image, image_res, CV_BGR2GRAY);
00106 threshold( image_res, image_res, 10, 255, THRESH_BINARY );
00107
00108
00109 threshold( current_believe, mask_believe, believe_threshold, 255, THRESH_BINARY );
00110
00111 bitwise_and(image_res, mask_believe, image_res);
00112
00113 if (this->remove_unreachable_points) {
00114
00115 threshold( unreachable_zones, mask_unreachable, 10, 255, THRESH_BINARY_INV );
00116 bitwise_and(image_res, mask_unreachable, image_res);
00117 }
00118
00119
00120 cv_ptr->image = image_res;
00121 cv_ptr->encoding = sensor_msgs::image_encodings::TYPE_8UC1;
00122
00123 return *cv_ptr->toImageMsg();
00124 }
00125
00126
00127 sensor_msgs::Image GetBoardInformationAlgorithm::getCurrentBelieveColors()
00128 {
00129 cv_bridge::CvImagePtr cv_ptr;
00130 cv::Mat believe_color, unreachable_color, image_res;
00131
00132 Mat mat_red(cv::Size(640,480), CV_8UC3, Scalar(0,0,255));
00133 Mat mat_green(cv::Size(640,480), CV_8UC3, Scalar(255,0,0));
00134
00135 cvtColor(current_believe, believe_color, CV_GRAY2BGR);
00136 bitwise_and(believe_color, mat_red, believe_color);
00137
00138 cvtColor(unreachable_zones, unreachable_color, CV_GRAY2BGR);
00139 bitwise_and(unreachable_color, mat_green, unreachable_color);
00140
00141 bitwise_or(unreachable_color, believe_color, image_res);
00142
00143
00144 cv_bridge::CvImage cvbridge_aux;
00145 cvbridge_aux.encoding = sensor_msgs::image_encodings::BGR8;
00146 cvbridge_aux.image = image_res;
00147 cvbridge_aux.header.stamp = ros::Time::now();
00148
00149 return *cvbridge_aux.toImageMsg();
00150 }
00151