image_view2.h
Go to the documentation of this file.
1 // -*- mode: c++ -*-
2 /*********************************************************************
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2014, 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/o2r other materials provided
17  * with the distribution.
18  * * Neither the name of the Willow Garage 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 #ifndef IMAGE_VIEW2_H_
37 #define IMAGE_VIEW2_H_
38 
39 #include <opencv/cv.h>
40 #include <opencv2/highgui/highgui.hpp>
41 
42 #include <ros/ros.h>
43 #include <sensor_msgs/Image.h>
44 #include <cv_bridge/cv_bridge.h>
48 #include <tf/transform_listener.h>
49 #include <dynamic_reconfigure/server.h>
50 
51 #include <image_view2/ImageMarker2.h>
52 #include <geometry_msgs/PointStamped.h>
53 #include <geometry_msgs/PolygonStamped.h>
54 #include <std_msgs/Empty.h>
55 #include <std_srvs/Empty.h>
56 #include <image_view2/ChangeMode.h>
57 #include <boost/thread.hpp>
58 #include <boost/format.hpp>
59 #include <boost/foreach.hpp>
60 #include <boost/circular_buffer.hpp>
61 #include <boost/lambda/lambda.hpp>
62 #include <pcl/point_types.h>
63 #include <pcl_ros/publisher.h>
64 #include <image_view2/ImageView2Config.h>
65 
66 #include <image_view2/MouseEvent.h>
67 
68 #define DEFAULT_COLOR CV_RGB(255,0,0)
69 #define USER_ROI_COLOR CV_RGB(255,0,0)
70 #define DEFAULT_CIRCLE_SCALE 20
71 #define DEFAULT_LINE_WIDTH 3
72 
73 namespace image_view2
74 {
75  typedef std::vector<image_view2::ImageMarker2::ConstPtr> V_ImageMarkerMessage;
76  inline CvScalar MsgToRGB(const std_msgs::ColorRGBA &color){
77  if(color.a == 0.0 && color.r == 0.0 && color.g == 0.0 && color.b == 0.0)
78  return DEFAULT_COLOR;
79  else
80  return CV_RGB(color.r*255, color.g*255, color.b*255);
81  }
82 
83  class ImageView2
84  {
85  public:
86  typedef ImageView2Config Config;
87  enum KEY_MODE {
95  };
96 
97  ImageView2();
99  ~ImageView2();
100  void pressKey(int key);
101  void markerCb(const image_view2::ImageMarker2ConstPtr& marker);
102  void infoCb(const sensor_msgs::CameraInfoConstPtr& msg);
103  void redraw();
104  void imageCb(const sensor_msgs::ImageConstPtr& msg);
105  void drawImage();
106  void addPoint(int x, int y);
107  void addRegionPoint(int x, int y);
108  void updateRegionWindowSize(int x, int y);
109  void setRegionWindowPoint(int x, int y);
110  void clearPointArray();
111  void publishPointArray();
112  void setMode(KEY_MODE mode);
113  KEY_MODE getMode();
114  void showImage();
115  static void mouseCb(int event, int x, int y, int flags, void* param);
116  bool isValidMovement(const cv::Point2f& start_point,
117  const cv::Point2f& end_point);
118  bool toggleSelection();
121  protected:
122  private:
123  void config_callback(Config &config, uint32_t level);
124  void eventCb(
125  const image_view2::MouseEvent::ConstPtr& event_msg);
126  void pointArrayToMask(std::vector<cv::Point2d>& points,
127  cv::Mat& mask);
129  cv::Mat& image,
130  const std_msgs::Header& header);
132  cv::Mat& image,
133  const std_msgs::Header& header);
135  // drawing helper methods
137  void drawLineStrip(const image_view2::ImageMarker2::ConstPtr& marker,
138  std::vector<CvScalar>& colors,
139  std::vector<CvScalar>::iterator& col_it);
140  void drawLineList(const image_view2::ImageMarker2::ConstPtr& marker,
141  std::vector<CvScalar>& colors,
142  std::vector<CvScalar>::iterator& col_it);
143  void drawPolygon(const image_view2::ImageMarker2::ConstPtr& marker,
144  std::vector<CvScalar>& colors,
145  std::vector<CvScalar>::iterator& col_it);
146  void drawPoints(const image_view2::ImageMarker2::ConstPtr& marker,
147  std::vector<CvScalar>& colors,
148  std::vector<CvScalar>::iterator& col_it);
149  void drawFrames(const image_view2::ImageMarker2::ConstPtr& marker,
150  std::vector<CvScalar>& colors,
151  std::vector<CvScalar>::iterator& col_it);
152  void drawText(const image_view2::ImageMarker2::ConstPtr& marker,
153  std::vector<CvScalar>& colors,
154  std::vector<CvScalar>::iterator& col_it);
155  void drawLineStrip3D(const image_view2::ImageMarker2::ConstPtr& marker,
156  std::vector<CvScalar>& colors,
157  std::vector<CvScalar>::iterator& col_it);
158  void drawLineList3D(const image_view2::ImageMarker2::ConstPtr& marker,
159  std::vector<CvScalar>& colors,
160  std::vector<CvScalar>::iterator& col_it);
161  void drawPolygon3D(const image_view2::ImageMarker2::ConstPtr& marker,
162  std::vector<CvScalar>& colors,
163  std::vector<CvScalar>::iterator& col_it);
164  void drawPoints3D(const image_view2::ImageMarker2::ConstPtr& marker,
165  std::vector<CvScalar>& colors,
166  std::vector<CvScalar>::iterator& col_it);
167  void drawText3D(const image_view2::ImageMarker2::ConstPtr& marker,
168  std::vector<CvScalar>& colors,
169  std::vector<CvScalar>::iterator& col_it);
170  void drawCircle3D(const image_view2::ImageMarker2::ConstPtr& marker,
171  std::vector<CvScalar>& colors,
172  std::vector<CvScalar>::iterator& col_it);
173  void drawCircle(const image_view2::ImageMarker2::ConstPtr& marker);
174  void drawMarkers();
175  void drawInteraction();
176  void drawGrid();
177  void cropROI();
178  void drawInfo(ros::Time& before_rendering);
181  std::string frame_id, ros::Time& acquisition_time,
182  std::map<std::string, int>& tf_fail,
183  tf::StampedTransform &transform);
184  void processMouseEvent(int event, int x, int y, int flags, void* param);
185  void processLeftButtonDown(int x, int y);
186  void processMove(int x, int y);
187  void processLeftButtonUp(int x, int y);
189  void checkMousePos(int& x, int& y);
190  void createDistortGridImage();
191  V_ImageMarkerMessage local_queue_;
196  std::string marker_topic_;
197  boost::circular_buffer<double> times_;
201 
202  V_ImageMarkerMessage marker_queue_;
203  boost::mutex queue_mutex_;
204  boost::mutex point_array_mutex_;
205  sensor_msgs::ImageConstPtr last_msg_;
206  sensor_msgs::CameraInfoConstPtr info_msg_;
208  boost::mutex image_mutex_;
217 
220  std::vector<std::string> frame_ids_;
221  std::vector<cv::Point2d> point_array_;
222  boost::mutex info_mutex_;
223 
224  // for grabcut selection
226  std::vector<cv::Point2d> point_bg_array_;
227  std::vector<cv::Point2d> point_fg_array_;
228  std::vector<cv::Point2d> poly_points_;
231  cv::Rect rect_bg_;
232  cv::Rect rect_fg_;
233  std::string window_name_;
234  boost::format filename_format_;
235  int font_;
236 
239  cv::Point2f button_up_pos_;
240  int count_;
244  double tf_timeout_;
260  bool autosize_;
262 
263  // for line mode interaction
264  boost::mutex poly_point_mutex_;
265  boost::mutex line_point_mutex_;
268  cv::Point line_start_point_;
269  cv::Point line_end_point_;
270  // thread safe setter
271  void updateLineStartPoint(cv::Point p);
272  void updateLineEndPoint(cv::Point p);
273  cv::Point getLineStartPoint();
274  cv::Point getLineEndPoint();
275  void publishLinePoints();
276  void updateLinePoint(cv::Point p);
277  void updatePolyPoint(cv::Point p);
278  void updatePolySelectingPoint(cv::Point p);
279  void clearPolyPoints();
280  void publishPolyPoints();
281  void finishSelectingPoly();
284  void resetInteraction();
294  image_view2::ChangeModeRequest& req,
295  image_view2::ChangeModeResponse& res);
297  std_srvs::EmptyRequest& req,
298  std_srvs::EmptyResponse& res);
300  std_srvs::EmptyRequest& req,
301  std_srvs::EmptyResponse& res);
303  std_srvs::EmptyRequest& req,
304  std_srvs::EmptyResponse& res);
306  std_srvs::EmptyRequest& req,
307  std_srvs::EmptyResponse& res);
309  std_srvs::EmptyRequest& req,
310  std_srvs::EmptyResponse& res);
312  std_srvs::EmptyRequest& req,
313  std_srvs::EmptyResponse& res);
315  std_srvs::EmptyRequest& req,
316  std_srvs::EmptyResponse& res);
317  cv::Point ratioPoint(double x, double y);
318  KEY_MODE stringToMode(const std::string& str);
319  };
320 }
321 
322 #endif
void config_callback(Config &config, uint32_t level)
ros::Publisher point_pub_
Definition: image_view2.h:248
image_transport::Subscriber image_sub_
Definition: image_view2.h:192
std::vector< std::string > frame_ids_
Definition: image_view2.h:220
boost::mutex image_mutex_
Definition: image_view2.h:208
void setMode(KEY_MODE mode)
ros::Subscriber info_sub_
Definition: image_view2.h:194
#define DEFAULT_COLOR
Definition: image_view2.h:68
cv::Point2f button_up_pos_
Definition: image_view2.h:239
void updateLineEndPoint(cv::Point p)
void publishMonoImage(ros::Publisher &pub, cv::Mat &image, const std_msgs::Header &header)
ros::ServiceServer grabcut_mode_srv_
Definition: image_view2.h:287
void updatePolyPoint(cv::Point p)
ros::ServiceServer rectangle_mode_srv_
Definition: image_view2.h:285
void updateLinePoint(cv::Point p)
ros::Publisher rectangle_img_pub_
Definition: image_view2.h:251
ros::Publisher poly_pub_
Definition: image_view2.h:258
void addPoint(int x, int y)
bool noneModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
cv::Point poly_selecting_point_
Definition: image_view2.h:229
std::vector< cv::Point2d > point_fg_array_
Definition: image_view2.h:227
void drawPolygon(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
cv_bridge::CvImage img_bridge_
Definition: image_view2.h:207
ros::Publisher background_rect_pub_
Definition: image_view2.h:256
sensor_msgs::CameraInfoConstPtr info_msg_
Definition: image_view2.h:206
boost::circular_buffer< double > times_
Definition: image_view2.h:197
ros::ServiceServer line_mode_srv_
Definition: image_view2.h:289
void drawText3D(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
std::string marker_topic_
Definition: image_view2.h:196
ros::ServiceServer none_mode_srv_
Definition: image_view2.h:290
bool seriesModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
void drawFrames(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
void setRegionWindowPoint(int x, int y)
void drawLineStrip(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
ros::ServiceServer series_mode_srv_
Definition: image_view2.h:286
void drawLineList(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
ros::ServiceServer change_mode_srv_
Definition: image_view2.h:292
void drawCircle(const image_view2::ImageMarker2::ConstPtr &marker)
void updateRegionWindowSize(int x, int y)
void publishRectFromMaskImage(ros::Publisher &pub, cv::Mat &image, const std_msgs::Header &header)
bool rectangleModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
KEY_MODE stringToMode(const std::string &str)
bool polyModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
void infoCb(const sensor_msgs::CameraInfoConstPtr &msg)
void drawPoints(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
V_ImageMarkerMessage marker_queue_
Definition: image_view2.h:202
void addRegionPoint(int x, int y)
boost::mutex point_array_mutex_
Definition: image_view2.h:204
static void mouseCb(int event, int x, int y, int flags, void *param)
std::vector< cv::Point2d > poly_points_
Definition: image_view2.h:228
boost::mutex line_point_mutex_
Definition: image_view2.h:265
ros::Publisher rectangle_pub_
Definition: image_view2.h:250
void drawPoints3D(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
std::vector< image_view2::ImageMarker2::ConstPtr > V_ImageMarkerMessage
Definition: image_view2.h:75
void drawText(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
void markerCb(const image_view2::ImageMarker2ConstPtr &marker)
void drawLineList3D(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
bool lineModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
boost::mutex queue_mutex_
Definition: image_view2.h:203
void processLeftButtonDown(int x, int y)
ros::Publisher background_mask_pub_
Definition: image_view2.h:254
ros::Publisher foreground_mask_pub_
Definition: image_view2.h:253
std::vector< cv::Point2d > point_array_
Definition: image_view2.h:221
sensor_msgs::ImageConstPtr last_msg_
Definition: image_view2.h:205
void drawLineStrip3D(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
void drawInfo(ros::Time &before_rendering)
image_transport::Publisher local_image_pub_
Definition: image_view2.h:199
void processLeftButtonUp(int x, int y)
void imageCb(const sensor_msgs::ImageConstPtr &msg)
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
Definition: image_view2.h:200
boost::mutex poly_point_mutex_
Definition: image_view2.h:264
bool changeModeServiceCallback(image_view2::ChangeModeRequest &req, image_view2::ChangeModeResponse &res)
void eventCb(const image_view2::MouseEvent::ConstPtr &event_msg)
boost::mutex info_mutex_
Definition: image_view2.h:222
void updatePolySelectingPoint(cv::Point p)
image_transport::Publisher image_pub_
Definition: image_view2.h:198
boost::format filename_format_
Definition: image_view2.h:234
ros::ServiceServer poly_mode_srv_
Definition: image_view2.h:291
image_geometry::PinholeCameraModel cam_model_
Definition: image_view2.h:219
void drawPolygon3D(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
bool isValidMovement(const cv::Point2f &start_point, const cv::Point2f &end_point)
V_ImageMarkerMessage local_queue_
Definition: image_view2.h:191
ros::Publisher move_point_pub_
Definition: image_view2.h:252
ImageView2Config Config
Definition: image_view2.h:86
bool grabcutRectModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
ros::Publisher line_pub_
Definition: image_view2.h:257
bool lookupTransformation(std::string frame_id, ros::Time &acquisition_time, std::map< std::string, int > &tf_fail, tf::StampedTransform &transform)
tf::TransformListener tf_listener_
Definition: image_view2.h:218
void pointArrayToMask(std::vector< cv::Point2d > &points, cv::Mat &mask)
void checkMousePos(int &x, int &y)
bool grabcutModeServiceCallback(std_srvs::EmptyRequest &req, std_srvs::EmptyResponse &res)
void updateLineStartPoint(cv::Point p)
void processMouseEvent(int event, int x, int y, int flags, void *param)
ros::ServiceServer grabcut_rect_mode_srv_
Definition: image_view2.h:288
void processMove(int x, int y)
CvScalar MsgToRGB(const std_msgs::ColorRGBA &color)
Definition: image_view2.h:76
std::vector< cv::Point2d > point_bg_array_
Definition: image_view2.h:226
ros::Subscriber event_sub_
Definition: image_view2.h:193
ros::Publisher foreground_rect_pub_
Definition: image_view2.h:255
ros::Subscriber marker_sub_
Definition: image_view2.h:195
std::string window_name_
Definition: image_view2.h:233
cv::Point ratioPoint(double x, double y)
void drawCircle3D(const image_view2::ImageMarker2::ConstPtr &marker, std::vector< CvScalar > &colors, std::vector< CvScalar >::iterator &col_it)
ros::Publisher point_array_pub_
Definition: image_view2.h:249


image_view2
Author(s): Kei Okada
autogenerated on Tue Feb 6 2018 03:45:03