NimbRo ROS Soccer Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
GuiManager.hpp
1 //GuiManager.hpp
2 // Created on: May 7, 2016
3 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
4 #pragma once
5 #include <ros/ros.h>
6 #include <stdio.h>
7 #include <stdarg.h>
8 #include <opencv2/opencv.hpp>
9 #include <boost/filesystem.hpp>
10 #include <vision_module/Tools/Parameters.hpp>
11 #include <vision_module/Tools/MatPublisher.hpp>
12 #include <vision_module/Projections/CameraProjections.hpp>
13 #include <rqt_vision_module/GuiEvent.h>
14 #include <rqt_vision_module/mouseevent.hpp>
15 #include <vision_module/Tools/LinearBoundaryChecker.hpp>
16 #include <rqt_vision_module/ConsoleMsg.h>
17 using namespace cv;
24 {
25 
26 public:
27  double length1;
28  double length2;
29  Point2d realLocation;
30  Point2d imgLocation;
32  {
33  reset();
34  }
35  bool isValid()
36  {
37  return ((length1 >= 0 && length2 >= 0) && GetDistance(realLocation) >= 0
38  && imgLocation.x >= 0 && imgLocation.x < IMGWIDTH
39  && imgLocation.y >= 0 && imgLocation.y < IMGHEIGHT);
40  }
41  void reset()
42  {
43  length1 = length2 = 0;
44  realLocation.x = realLocation.y = 0;
45  imgLocation.x = imgLocation.y = -1;
46  }
47 };
53 class GuiRetS
54 {
55 public:
56  Point2d p1;
57  Point2d p2;
58  bool hold;
59  GuiRetS()
60  {
61  reset();
62  }
63  bool isFinished()
64  {
65  return !hold && isValid();
66  }
67  bool isValid()
68  {
69  return p1.x >= 0 && p1.x < IMGWIDTH && p1.y >= 0 && p1.y < IMGHEIGHT
70  && p2.x >= 0 && p2.x < IMGWIDTH && p2.y >= 0 && p2.y < IMGHEIGHT
71  && abs(p1.x - p2.x) >= 1 && abs(p1.y && p2.y) >= 1;
72  }
73  void reset()
74  {
75  p1.x = p1.y = p2.x = p2.y = -1;
76  hold = false;
77  }
78  cv::Rect getRect()
79  {
80  Rect rec;
81  if (p1.y <= p2.y && p1.x <= p2.x)
82  {
83  rec.x = p1.x;
84  rec.y = p1.y;
85  rec.width = abs(p1.x - p2.x);
86  rec.height = abs(p1.y - p2.y);
87  }
88  else if (p1.y <= p2.y && p1.x > p2.x)
89  {
90  rec.x = p2.x;
91  rec.y = p1.y;
92  rec.width = abs(p1.x - p2.x);
93  rec.height = abs(p1.y - p2.y);
94  }
95  else if (p1.y > p2.y && p1.x <= p2.x)
96  {
97  rec.x = p1.x;
98  rec.y = p2.y;
99  rec.width = abs(p1.x - p2.x);
100  rec.height = abs(p1.y - p2.y);
101  }
102  else if (p1.y > p2.y && p1.x > p2.x)
103  {
104  rec.x = p2.x;
105  rec.y = p2.y;
106  rec.width = abs(p1.x - p2.x);
107  rec.height = abs(p1.y - p2.y);
108  }
109  return rec;
110  }
111 };
118 {
119 private:
120  DistanceSizeS distanceSizeSample;
121  Point mouseLastMovePos;
122  vector<Point> histogramContour;
123  GuiRetS rectSaver;
124  ros::Publisher vision_msg_pub;
125  CameraProjections *projection;
126  CascadeClassifier object_cascade;
127 public:
128  ros::NodeHandle nodeHandle;
129  ros::Subscriber gui_events_sub;
130  ros::Subscriber rviz_click_sub;
131 
132  GuiManager(CameraProjections *projection) :
133  mouseLastMovePos(0, 0), projection(projection)
134  {
135  vision_msg_pub = nodeHandle.advertise<rqt_vision_module::ConsoleMsg>(
136  "/rqt_vision_module/console", 10);
137  gui_events_sub = nodeHandle.subscribe<rqt_vision_module::GuiEvent>(
138  "/rqt_vision_module/gui_events", 30, &GuiManager::gui_events_callback,
139  this);
140  rviz_click_sub = nodeHandle.subscribe<geometry_msgs::PointStamped>(
141  "/clicked_point", 1, &GuiManager::rviz_click_callback, this);
142  }
143 
144  void writeGuiConsole(Scalar color, const char * format, ...)
145  {
146  char buffer[256];
147  va_list args;
148  va_start(args, format);
149  vsnprintf(buffer, 255, format, args);
150  ROS_INFO(" %s", buffer);
151  rqt_vision_module::ConsoleMsg msg;
152  msg.message = buffer;
153  msg.b = color.val[0];
154  msg.g = color.val[1];
155  msg.r = color.val[2];
156  msg.font_size=12;
157  vision_msg_pub.publish(msg);
158  va_end(args);
159  }
160 
161  void writeGuiConsoleFormat(Scalar color, const char * format, ...)
162  {
163  time_t t = time(0);
164  char bufferTime[9] =
165  { 0 };
166  strftime(bufferTime, 12, "%H:%M:%S | ", localtime(&t));
167 
168  char buffer[256];
169  va_list args;
170  va_start(args, format);
171  vsnprintf(buffer, 255, format, args);
172 
173  string txt=bufferTime;
174  txt+=buffer;
175  if (color == redColor())
176  {
177  ROS_ERROR(" %s", txt.c_str());
178  }
179  if (color == yellowColor())
180  {
181  ROS_WARN(" %s", txt.c_str());
182  }
183  else
184  {
185  ROS_INFO(" %s", txt.c_str());
186  }
187  rqt_vision_module::ConsoleMsg msg;
188  msg.message = txt;
189  msg.message += "\r\n";
190  msg.b = color.val[0];
191  msg.g = color.val[1];
192  msg.r = color.val[2];
193  msg.font_size=12;
194  vision_msg_pub.publish(msg);
195  va_end(args);
196  }
197 
198  inline bool Init()
199  {
200  return LoadCascade();
201  }
202  inline bool LoadCascade()
203  {
204  return object_cascade.load(params.configPath + "cascadeBall.xml");
205  }
206  void Publish(const Mat &gray, Mat &guiRawImg, bool shouldPublish);
207  void Update(const Mat &rawHSV, const Mat & img);
208  void rviz_click_callback(const geometry_msgs::PointStampedConstPtr& msg);
209  void gui_events_callback(const rqt_vision_module::GuiEvent::ConstPtr & msg);
210  inline ~GuiManager()
211  {
212  }
213 
214 };
215 
A container class for gui rectangles.
Definition: GuiManager.hpp:53
For pixel projections.
Definition: CameraProjections.hpp:50
A container class for distance-size calibration.
Definition: GuiManager.hpp:23
A container class for managing gui events.
Definition: GuiManager.hpp:117