NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ObstacleDetector.hpp
1 //ObstacleDetector.hpp
2 // Created on: May 14, 2015
3 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
4 #pragma once
5 #include <opencv2/opencv.hpp>
6 #include <math.h>
7 #include <vision_module/Tools/Parameters.hpp>
8 #include <vision_module/Tools/General.hpp>
9 #include <vision_module/Projections/CameraProjections.hpp>
10 #include <vision_module/SoccerObjects/IDetector.hpp>
11 #include <vision_module/Tools/SortFuntions.hpp>
12 #include <algorithm> // std::sort
13 using namespace cv;
14 
15 class ObstacleC
16 {
17 private:
18 
19  double confidence;
20 public:
21  Point2d Position;
22  long unsigned int id;
23  double getConfidence()
24  {
25  boundry_n(confidence, 0, 1);
26  return confidence;
27  }
28 
29  bool decayConfidence()
30  {
31  confidence *= params.obstacle->decayConfidence();
32  return confidence > 0.1;
33  }
34 
35  ObstacleC(Point2d position, double confidence = 1.0,int id=0) :
36  confidence(confidence), Position(position),id(id)
37  {
38 
39  }
40 
41  bool update(Point2d _pos, double _conf)
42  {
43  if (GetDistance(_pos, Position) <= params.obstacle->maxPossibleJump())
44  {
45  boundry_n(_conf, 0, 1);
46  lowPass(_pos, Position, _conf * params.obstacle->lowPassCoef());
47  confidence = _conf;
48  return true; //Input was similar to this obstacle and updated this obstacle
49  }
50  else
51  {
52  return false; //Input obstacle is not similar to this obstacle
53  }
54  }
55 
56  virtual ~ObstacleC()
57  {
58  }
59 };
65 class ObstacleDetector: public IDetector
66 {
67 public:
68 
69  bool GetObstacleContours(Mat &obstacleMask, vector<cv::Point2f> &resInReal,
70  Mat &guiImg, CameraProjections &projection, bool SHOWGUI)
71  {
72  const int minArea = params.obstacle->minArea();
73  vector<vector<cv::Point> > obstContours;
74 
75  cv::findContours(obstacleMask, obstContours, CV_RETR_EXTERNAL,
76  CV_CHAIN_APPROX_NONE);
77 
78  resInReal.reserve(obstContours.size());
79 
80  for (size_t i = 0; i < obstContours.size(); i++) // iterate through each contour.
81  {
82  cv::approxPolyDP(obstContours[i], obstContours[i],
83  cv::arcLength(obstContours[i], true) * 0.003, true);
84  double area = contourArea(obstContours[i]);
85  if (area <= minArea)
86  continue;
87  cv::Rect rec = boundingRect(obstContours[i]);
88  cv::Point btnPoint(rec.x + rec.width / 2, rec.y + rec.height);
89 
90  cv::Point2f btnInReal;
91  if (!projection.GetOnRealCordinate_single(btnPoint, btnInReal))
92  {
93  ROS_ERROR("Error in programming!");
94  }
95  if (SHOWGUI && params.obstacle->showResult())
96  {
97  drawContours(guiImg, obstContours, i, Scalar(240, 250, 250), 2,
98  8);
99  }
100  double distanceToRobot = GetDistance(btnInReal);
101  if (distanceToRobot >= params.obstacle->minDistance()
102  && distanceToRobot <= params.obstacle->maxDistance())
103  {
104  resInReal.push_back(btnInReal);
105  }
106  }
107 
108  return resInReal.size() > 0;
109  }
110 
111  inline bool Init()
112  {
113  return true;
114  }
115 };
For pixel projections.
Definition: CameraProjections.hpp:50
For detecting other robots with black feet.
Definition: ObstacleDetector.hpp:65