00001 #include "camera_object_detection_alg.h"
00002
00003 CameraObjectDetectionAlgorithm::CameraObjectDetectionAlgorithm(void)
00004 {
00005 this->ptrPrms = &this->prms;
00006 this->ptrRFs = &this->RFs;
00007 this->ptrClfr = &this->clfr;
00008
00009 ptrRFs->fun_set_values(ptrPrms->fun_get_num_ferns(), ptrPrms->fun_get_num_features(), ptrPrms->fun_get_fern_size());
00010 ptrRFs->fun_random_ferns();
00011 }
00012
00013 CameraObjectDetectionAlgorithm::~CameraObjectDetectionAlgorithm(void)
00014 {
00015 }
00016
00017 void CameraObjectDetectionAlgorithm::config_update(Config& new_cfg, uint32_t level)
00018 {
00019 this->lock();
00020
00021
00022 this->config_=new_cfg;
00023
00024 this->unlock();
00025 }
00026
00027
00028 bool CameraObjectDetectionAlgorithm::init_detector(sensor_msgs::ImageConstPtr &image,cv::Rect &coord)
00029 {
00030 ROS_INFO("CameraObjectDetectionAlgorithm::init_detector");
00031 this->lock();
00032 try
00033 {
00034
00035 this->cv_image = cv_bridge::toCvCopy(image,sensor_msgs::image_encodings::BGR8);
00036 this->img=this->cv_image->image;
00037 this->show_img=this->cv_image->image;
00038
00039 CvRect box = cvRect(coord.x-coord.width/2,coord.y-coord.height/2 ,coord.width ,coord.height);
00040
00041 ROS_INFO("CameraObjectDetectionAlgorithm::init_detector: fun_train_classifier(..., img, box)");
00042 fun_train_classifier(this->ptrPrms, this->ptrClfr, this->ptrRFs, &this->img, box);
00043
00044 this->unlock();
00045 ROS_INFO("CameraObjectDetectionAlgorithm::init_detector done");
00046 return true;
00047 }
00048 catch (cv_bridge::Exception& e)
00049 {
00050 ROS_ERROR("cv_bridge exception: %s", e.what());
00051 this->unlock();
00052 return false;
00053 }
00054 }
00055
00056 bool CameraObjectDetectionAlgorithm::detect(sensor_msgs::ImageConstPtr &image)
00057 {
00058 try
00059 {
00060 this->lock();
00061
00062 this->cv_image = cv_bridge::toCvCopy(image, sensor_msgs::image_encodings::BGR8);
00063 this->img=this->cv_image->image;
00064 this->show_img=this->cv_image->image;
00065
00066
00067
00068 this->detections = fun_detection(this->ptrPrms, this->ptrClfr, this->ptrRFs, &this->img);
00069
00070
00071 this->assistance = fun_check_assistance(this->ptrPrms, this->detections);
00072
00073
00074 if(this->assistance)
00075 {
00076
00077 fun_show_detections(this->ptrPrms, this->ptrClfr, &this->show_img, this->detections, this->assistance);
00078 this->unlock();
00079 return assistance;
00080 }
00081 else
00082 {
00083
00084 fun_learning(this->ptrPrms, this->ptrRFs, this->ptrClfr, &this->img, this->detections, this->assistance, true);
00085
00086
00087 fun_show_detections(this->ptrPrms, this->ptrClfr, &this->show_img, this->detections, this->assistance);
00088
00089 this->unlock();
00090 return this->assistance;
00091 }
00092 }
00093 catch (cv_bridge::Exception& e)
00094 {
00095 this->unlock();
00096 ROS_ERROR("cv_bridge exception: %s", e.what());
00097 return true;
00098 }
00099 }
00100
00101 bool CameraObjectDetectionAlgorithm::learn_detector(bool valid)
00102 {
00103 try
00104 {
00105 this->lock();
00106
00107
00108 fun_learning(this->ptrPrms, this->ptrRFs, this->ptrClfr, &this->img, this->detections, this->assistance, valid);
00109
00110
00111 fun_show_detections(this->ptrPrms, this->ptrClfr, &this->show_img, this->detections, this->assistance);
00112
00113 this->unlock();
00114 return true;
00115 }
00116 catch (cv_bridge::Exception& e)
00117 {
00118 this->unlock();
00119 ROS_ERROR("cv_bridge exception: %s", e.what());
00120 return false;
00121 }
00122 }
00123
00124 bool CameraObjectDetectionAlgorithm::get_output_rect(cv::Rect & rect)
00125 {
00126 int xa, ya, xb, yb;
00127 float value;
00128 get_max_detection_values(this->detections, xa, ya, xb, yb, value);
00129
00130 if(value>0.5)
00131 {
00132 rect.width = abs(xa-xb);
00133 rect.height = abs(ya-yb);
00134 rect.x = xa+ rect.width/2;
00135 rect.y = ya+ rect.height/2;
00136 return true;
00137 }
00138 else
00139 {
00140 rect.width = 0;
00141 rect.height = 0;
00142 rect.x = 0;
00143 rect.y = 0;
00144 return false;
00145 }
00146 }
00147
00148 void CameraObjectDetectionAlgorithm::get_output_image(sensor_msgs::ImagePtr &image)
00149 {
00150 cv_bridge::CvImage roscv_outImage;
00151 roscv_outImage.header = this->cv_image->header;
00152 roscv_outImage.encoding = this->cv_image->encoding;
00153 roscv_outImage.image = cv::cvarrToMat(&this->show_img);
00154 image = roscv_outImage.toImageMsg();
00155 }