unified_tracker_node.cpp
Go to the documentation of this file.
1 #include <cv_bridge/cv_bridge.h>
2 #include <homography_optimizer/unified.h>
4 #include <opencv2/highgui/highgui.hpp>
5 #include <ros/ros.h>
6 #include <std_msgs/Char.h>
7 #include <vtec_msgs/TrackingResult.h>
8 
10 {
11  STARTING = 0,
14 };
15 
16 VTEC::UnifiedHomographyOptimizer* unified_optimizer;
17 
18 cv::Mat H;
19 float alpha, beta;
22 
26 
29 
30 bool start_command = false;
31 cv::Mat cur_img;
32 
33 int image_index = 0;
34 
46 void fillTrackingMsg(vtec_msgs::TrackingResult& msg, const double score, const cv::Mat& H, const float alpha,
47  const float beta, int bbox_size_x, int bbox_size_y)
48 {
49  cv::Point2f p1(0, 0), p2(0, bbox_size_y), p3(bbox_size_x, 0), p4(bbox_size_x, bbox_size_y);
50  VTEC::warpPoints(p1, H);
51  VTEC::warpPoints(p2, H);
52  VTEC::warpPoints(p3, H);
53  VTEC::warpPoints(p4, H);
54 
55  geometry_msgs::Point p;
56  p.z = 0.0;
57  p.x = p1.x;
58  p.y = p1.y;
59  msg.corners[0] = p;
60 
61  p.x = p2.x;
62  p.y = p2.y;
63  msg.corners[1] = p;
64 
65  p.x = p3.x;
66  p.y = p3.y;
67  msg.corners[2] = p;
68 
69  p.x = p4.x;
70  p.y = p4.y;
71  msg.corners[3] = p;
72 
73  msg.alpha = alpha;
74  msg.beta = beta;
75  msg.score = score;
76 
77  msg.homography[0] = H.at<double>(0, 0);
78  msg.homography[1] = H.at<double>(0, 1);
79  msg.homography[2] = H.at<double>(0, 2);
80  msg.homography[3] = H.at<double>(1, 0);
81  msg.homography[4] = H.at<double>(1, 1);
82  msg.homography[5] = H.at<double>(1, 2);
83  msg.homography[6] = H.at<double>(2, 0);
84  msg.homography[7] = H.at<double>(2, 1);
85  msg.homography[8] = H.at<double>(2, 2);
86 }
87 
92 {
93  H = cv::Mat::eye(3, 3, CV_64F);
94  H.at<double>(0, 2) = BBOX_POS_X;
95  H.at<double>(1, 2) = BBOX_POS_Y;
96  unified_optimizer->setHomography(H);
97  alpha = 1.0;
98  beta = 0.0;
99 
101  cv::Mat reference_template;
102  unified_optimizer->getReferenceTemplate(reference_template);
103 
104  reference_template.convertTo(out_ref_template, CV_8U);
105 }
106 
112 void imageCallback(const sensor_msgs::ImageConstPtr& msg)
113 {
114  try
115  {
116  cur_img = cv_bridge::toCvShare(msg, "mono8")->image;
117  vtec_msgs::TrackingResult result_msg;
118  result_msg.header = msg->header;
119 
120  cv::imwrite("/home/mestrado/datasets/" + std::to_string(image_index++) + ".jpg", cur_img);
121  cv::Mat H_test = H.clone();
122  float alpha_test = alpha;
123  float beta_test = beta;
124 
125  double zncc = 0.0;
126 
127  bool force_reference_image_pub = false;
128 
129  if (start_command)
130  {
131  ROS_INFO("Reference Image Selected");
132  start_command = false;
133  start_tracking();
134  force_reference_image_pub = true;
135  state = TRACKING;
136  }
137 
138  if (state != STARTING)
139  {
140  zncc = unified_optimizer->optimize(cur_img, H_test, alpha_test, beta_test, VTEC::ZNCC_PREDICTOR);
141 
142  state = TRACKING;
143  H = H_test;
144  alpha = alpha_test;
145  beta = beta_test;
146 
147  cv::Mat current_template;
148  unified_optimizer->getCurrentTemplate(current_template);
149 
150  if (zncc > 0.7)
151  {
152  VTEC::drawResult(cur_img, H, zncc, BBOX_SIZE_X, BBOX_SIZE_Y, cv::Scalar(0.0, 255.0, 0.0));
153  cv::Mat out_cur_template;
154  current_template.convertTo(out_cur_template, CV_8U);
155  sensor_msgs::ImagePtr stabilized_msg =
156  cv_bridge::CvImage(std_msgs::Header(), "mono8", out_cur_template).toImageMsg();
157  stabilized_pub_ptr->publish(stabilized_msg);
158  }
159  fillTrackingMsg(result_msg, zncc, H, alpha, beta, BBOX_SIZE_X, BBOX_SIZE_Y);
160  results_pub_ptr->publish(result_msg);
161 
162  if (ros::Time::now() - last_ref_pub_time > ros::Duration(5.0) || force_reference_image_pub)
163  {
164  sensor_msgs::ImagePtr reference_msg =
165  cv_bridge::CvImage(std_msgs::Header(), "mono8", out_ref_template).toImageMsg();
166  reference_pub_ptr->publish(reference_msg);
167  last_ref_pub_time = ros::Time::now();
168  }
169  }
170 
171  // if ((state == NOT_TRACKING && zncc > 0.5) || (state == TRACKING && zncc > 0.0))
172  // {
173 
174  // }
175  // if (!((state == NOT_TRACKING && zncc > 0.5) || (state == TRACKING && zncc > 0.0)) && (state != STARTING))
176  // {
177  // state = NOT_TRACKING;
178  // }
179 
180  if (state == STARTING)
181  {
182  VTEC::drawResult(cur_img, H, zncc, BBOX_SIZE_X, BBOX_SIZE_Y);
183  cv::putText(cur_img, "press S to start tracking", cv::Point(30, 60), CV_FONT_HERSHEY_SIMPLEX, 1, 255, 3);
184  }
185 
186  sensor_msgs::ImagePtr annotated_msg = cv_bridge::CvImage(std_msgs::Header(), "mono8", cur_img).toImageMsg();
187  annotated_msg->header.frame_id = "camera";
188  annotated_pub_ptr->publish(annotated_msg);
189  }
190  catch (cv_bridge::Exception& e)
191  {
192  ROS_ERROR("Could not convert from '%s' to 'mono8'.", msg->encoding.c_str());
193  }
194 }
195 
201 void cmdCallback(const std_msgs::Char cmd_msg)
202 {
203  switch (cmd_msg.data)
204  {
205  // S key
206  case 115:
207  ROS_INFO("(re)starting tracking");
208  start_command = true;
209  break;
210  default:
211  break;
212  }
213 }
214 
215 int main(int argc, char** argv)
216 {
217  ros::init(argc, argv, "ibgho_tracker_node");
218  ros::NodeHandle nh;
219  ros::NodeHandle nhPrivate("~");
220 
221  // Tracker Parameters
222  int MAX_NB_ITERATION_PER_LEVEL;
223  int MAX_NB_PYR_LEVEL;
224  double PIXEL_KEEP_RATE;
225  std::string reference_image_path;
226  std::string image_topic = "usb_cam/image_raw";
227  std::string homography_type = "full";
228  bool robust_flag = false;
229  double fb_factor = 1.0;
230 
231  nhPrivate.param<int>("bbox_pos_x", BBOX_POS_X, 200);
232  nhPrivate.param<int>("bbox_pos_y", BBOX_POS_Y, 150);
233  nhPrivate.param<int>("bbox_size_x", BBOX_SIZE_X, 200);
234  nhPrivate.param<int>("bbox_size_y", BBOX_SIZE_Y, 200);
235  nhPrivate.param<int>("max_nb_iter_per_level", MAX_NB_ITERATION_PER_LEVEL, 5);
236  nhPrivate.param<int>("max_nb_pyr_level", MAX_NB_PYR_LEVEL, 2);
237  nhPrivate.param<double>("sampling_rate", PIXEL_KEEP_RATE, 1.0);
238  nhPrivate.getParam("image_topic", image_topic);
239  nhPrivate.getParam("fb_factor", fb_factor);
240 
241  // Register publisher
243  image_transport::Publisher annotated_pub = it.advertise("annotated_image", 1);
244  annotated_pub_ptr = &annotated_pub;
245 
246  image_transport::Publisher stabilized_pub = it.advertise("stabilized_image", 1);
247  stabilized_pub_ptr = &stabilized_pub;
248 
249  image_transport::Publisher reference_pub = it.advertise("reference_image", 1);
250  reference_pub_ptr = &reference_pub;
251 
252  ros::Publisher results_pub = nh.advertise<vtec_msgs::TrackingResult>("tracking", 1);
253  results_pub_ptr = &results_pub;
254 
255  image_transport::Subscriber sub = it.subscribe(image_topic, 1, imageCallback);
256 
257  ros::Subscriber cmd_sub = nh.subscribe("track_cmd", 1, cmdCallback);
258 
259  // Initialize the optimizer according to the homography type
260  unified_optimizer = new VTEC::UnifiedHomographyOptimizer();
261  unified_optimizer->initialize(MAX_NB_ITERATION_PER_LEVEL, MAX_NB_PYR_LEVEL, PIXEL_KEEP_RATE);
262  unified_optimizer->setFBFactor(fb_factor);
263 
264  // Start optimizer
265  H = cv::Mat::eye(3, 3, CV_64F);
266  H.at<double>(0, 2) = BBOX_POS_X;
267  H.at<double>(1, 2) = BBOX_POS_Y;
268  unified_optimizer->setHomography(H);
269  alpha = 1.0;
270  beta = 0.0;
271 
272  last_ref_pub_time = ros::Time::now();
273 
274  ros::spin();
275 }
Subscriber subscribe(const std::string &base_topic, uint32_t queue_size, const boost::function< void(const sensor_msgs::ImageConstPtr &)> &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
image_transport::Publisher * annotated_pub_ptr
float alpha
VTEC::UnifiedHomographyOptimizer * unified_optimizer
void publish(const boost::shared_ptr< M > &message) const
int BBOX_SIZE_Y
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
float beta
ros::Publisher * results_pub_ptr
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
int BBOX_POS_Y
void cmdCallback(const std_msgs::Char cmd_msg)
Callback for the keyboard command.
ROSCPP_DECL void spin(Spinner &spinner)
image_transport::Publisher * reference_pub_ptr
int BBOX_SIZE_X
void publish(const sensor_msgs::Image &message) const
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
int BBOX_POS_X
tracking_states
cv::Mat cur_img
int main(int argc, char **argv)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void fillTrackingMsg(vtec_msgs::TrackingResult &msg, const double score, const cv::Mat &H, const float alpha, const float beta, int bbox_size_x, int bbox_size_y)
Fills a vtec_msgs/TrackingResult message.
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
Callback to handle incoming images.
cv::Mat H
int image_index
ros::Time last_ref_pub_time
bool getParam(const std::string &key, std::string &s) const
cv::Mat out_ref_template
void start_tracking()
Starts a tracking.
static Time now()
#define ROS_ERROR(...)
image_transport::Publisher * stabilized_pub_ptr
bool start_command


vtec_tracker
Author(s):
autogenerated on Sat Dec 7 2019 03:17:05