calc_flow.cpp
Go to the documentation of this file.
1 //
2 // JSK calc_flow
3 //
4 #include <ros/ros.h>
5 #include <ros/names.h>
7 #include <sensor_msgs/Image.h>
8 #include <sensor_msgs/CameraInfo.h>
12 
13 #include <cv_bridge/cv_bridge.h>
14 #if ( CV_MAJOR_VERSION >= 4)
15 #include <opencv2/opencv.hpp>
16 #else
17 #include <cv.hpp>
18 #endif
19 
20 #include <stdlib.h>
21 
23 private:
28  std::string namespace_;
29  cv::Mat prevImg;
30  cv::Mat flow;
31  //cv::Mat *nextImg;
32 public:
33  calc_flow_node () : nh_(), it_(nh_), flow(0, 0, CV_8UC1), prevImg(0, 0, CV_8UC1) {
34  //flow = new cv::Mat(0, 0, CV_8UC1);
35 
36  namespace_ = nh_.resolveName("camera");
37  result_pub_ = nh_.advertise<sensor_msgs::Image> ("flow_image", 1);
38  sub_ = it_.subscribe(namespace_ + "/image", 10, &calc_flow_node::imageCB, this);
39  }
40 
41  void imageCB(const sensor_msgs::ImageConstPtr &img) {
42  //IplImage *ipl_ = new IplImage();
43  //ipl_ = cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
44  //sensor_msgs::CvBridge bridge;
45  cv_bridge::CvImagePtr cv_ptr;
46  //cvResize(bridge.imgMsgToCv(img, "mono8"), ipl_);
47  //IplImage *ipl_ = bridge.imgMsgToCv(img, "mono8");
48  //cv_ptr = cv_bridge::toCvCopy(img, "mono8");
49  cv_ptr = cv_bridge::toCvCopy(img, "mono8");
50  bool prevImg_update_required = false;
51  if((flow.cols != (int)img->width) ||
52  (flow.rows != (int)img->height)) {
53  ROS_INFO("make flow");
54  cv_ptr->image.copyTo(flow);
55  prevImg_update_required = true;
56  }
57  if(prevImg_update_required) {
58  cv_ptr->image.copyTo(prevImg);
59  prevImg_update_required = false;
60  ROS_INFO("return");
61  return;
62  }
63  //
64  //ROS_INFO("subscribe image");
65  //prevImg.
66  //cv::Mat *nextImg = new cv::Mat(ipl_);
67  cv::Mat nextImg(img->height, img->width, CV_8UC1);
68  //memcpy(nextImg->data, ipl_->imageData, img->height*img->width);
69  cv_ptr->image.copyTo(nextImg);
70 
71  cv::calcOpticalFlowFarneback(prevImg, nextImg, flow,
72  0.5, 3, 15, 3, 5, 1.2, 0 );
73  // 0.5, 2,
74  // 16, 4,
75  // 5, 1.1, 0);
76  //cv::OPTFLOW_USE_INITIAL_FLOW);
77  nextImg.copyTo(prevImg);
78 
79  sensor_msgs::Image result;
80  result.header = img->header;
81  result.width = flow.cols;
82  result.height = flow.rows;
83  result.encoding = "mono8";
84  result.step = flow.cols;
85  result.data.resize(flow.cols * flow.rows);
86  CvPoint2D32f *ptr = (CvPoint2D32f *)flow.data;
87  for(int i = 0; i<result.data.size(); i++) {
88  // copy flow -> result
89  //result.data[i] = ;
90  int val = 10 * sqrt(ptr[i].x * ptr[i].x + ptr[i].y * ptr[i].y);
91  result.data[i] = 255>val?val:255;
92  }
93  result_pub_.publish(result);
94  }
95 };
96 
97 int main(int argc, char **argv)
98 {
99  ros::init(argc, argv, "calc_flow");
100  //cv::namedWindow(std::string("window"), );
101  calc_flow_node flow_node;
102 
103  ros::spin();
104  return 0;
105 }
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())
cv::Mat flow
Definition: calc_flow.cpp:30
void publish(const boost::shared_ptr< M > &message) const
std::string resolveName(const std::string &name, bool remap=true) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int main(int argc, char **argv)
Definition: calc_flow.cpp:97
image_transport::Subscriber sub_
Definition: calc_flow.cpp:26
ROSCPP_DECL void spin(Spinner &spinner)
image_transport::ImageTransport it_
Definition: calc_flow.cpp:25
ros::NodeHandle nh_
Definition: calc_flow.cpp:24
result
void imageCB(const sensor_msgs::ImageConstPtr &img)
Definition: calc_flow.cpp:41
double sqrt()
#define ROS_INFO(...)
std::string namespace_
Definition: calc_flow.cpp:28
CvImagePtr toCvCopy(const sensor_msgs::ImageConstPtr &source, const std::string &encoding=std::string())
x
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
y
ros::Publisher result_pub_
Definition: calc_flow.cpp:27
cv::Mat prevImg
Definition: calc_flow.cpp:29


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27