NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Vision.hpp
1 //Vision.hpp
2 // Created on: Apr 20, 2015
3 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
32 #pragma once
33 
34 #include <ros/ros.h>
35 #include <std_msgs/String.h>
36 #include <ros/console.h>
37 #include <opencv2/opencv.hpp>
38 #include <opencv2/highgui/highgui.hpp>
39 #include <sstream>
40 #include <boost/timer/timer.hpp>
41 #include <inttypes.h>
42 #include <time.h>
43 #include <std_msgs/Int64.h>
44 #include <string.h>
45 #include <image_transport/image_transport.h>
46 #include <image_geometry/pinhole_camera_model.h>
47 #include <tf/transform_listener.h>
48 #include <sensor_msgs/image_encodings.h>
49 #include <sensor_msgs/distortion_models.h>
50 #include <sys/ioctl.h>
51 #include <linux/videodev2.h>
52 #include <stdio.h>
53 #include <fcntl.h>
54 #include <unistd.h>
55 #include <cv_bridge/cv_bridge.h>
56 #include <sensor_msgs/image_encodings.h>
57 #include <config_server/parameter.h>
58 #include <std_srvs/Empty.h>
59 #include <boost/format.hpp>
60 #include <visualization_msgs/Marker.h>
61 #include <cmath>
62 #include <math.h>
63 #include <algorithm> // std::sort
64 #include <vis_utils/marker_manager.h>
65 #include <nav_msgs/OccupancyGrid.h>
66 #include <vision_module/Inputs/CameraDummy.hpp>
67 #include <vision_module/Tools/General.hpp>
68 #include <vision_module/Tools/Parameters.hpp>
69 #include <boost/foreach.hpp>
70 #include <vision_module/Inputs/Camera.hpp>
71 #include <vision_module/Projections/CameraProjections.hpp>
72 #include <vision_module/Tools/MatPublisher.hpp>
73 #include <vision_module/Projections/IPM.hpp>
74 #include <vision_module/Tools/LineSegment.hpp>
75 #include <vision_module/vision_outputs.h>
76 #include <vision_module/Tools/HSVPresenter.hpp>
77 #include <vision_module/Inputs/ICamera.hpp>
78 #include <vector>
79 #include <robotcontrol/RobotHeading.h>
80 #include <plot_msgs/plot_manager.h>
81 #include <vision_module/SoccerObjects/BallDetector.hpp>
82 #include <vision_module/SoccerObjects/CircleDetector.hpp>
83 #include <vision_module/SoccerObjects/FieldDetector.hpp>
84 #include <vision_module/SoccerObjects/GoalDetector.hpp>
85 #include <vision_module/SoccerObjects/LineDetector.hpp>
86 #include <vision_module/SoccerObjects/ObstacleDetector.hpp>
87 #include <vision_module/Localization/Localization.hpp>
88 #include <vision_module/vision_outputs.h>
89 #include <vision_module/localization_output.h>
90 #include <numeric>
91 #include <vision_module/Tools/GuiManager.hpp>
92 
98 class Vision
99 {
100 public:
101  CameraProjections ProjectionObj;
102  Mat RawHSVImg;
103  Mat GrayImg;
104 private:
105  bool destroyed;
106  long int visionCounter;
107  bool updateGuiImg;
108  ICamera *cam;
109  Mat guiTopViwRotate, guiImg, guiUndistorted;
110  Mat ballBinary, fieldBinary, fieldConvectHull, goalBinary, cannyImgInField,
111  obstacleBinary;
112  ros::NodeHandle nodeHandle;
113  vis_utils::MarkerManager egoDetectionMarker;
114  vis_utils::MarkerManager localizationMarker;
115  vis_utils::GenMarker egoLinesM;
116  vis_utils::GenMarker egoFieldM;
117  vis_utils::GenMarker egoGoalPostLM;
118  vis_utils::GenMarker egoGoalPostRM;
119  vis_utils::GenMarker egoCircleM;
120  vis_utils::SphereMarker egoBallM;
121  vis_utils::GenMarker locPhiM;
122  vis_utils::SphereMarker locM;
123  MatPublisher edgeImg_pub;
124  MatPublisher guiImg_pub;
125  MatPublisher webImg_pub;
126  Point3d head_Control_Pos;
127  FieldDetector fieldDetector;
128  BallDetector ballDetector;
129  LineDetector lineDetector;
130  CircleDetector circleDetector;
131  GoalDetector goalDetector;
132  ObstacleDetector obstacleDetector;
133  vector<ObstacleC> obstacles;
134  Localization loc;
135  HSVPresenter hsvPresenter;
136  GuiManager guiManager;
137  vector<cv::Point2f> fieldHullReal;
138  vector<cv::Point2f> fieldHullRealRotated;
139  cv::Point2f fieldHullRealCenter;
140  ros::Publisher visionOutputs_pub;
141 
142  enum PMIds
143  {
144  PM_FRAME_RATE = 0,
145  PM_HEADING_OFFSET,
146  PM_LAST_AVALIBLE_TF,
147  PM_OBSTACLE_TIME,
148  PM_LINE_TIME,
149  PM_BALL_TIME,
150  PM_LOCALIZATION_TIME,
151  PM_GOAL_TIME,
152  PM_CIRCLE_TIME,
153  PM_COUNT
154  };
155  plot_msgs::PlotManagerFS plotM;
156  string rName;
157  vision_module::vision_outputs outputs;
158 public:
159  Vision(bool dummy) :
160  ProjectionObj(), destroyed(false), visionCounter(0), updateGuiImg(
161  false), egoDetectionMarker("/vision/egoDetectionMarker"), localizationMarker(
162  "/vision/localization"), egoLinesM(&egoDetectionMarker,
163  "/ego_floor", "lines"), egoFieldM(&egoDetectionMarker,
164  "/ego_floor", "field"), egoGoalPostLM(&egoDetectionMarker,
165  "/ego_floor", "goals"), egoGoalPostRM(&egoDetectionMarker,
166  "/ego_floor", "goals"), egoCircleM(&egoDetectionMarker,
167  "/ego_floor", "circle"), egoBallM(&egoDetectionMarker,
168  "/ego_floor", "ball"), locPhiM(&localizationMarker,
169  "/ego_floor"), locM(&localizationMarker, "/ego_floor"), edgeImg_pub(
170  "/vision/edgeImg"), guiImg_pub("/vision/guiImg"), webImg_pub(
171  "/vision/webImg"), fieldDetector(), ballDetector(), lineDetector(), circleDetector(), goalDetector(), obstacleDetector(), loc(
172  &obstacles), hsvPresenter(), guiManager(&ProjectionObj), plotM(
173  PM_COUNT, "/VisionModule")
174  {
175 
176  char const* robotName;
177  robotName = getenv("VIS_HOSTNAME");
178  rName = "";
179  if (robotName != NULL)
180  {
181  std::size_t found = string(robotName).find("xs");
182  if (found != std::string::npos)
183  {
184  rName = robotName;
185  }
186  else
187  {
188  rName = "unknown";
189  }
190  ROS_INFO("Robot Name = %s", rName.c_str());
191  }
192 
193  if (dummy == true)
194  {
195  cam = new CameraDummy();
196  }
197  else
198  {
199  cam = new Camera();
200  }
201 
202  visionOutputs_pub = nodeHandle.advertise<vision_module::vision_outputs>(
203  "/vision/outputs", 10);
204 
205 
206  plotM.setName(PM_FRAME_RATE, "FPS");
207  plotM.setName(PM_HEADING_OFFSET, "headingOffset");
208  plotM.setName(PM_OBSTACLE_TIME, "obst_time");
209  plotM.setName(PM_LINE_TIME, "line_time");
210  plotM.setName(PM_BALL_TIME, "ball_time");
211  plotM.setName(PM_GOAL_TIME, "goal_time");
212  plotM.setName(PM_CIRCLE_TIME, "circle_time");
213  plotM.setName(PM_LOCALIZATION_TIME, "loc_time");
214  plotM.setName(PM_LAST_AVALIBLE_TF, "lastAvalibleTF");
215  if (!plotM.checkNames())
216  {
217  ROS_WARN("Check checkNames function for plotM!");
218  }
219  }
220 
221  void plot_fps(double _in)
222  {
223  plotM.plotScalar(_in, PM_FRAME_RATE);
224  }
225 
226  bool GenerateTopViewImg(ros::Time now);
227  void UpdateRangeImage(ros::Time now);
228  void Process(ros::Time capTime);
229  virtual ~Vision()
230  {
231  DeInit();
232  }
233  bool update();
234  bool Init();
235  void DeInit()
236  {
237  if (!destroyed)
238  {
239  cam->DeInitCameraDevice();
240  delete cam;
241  destroyed = true;
242  }
243  }
244 
245 };
246 
For detecting field boundary.
Definition: FieldDetector.hpp:19
For detecting at least 50% white ball.
Definition: BallDetector.hpp:46
This class is for managing all the required actions to find interesting objects in soccer field...
Definition: Vision.hpp:98
For pixel projections.
Definition: CameraProjections.hpp:50
A class for publish mat objects.
Definition: MatPublisher.hpp:40
For localization in the soccer field.
Definition: Localization.hpp:95
For detecting field lines.
Definition: LineDetector.hpp:22
For detecting center circle.
Definition: CircleDetector.hpp:20
For detecting other robots with black feet.
Definition: ObstacleDetector.hpp:65
For detecting potential goal posts.
Definition: GoalDetector.hpp:20
This class is responsible for imitating image acquisition process as a dummy replacement for Camera C...
Definition: CameraDummy.hpp:25
This class is responsible for image acquisition process As an output this class provides the raw im...
Definition: Camera.hpp:25
A container class for managing gui events.
Definition: GuiManager.hpp:117
A class for showing HSV color range.
Definition: HSVPresenter.hpp:15