1 #include <cv_bridge/cv_bridge.h> 2 #include <homography_optimizer/ibg.h> 4 #include <opencv2/highgui/highgui.hpp> 6 #include <std_msgs/Char.h> 7 #include <vtec_msgs/TrackingResult.h> 43 const cv::Mat &
H,
const float alpha,
const float beta,
44 int bbox_size_x,
int bbox_size_y) {
45 cv::Point2f p1(0, 0), p2(0, bbox_size_y), p3(bbox_size_x, 0),
46 p4(bbox_size_x, bbox_size_y);
47 VTEC::warpPoints(p1, H);
48 VTEC::warpPoints(p2, H);
49 VTEC::warpPoints(p3, H);
50 VTEC::warpPoints(p4, H);
52 geometry_msgs::Point p;
74 msg.homography[0] = H.at<
double>(0, 0);
75 msg.homography[1] = H.at<
double>(0, 1);
76 msg.homography[2] = H.at<
double>(0, 2);
77 msg.homography[3] = H.at<
double>(1, 0);
78 msg.homography[4] = H.at<
double>(1, 1);
79 msg.homography[5] = H.at<
double>(1, 2);
80 msg.homography[6] = H.at<
double>(2, 0);
81 msg.homography[7] = H.at<
double>(2, 1);
82 msg.homography[8] = H.at<
double>(2, 2);
89 H = cv::Mat::eye(3, 3, CV_64F);
98 cv::Mat reference_template;
111 cur_img = cv_bridge::toCvShare(msg,
"mono8")->image;
112 vtec_msgs::TrackingResult result_msg;
113 result_msg.header = msg->header;
115 cv::Mat H_test =
H.clone();
116 float alpha_test =
alpha;
117 float beta_test =
beta;
121 bool force_reference_image_pub =
false;
124 ROS_INFO(
"Reference Image Selected");
127 force_reference_image_pub =
true;
133 VTEC::ZNCC_PREDICTOR);
143 cv::Mat current_template;
146 cv::Mat out_cur_template;
147 current_template.convertTo(out_cur_template, CV_8U);
148 sensor_msgs::ImagePtr stabilized_msg =
149 cv_bridge::CvImage(std_msgs::Header(),
"mono8", out_cur_template)
151 stabilized_pub_ptr->
publish(stabilized_msg);
154 cv::Scalar(0.0, 255.0, 0.0));
157 results_pub_ptr->
publish(result_msg);
160 force_reference_image_pub) {
161 sensor_msgs::ImagePtr reference_msg =
164 reference_pub_ptr->
publish(reference_msg);
173 cv::putText(
cur_img,
"press S to start tracking", cv::Point(30, 60),
174 CV_FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 3);
177 sensor_msgs::ImagePtr annotated_msg =
178 cv_bridge::CvImage(std_msgs::Header(),
"mono8",
cur_img).toImageMsg();
179 annotated_msg->header.frame_id =
"camera";
180 annotated_pub_ptr->
publish(annotated_msg);
181 }
catch (cv_bridge::Exception &e) {
182 ROS_ERROR(
"Could not convert from '%s' to 'mono8'.", msg->encoding.c_str());
192 switch (cmd_msg.data) {
203 int main(
int argc,
char **argv) {
204 ros::init(argc, argv,
"ibgho_tracker_node");
209 int MAX_NB_ITERATION_PER_LEVEL;
210 int MAX_NB_PYR_LEVEL;
211 double PIXEL_KEEP_RATE;
212 std::string reference_image_path;
213 std::string image_topic =
"usb_cam/image_raw";
214 std::string homography_type =
"full";
215 bool robust_flag =
false;
221 nhPrivate.
param<
int>(
"max_nb_iter_per_level", MAX_NB_ITERATION_PER_LEVEL, 5);
222 nhPrivate.
param<
int>(
"max_nb_pyr_level", MAX_NB_PYR_LEVEL, 2);
223 nhPrivate.
param<
double>(
"sampling_rate", PIXEL_KEEP_RATE, 1.0);
224 nhPrivate.
getParam(
"image_topic", image_topic);
225 nhPrivate.
getParam(
"homography_type", homography_type);
226 nhPrivate.
getParam(
"robust_flag", robust_flag);
231 annotated_pub_ptr = &annotated_pub;
235 stabilized_pub_ptr = &stabilized_pub;
238 reference_pub_ptr = &reference_pub;
241 nh.
advertise<vtec_msgs::TrackingResult>(
"tracking", 1);
242 results_pub_ptr = &results_pub;
249 if (homography_type ==
"affine") {
251 }
else if (homography_type ==
"stretch") {
257 ibg_optimizer->initialize(MAX_NB_ITERATION_PER_LEVEL, MAX_NB_PYR_LEVEL,
266 H = cv::Mat::eye(3, 3, CV_64F);
267 H.at<
double>(0, 2) = BBOX_POS_X;
268 H.at<
double>(1, 2) = BBOX_POS_Y;
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())
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
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)
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.
image_transport::Publisher * stabilized_pub_ptr
ROSCPP_DECL void spin(Spinner &spinner)
int main(int argc, char **argv)
VTEC::IBGHomographyOptimizer * ibg_optimizer
void publish(const sensor_msgs::Image &message) const
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val) const
image_transport::Publisher * annotated_pub_ptr
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
Callback to handle incoming images.
ros::Publisher * results_pub_ptr
void start_tracking()
Starts a tracking.
bool getParam(const std::string &key, std::string &s) const
void cmdCallback(const std_msgs::Char cmd_msg)
Callback for the keyboard command.
image_transport::Publisher * reference_pub_ptr
ros::Time last_ref_pub_time