consensus_tracking.cpp
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2016, JSK Lab
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the JSK Lab nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 #include <algorithm>
37 #define _USE_MATH_DEFINES
38 #include <math.h>
39 
40 #include <opencv2/opencv.hpp>
41 
42 #include <cv_bridge/cv_bridge.h>
43 #include <sensor_msgs/Image.h>
45 
47 
48 namespace jsk_perception
49 {
51  {
52  DiagnosticNodelet::onInit();
53 
54  pnh_->param("approximate_sync", approximate_sync_, false);
55  pnh_->param("queue_size", queue_size_, 100);
56 
57  pub_mask_image_ = advertise<sensor_msgs::Image>(*pnh_, "output/mask", 1);
58  pub_debug_image_ = advertise<sensor_msgs::Image>(*pnh_, "debug/image", 1);
59 
60  // subscribers to set initial tracking window.
61  sub_image_to_init_.subscribe(*pnh_, "input", 1);
62  sub_polygon_to_init_.subscribe(*pnh_, "input/polygon", 1);
64  {
65  async_ = boost::make_shared<message_filters::Synchronizer<ApproximateSyncPolicy> >(queue_size_);
67  async_->registerCallback(boost::bind(&ConsensusTracking::setInitialWindow, this, _1, _2));
68  }
69  else
70  {
71  sync_ = boost::make_shared<message_filters::Synchronizer<ExactSyncPolicy> >(queue_size_);
73  sync_->registerCallback(boost::bind(&ConsensusTracking::setInitialWindow, this, _1, _2));
74  }
75 
76  onInitPostProcess();
77  }
78 
80  // message_filters::Synchronizer needs to be called reset
81  // before message_filters::Subscriber is freed.
82  // Calling reset fixes the following error on shutdown of the nodelet:
83  // terminate called after throwing an instance of
84  // 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::lock_error> >'
85  // what(): boost: mutex lock failed in pthread_mutex_lock: Invalid argument
86  // Also see https://github.com/ros/ros_comm/issues/720 .
87  sync_.reset();
88  async_.reset();
89  }
90 
92  {
93  // subscribers to process the tracking
94  sub_image_ = pnh_->subscribe("input", 1, &ConsensusTracking::getTrackingResult, this);
95  }
96 
98  {
100  }
101 
102  void ConsensusTracking::setInitialWindow(const sensor_msgs::Image::ConstPtr& image_msg,
103  const geometry_msgs::PolygonStamped::ConstPtr& poly_msg)
104  {
105  boost::mutex::scoped_lock lock(mutex_);
106 
107  cv::Mat image = cv_bridge::toCvCopy(image_msg, sensor_msgs::image_encodings::BGR8)->image;
108 
109  // Convert color image to gray to track.
110  cv::Mat gray;
111  cv::cvtColor(image, gray, CV_BGR2GRAY);
112 
113  // Set initial rectangle vertices
114  cv::Point2f initial_top_left = cv::Point2f(poly_msg->polygon.points[0].x, poly_msg->polygon.points[0].y);
115  cv::Point2f initial_bottom_right = cv::Point2f(poly_msg->polygon.points[1].x, poly_msg->polygon.points[1].y);
116 
117  cmt.initialise(gray, initial_top_left, initial_bottom_right);
118  window_initialized_ = true;
119  ROS_INFO("A window is initialized. top_left: (%lf, %lf), bottom_right: (%lf, %lf)",
120  initial_top_left.x, initial_top_left.y, initial_bottom_right.x, initial_bottom_right.y);
121  }
122 
123  void ConsensusTracking::getTrackingResult(const sensor_msgs::Image::ConstPtr& image_msg)
124  {
125  boost::mutex::scoped_lock lock(mutex_);
126 
127  if (!window_initialized_)
128  {
129  return;
130  }
131 
132  cv::Mat image = cv_bridge::toCvCopy(image_msg, sensor_msgs::image_encodings::BGR8)->image;
133 
134  // Convert color image to gray and track it.
135  cv::Mat gray;
136  cv::cvtColor(image, gray, CV_BGR2GRAY);
137  cmt.processFrame(gray);
138 
139  // Draw rectangle for debug view.
140  for (int i=0; i < cmt.trackedKeypoints.size(); i++)
141  {
142  cv::circle(image, cmt.trackedKeypoints[i].first.pt, 3, cv::Scalar(255, 0, 0));
143  }
144  cv::line(image, cmt.topLeft, cmt.topRight, cv::Scalar(0, 30, 255));
145  cv::line(image, cmt.topRight, cmt.bottomRight, cv::Scalar(0, 30, 255));
146  cv::line(image, cmt.bottomRight, cmt.bottomLeft, cv::Scalar(0, 30, 255));
147  cv::line(image, cmt.bottomLeft, cmt.topLeft, cv::Scalar(0, 30, 255));
148 
149  // Generate object mask.
150  cv::Point diff = cmt.topLeft - cmt.bottomLeft;
151  cv::Point center2 = cmt.topLeft + cmt.bottomRight;
152  cv::Point2f center = cv::Point2f(center2.x / 2, center2.y / 2);
153  cv::Size2f size = cv::Size2f(cv::norm(cmt.topLeft - cmt.topRight), cv::norm(cmt.topLeft - cmt.bottomLeft));
154  cv::RotatedRect rRect = cv::RotatedRect(center, size, asin(diff.x / cv::norm(diff)) * 180 / M_PI);
155  cv::Point2f vertices2f[4];
156  rRect.points(vertices2f);
157  cv::Point vertices[4];
158  for (int i = 0; i < 4; ++i)
159  {
160  vertices[i] = vertices2f[i];
161  }
162  cv::Mat mask = cv::Mat::zeros(image.rows, image.cols, CV_8UC1);
163  cv::fillConvexPoly(mask, vertices, 4, cv::Scalar(255));
164 
165  // Publish all.
166  pub_mask_image_.publish(cv_bridge::CvImage(image_msg->header,
168  mask).toImageMsg());
169  pub_debug_image_.publish(cv_bridge::CvImage(image_msg->header,
171  image).toImageMsg());
172  }
173 } // namespace jsk_perception
174 
consensus_tracking.h
jsk_perception::ConsensusTracking
Definition: consensus_tracking.h:82
image_encodings.h
i
int i
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
M_PI
#define M_PI
_1
boost::arg< 1 > _1
jsk_perception::ConsensusTracking::pub_debug_image_
ros::Publisher pub_debug_image_
Definition: consensus_tracking.h:136
jsk_perception::ConsensusTracking::mutex_
boost::mutex mutex_
Definition: consensus_tracking.h:146
ros::Subscriber::shutdown
void shutdown()
random_forest_client_sample.circle
circle
Definition: random_forest_client_sample.py:78
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
jsk_perception::ConsensusTracking::setInitialWindow
void setInitialWindow(const sensor_msgs::Image::ConstPtr &img_msg, const geometry_msgs::PolygonStamped::ConstPtr &poly_msg)
Definition: consensus_tracking.cpp:102
jsk_perception::ConsensusTracking::sub_image_
ros::Subscriber sub_image_
Definition: consensus_tracking.h:138
jsk_perception::ConsensusTracking::async_
boost::shared_ptr< message_filters::Synchronizer< ApproximateSyncPolicy > > async_
Definition: consensus_tracking.h:140
jsk_perception::ConsensusTracking::~ConsensusTracking
virtual ~ConsensusTracking()
Definition: consensus_tracking.cpp:79
class_list_macros.h
jsk_perception
Definition: add_mask_image.h:48
cv_bridge::toCvCopy
CvImagePtr toCvCopy(const sensor_msgs::CompressedImage &source, const std::string &encoding=std::string())
jsk_perception::ConsensusTracking::pub_mask_image_
ros::Publisher pub_mask_image_
Definition: consensus_tracking.h:135
jsk_perception::ConsensusTracking::queue_size_
int queue_size_
Definition: consensus_tracking.h:149
math.h
_2
boost::arg< 2 > _2
lock
mutex_t * lock
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(jsk_perception::ConsensusTracking, nodelet::Nodelet)
jsk_perception::ConsensusTracking::window_initialized_
bool window_initialized_
Definition: consensus_tracking.h:147
message_filters::Subscriber::subscribe
void subscribe()
nodelet::Nodelet
jsk_perception::ConsensusTracking::cmt
CMT cmt
Definition: consensus_tracking.h:144
sensor_msgs::image_encodings::MONO8
const std::string MONO8
cv_bridge.h
jsk_perception::ConsensusTracking::approximate_sync_
bool approximate_sync_
Definition: consensus_tracking.h:148
cv_bridge::CvImage
jsk_perception::ConsensusTracking::sub_image_to_init_
message_filters::Subscriber< sensor_msgs::Image > sub_image_to_init_
Definition: consensus_tracking.h:141
sensor_msgs::image_encodings::BGR8
const std::string BGR8
jsk_perception::ConsensusTracking::sync_
boost::shared_ptr< message_filters::Synchronizer< ExactSyncPolicy > > sync_
Definition: consensus_tracking.h:139
jsk_perception::ConsensusTracking::getTrackingResult
virtual void getTrackingResult(const sensor_msgs::Image::ConstPtr &image_msg)
Definition: consensus_tracking.cpp:123
jsk_perception::ConsensusTracking::sub_polygon_to_init_
message_filters::Subscriber< geometry_msgs::PolygonStamped > sub_polygon_to_init_
Definition: consensus_tracking.h:142
jsk_perception::ConsensusTracking::subscribe
virtual void subscribe()
Definition: consensus_tracking.cpp:91
jsk_perception::ConsensusTracking::unsubscribe
virtual void unsubscribe()
Definition: consensus_tracking.cpp:97
ROS_INFO
#define ROS_INFO(...)
jsk_perception::ConsensusTracking::onInit
virtual void onInit()
Definition: consensus_tracking.cpp:50


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Fri May 16 2025 03:11:16