fback_flow_nodelet.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, Kei Okada.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Kei Okada nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 // https://github.com/Itseez/opencv/blob/2.4/samples/cpp/fback.cpp
36 /*
37  * This program demonstrates dense optical flow algorithm by Gunnar Farneback
38  * Mainly the function: calcOpticalFlowFarneback()
39  */
40 
41 #include <ros/ros.h>
42 #include "opencv_apps/nodelet.h"
44 #include <cv_bridge/cv_bridge.h>
46 
47 #include <opencv2/highgui/highgui.hpp>
48 #include <opencv2/imgproc/imgproc.hpp>
49 #include <opencv2/video/tracking.hpp>
50 
51 #include <dynamic_reconfigure/server.h>
52 #include "opencv_apps/FBackFlowConfig.h"
53 #include "opencv_apps/FlowArrayStamped.h"
54 
55 namespace opencv_apps
56 {
57 class FBackFlowNodelet : public opencv_apps::Nodelet
58 {
63 
65 
66  typedef opencv_apps::FBackFlowConfig Config;
67  typedef dynamic_reconfigure::Server<Config> ReconfigureServer;
70 
71  int queue_size_;
72  bool debug_view_;
74 
75  std::string window_name_;
76  static bool need_config_update_;
77 
78  cv::Mat prevgray, gray, flow, cflow;
79 
80  void reconfigureCallback(Config& new_config, uint32_t level)
81  {
82  config_ = new_config;
83  }
84 
85  const std::string& frameWithDefault(const std::string& frame, const std::string& image_frame)
86  {
87  if (frame.empty())
88  return image_frame;
89  return frame;
90  }
91 
92  void imageCallbackWithInfo(const sensor_msgs::ImageConstPtr& msg, const sensor_msgs::CameraInfoConstPtr& cam_info)
93  {
94  doWork(msg, cam_info->header.frame_id);
95  }
96 
97  void imageCallback(const sensor_msgs::ImageConstPtr& msg)
98  {
99  doWork(msg, msg->header.frame_id);
100  }
101 
102  static void trackbarCallback(int /*unused*/, void* /*unused*/)
103  {
104  need_config_update_ = true;
105  }
106 
107  void doWork(const sensor_msgs::ImageConstPtr& msg, const std::string& input_frame_from_msg)
108  {
109  // Work on the image.
110  try
111  {
112  // Convert the image into something opencv can handle.
113  cv::Mat frame = cv_bridge::toCvShare(msg, sensor_msgs::image_encodings::BGR8)->image;
114 
115  // Messages
116  opencv_apps::FlowArrayStamped flows_msg;
117  flows_msg.header = msg->header;
118 
119  if (debug_view_)
120  {
121  cv::namedWindow(window_name_, cv::WINDOW_AUTOSIZE);
123  {
124  reconfigure_server_->updateConfig(config_);
126  }
127  }
128 
129  // Check if clock is jumped back
130  if (ros::Time::isSimTime() && prev_stamp_ > msg->header.stamp)
131  {
132  NODELET_WARN_STREAM("Detected jump back in time of " << msg->header.stamp << ". Clearing optical flow cache.");
133  prevgray = cv::Mat();
134  }
135 
136  // Do the work
137  if (frame.channels() > 1)
138  {
139  cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
140  }
141  else
142  {
143  frame.copyTo(gray);
144  }
145  if (prevgray.data)
146  {
147  cv::calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
148  cv::cvtColor(prevgray, cflow, cv::COLOR_GRAY2BGR);
149  // drawOptFlowMap(flow, cflow, 16, 1.5, Scalar(0, 255, 0));
150  int step = 16;
151  cv::Scalar color = cv::Scalar(0, 255, 0);
152  for (int y = 0; y < cflow.rows; y += step)
153  for (int x = 0; x < cflow.cols; x += step)
154  {
155  const cv::Point2f& fxy = flow.at<cv::Point2f>(y, x);
156  cv::line(cflow, cv::Point(x, y), cv::Point(cvRound(x + fxy.x), cvRound(y + fxy.y)), color);
157  cv::circle(cflow, cv::Point(x, y), 2, color, -1);
158 
159  opencv_apps::Flow flow_msg;
160  opencv_apps::Point2D point_msg;
161  opencv_apps::Point2D velocity_msg;
162  point_msg.x = x;
163  point_msg.y = y;
164  velocity_msg.x = fxy.x;
165  velocity_msg.y = fxy.y;
166  flow_msg.point = point_msg;
167  flow_msg.velocity = velocity_msg;
168  flows_msg.status.push_back(true);
169  flows_msg.flow.push_back(flow_msg);
170  }
171  }
172 
173  std::swap(prevgray, gray);
174 
175  //-- Show what you got
176  if (debug_view_)
177  {
178  cv::imshow(window_name_, cflow);
179  int c = cv::waitKey(1);
180  }
181 
182  // Publish the image.
183  sensor_msgs::Image::Ptr out_img = cv_bridge::CvImage(msg->header, "bgr8", cflow).toImageMsg();
184  img_pub_.publish(out_img);
185  msg_pub_.publish(flows_msg);
186  }
187  catch (cv::Exception& e)
188  {
189  NODELET_ERROR("Image processing error: %s %s %s %i", e.err.c_str(), e.func.c_str(), e.file.c_str(), e.line);
190  }
191 
192  prev_stamp_ = msg->header.stamp;
193  }
194 
195  void subscribe() // NOLINT(modernize-use-override)
196  {
197  NODELET_DEBUG("Subscribing to image topic.");
198  if (config_.use_camera_info)
199  cam_sub_ = it_->subscribeCamera("image", queue_size_, &FBackFlowNodelet::imageCallbackWithInfo, this);
200  else
201  img_sub_ = it_->subscribe("image", queue_size_, &FBackFlowNodelet::imageCallback, this);
202  }
203 
204  void unsubscribe() // NOLINT(modernize-use-override)
205  {
206  NODELET_DEBUG("Unsubscribing from image topic.");
207  img_sub_.shutdown();
208  cam_sub_.shutdown();
209  }
210 
211 public:
212  virtual void onInit() // NOLINT(modernize-use-override)
213  {
214  Nodelet::onInit();
216 
217  pnh_->param("queue_size", queue_size_, 3);
218  pnh_->param("debug_view", debug_view_, false);
219  if (debug_view_)
220  {
221  always_subscribe_ = true;
222  }
223  prev_stamp_ = ros::Time(0, 0);
224 
225  window_name_ = "flow";
226 
227  reconfigure_server_ = boost::make_shared<dynamic_reconfigure::Server<Config> >(*pnh_);
228  dynamic_reconfigure::Server<Config>::CallbackType f =
230  reconfigure_server_->setCallback(f);
231 
232  img_pub_ = advertiseImage(*pnh_, "image", 1);
233  msg_pub_ = advertise<opencv_apps::FlowArrayStamped>(*pnh_, "flows", 1);
234 
236  }
237 };
239 } // namespace opencv_apps
240 
241 namespace fback_flow
242 {
244 {
245 public:
246  virtual void onInit() // NOLINT(modernize-use-override)
247  {
248  ROS_WARN("DeprecationWarning: Nodelet fback_flow/fback_flow is deprecated, "
249  "and renamed to opencv_apps/fback_flow.");
251  }
252 };
253 } // namespace fback_flow
254 
255 #ifdef USE_PLUGINLIB_CLASS_LIST_MACROS_H
257 #else
259 #endif
opencv_apps::FBackFlowNodelet::it_
boost::shared_ptr< image_transport::ImageTransport > it_
Definition: fback_flow_nodelet.cpp:128
nodelet.h
NODELET_ERROR
#define NODELET_ERROR(...)
opencv_apps::Nodelet::onInitPostProcess
virtual void onInitPostProcess()
Post processing of initialization of nodelet. You need to call this method in order to use always_sub...
Definition: nodelet.cpp:77
ros::Publisher
image_encodings.h
image_transport::ImageTransport
ros::Time::isSimTime
static bool isSimTime()
boost::shared_ptr< image_transport::ImageTransport >
cv_bridge::CvImage::toImageMsg
sensor_msgs::ImagePtr toImageMsg() const
opencv_apps::FBackFlowNodelet::subscribe
void subscribe()
This method is called when publisher is subscribed by other nodes. Set up subscribers in this method.
Definition: fback_flow_nodelet.cpp:259
opencv_apps::FBackFlowNodelet::onInit
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method.
Definition: fback_flow_nodelet.cpp:276
ros.h
opencv_apps::FBackFlowNodelet::cam_sub_
image_transport::CameraSubscriber cam_sub_
Definition: fback_flow_nodelet.cpp:125
opencv_apps::FBackFlowNodelet::prev_stamp_
ros::Time prev_stamp_
Definition: fback_flow_nodelet.cpp:137
boost::placeholders::_1
boost::arg< 1 > _1
Definition: nodelet.cpp:44
opencv_apps::FBackFlowNodelet::config_
Config config_
Definition: fback_flow_nodelet.cpp:132
opencv_apps::Nodelet
Nodelet to automatically subscribe/unsubscribe topics according to subscription of advertised topics.
Definition: nodelet.h:90
opencv_apps::FBackFlowNodelet::reconfigure_server_
boost::shared_ptr< ReconfigureServer > reconfigure_server_
Definition: fback_flow_nodelet.cpp:133
NODELET_WARN_STREAM
#define NODELET_WARN_STREAM(...)
opencv_apps::FBackFlowNodelet::ReconfigureServer
dynamic_reconfigure::Server< Config > ReconfigureServer
Definition: fback_flow_nodelet.cpp:131
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
f
f
image_transport::Subscriber
opencv_apps::Nodelet::advertiseImage
image_transport::Publisher advertiseImage(ros::NodeHandle &nh, const std::string &topic, int queue_size)
Advertise an image topic and watch the publisher. Publishers which are created by this method....
Definition: nodelet.h:201
class_list_macros.h
opencv_apps::FBackFlowNodelet::window_name_
std::string window_name_
Definition: fback_flow_nodelet.cpp:139
opencv_apps::FBackFlowNodelet::debug_view_
bool debug_view_
Definition: fback_flow_nodelet.cpp:136
image_transport::CameraSubscriber
opencv_apps::FBackFlowNodelet::msg_pub_
ros::Publisher msg_pub_
Definition: fback_flow_nodelet.cpp:126
fback_flow
Definition: fback_flow_nodelet.cpp:241
image_transport::CameraSubscriber::shutdown
void shutdown()
opencv_apps::FBackFlowNodelet::queue_size_
int queue_size_
Definition: fback_flow_nodelet.cpp:135
opencv_apps::FBackFlowNodelet::trackbarCallback
static void trackbarCallback(int, void *)
Definition: fback_flow_nodelet.cpp:166
boost::placeholders::_2
boost::arg< 2 > _2
Definition: nodelet.cpp:45
opencv_apps::Nodelet::always_subscribe_
bool always_subscribe_
A flag to disable watching mechanism and always subscribe input topics. It can be specified via ~alwa...
Definition: nodelet.h:300
ROS_WARN
#define ROS_WARN(...)
fback_flow::FBackFlowNodelet::onInit
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method.
Definition: fback_flow_nodelet.cpp:246
PLUGINLIB_EXPORT_CLASS
PLUGINLIB_EXPORT_CLASS(opencv_apps::FBackFlowNodelet, nodelet::Nodelet)
image_transport::Publisher::publish
void publish(const sensor_msgs::Image &message) const
opencv_apps
Demo code to calculate moments.
Definition: nodelet.h:68
opencv_apps::FBackFlowNodelet::frameWithDefault
const std::string & frameWithDefault(const std::string &frame, const std::string &image_frame)
Definition: fback_flow_nodelet.cpp:149
opencv_apps::FBackFlowNodelet::doWork
void doWork(const sensor_msgs::ImageConstPtr &msg, const std::string &input_frame_from_msg)
Definition: fback_flow_nodelet.cpp:171
image_transport.h
opencv_apps::FBackFlowNodelet
Definition: fback_flow_nodelet.cpp:89
nodelet::Nodelet
ros::Time
fback_flow::FBackFlowNodelet
Definition: fback_flow_nodelet.cpp:243
image_transport::Publisher
opencv_apps::FBackFlowNodelet::imageCallbackWithInfo
void imageCallbackWithInfo(const sensor_msgs::ImageConstPtr &msg, const sensor_msgs::CameraInfoConstPtr &cam_info)
Definition: fback_flow_nodelet.cpp:156
cv_bridge.h
opencv_apps::FBackFlowNodelet::Config
opencv_apps::FBackFlowConfig Config
Definition: fback_flow_nodelet.cpp:130
cv_bridge::CvImage
opencv_apps::FBackFlowNodelet::img_sub_
image_transport::Subscriber img_sub_
Definition: fback_flow_nodelet.cpp:124
class_list_macros.hpp
opencv_apps::FBackFlowNodelet::cflow
cv::Mat cflow
Definition: fback_flow_nodelet.cpp:142
opencv_apps::FBackFlowNodelet::img_pub_
image_transport::Publisher img_pub_
Definition: fback_flow_nodelet.cpp:123
opencv_apps::FBackFlowNodelet::reconfigureCallback
void reconfigureCallback(Config &new_config, uint32_t level)
Definition: fback_flow_nodelet.cpp:144
opencv_apps::FBackFlowNodelet::unsubscribe
void unsubscribe()
This method is called when publisher is unsubscribed by other nodes. Shut down subscribers in this me...
Definition: fback_flow_nodelet.cpp:268
opencv_apps::FBackFlowNodelet::flow
cv::Mat flow
Definition: fback_flow_nodelet.cpp:142
opencv_apps::FBackFlowNodelet::imageCallback
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
Definition: fback_flow_nodelet.cpp:161
sensor_msgs::image_encodings::BGR8
const std::string BGR8
cv_bridge::toCvShare
CvImageConstPtr toCvShare(const sensor_msgs::Image &source, const boost::shared_ptr< void const > &tracked_object, const std::string &encoding=std::string())
opencv_apps::FBackFlowNodelet::gray
cv::Mat gray
Definition: fback_flow_nodelet.cpp:142
opencv_apps::Nodelet::nh_
boost::shared_ptr< ros::NodeHandle > nh_
Shared pointer to nodehandle.
Definition: nodelet.h:272
opencv_apps::FBackFlowNodelet::need_config_update_
static bool need_config_update_
Definition: fback_flow_nodelet.cpp:140
opencv_apps::FBackFlowNodelet::prevgray
cv::Mat prevgray
Definition: fback_flow_nodelet.cpp:142
opencv_apps::Nodelet::onInit
virtual void onInit()
Initialize nodehandles nh_ and pnh_. Subclass should call this method in its onInit method.
Definition: nodelet.cpp:60
opencv_apps::Nodelet::pnh_
boost::shared_ptr< ros::NodeHandle > pnh_
Shared pointer to private nodehandle.
Definition: nodelet.h:277
NODELET_DEBUG
#define NODELET_DEBUG(...)
image_transport::Subscriber::shutdown
void shutdown()


opencv_apps
Author(s): Kei Okada
autogenerated on Fri May 23 2025 02:49:49