NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
General.hpp
1 // General Functions for Vision Module
2 // Author: Hafez Farazi <farazi@ais.uni-bonn.de>
3 
4 #pragma once
5 #include <math.h>
6 #include <vector>
7 #include <opencv2/opencv.hpp>
8 #include <vision_module/Tools/Parameters.hpp>
9 #include <vision_module/Tools/LineSegment.hpp>
10 #include <boost/timer/timer.hpp>
11 #include <ros/ros.h>
12 #include <sstream>
13 using namespace cv;
14 using namespace std;
15 
16 #define HAF_LOG_THROTTLE(rate, level, name, ...) \
17  do \
18  { \
19  ROSCONSOLE_DEFINE_LOCATION(true, level, name); \
20  static double last_hit = 0.0; \
21  ::ros::WallTime now = ::ros::WallTime::now(); \
22  if (ROS_UNLIKELY(__rosconsole_define_location__enabled) && ROS_UNLIKELY(last_hit + rate <= now.toSec())) \
23  { \
24  last_hit = now.toSec(); \
25  ROSCONSOLE_PRINT_AT_LOCATION(__VA_ARGS__); \
26  } \
27  } while(0)
28 
29 #define HAF_INFO_THROTTLE(rate, ...) HAF_LOG_THROTTLE(rate, ::ros::console::levels::Info, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
30 #define HAF_WARN_THROTTLE(rate, ...) HAF_LOG_THROTTLE(rate, ::ros::console::levels::Warn, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
31 #define HAF_ERROR_THROTTLE(rate, ...) HAF_LOG_THROTTLE(rate, ::ros::console::levels::Error, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
32 #define HAF_DEBUG_THROTTLE(rate, ...) HAF_LOG_THROTTLE(rate, ::ros::console::levels::Debug, ROSCONSOLE_DEFAULT_NAME, __VA_ARGS__)
33 
34 #define min_n(a,b) ((a) < (b) ? (a) : (b))
35 #define max_n(a,b) ((a) > (b) ? (a) : (b))
36 #define boundry_n(n,a,b) {n=min_n(b,n);n=max_n(a,n);}
37 #define DB() printf("Line number %d in file %s\n", __LINE__, __FILE__)
38 #define DT() boost::timer::auto_cpu_timer tmpAutoTimer
39 class CircleC
40 {
41 public:
42  cv::Point2f Center;
43  float Radius;
44  CircleC()
45  {
46  Radius=0;
47  }
48  CircleC(cv::Point2f Center,float Radius):Center(Center),Radius(Radius)
49  {
50 
51  }
52  CircleC(const CircleC &_in):Center(_in.Center),Radius(_in.Radius)
53  {
54 
55  }
56 };
57 
58 CircleC mergeCircleC(CircleC A, CircleC B);
59 bool hasIntersection(CircleC A, CircleC B);
60 bool lineFormIntersectionWithCircleC(LineSegment l, CircleC c, Point2f &p1, Point2f &p2);
61 
62 cv::Rect CircleCToRect(CircleC circ);
63 CircleC RectToCircleC(cv::Rect rec);
64 void RotateAroundPoint(Point2d pQuery, double alpha, Point2d &res);
65 void RotateCoordinateAxis(double alpha, Point2d p, Point2d &res);
66 double Radian2Degree(double r);
67 double Degree2Radian(double d);
68 void lowPass(Point3d newrec, Point3d &res, double coef = 0.01);
69 void lowPass(Point2d newrec, Point2d &res, double coef = 0.01);
70 void lowPass(double newrec, double &res, double coef = 0.01);
71 void lowPassAngleDegree180(double newrec, double &res, double coef = 0.01);
72 double AngleSubDegree180(double first, double second);
73 Point2d GetCenterMinBounding(vector<Point> con);
74 Point2d GetCenterBoundingRec(vector<Point> con);
75 double GetDistance(Point2d p);
76 float GetDistance(Point2f p);
77 double GetDistance(Point2d p, Point2d p2);
78 
79 int Left(Rect rec);
80 //http://www.juergenwiki.de/work/wiki/doku.php?id=public:hog_descriptor_computation_and_visualization
81 Mat get_hogdescriptor_visual_image(Mat& origImg,
82  vector<float>& descriptorValues, Size winSize, Size cellSize,
83  int scaleFactor, double viz_factor);
84 
91 cv::Point2f RotateCoordinateAxis(double alpha, cv::Point2f p);
95 cv::Point2f RotateAroundPoint(cv::Point2f pQuery, double alpha,
96  cv::Point2f pCenter);
97 
101 cv::Point2f RotateAroundPoint(cv::Point2f pQuery, double alpha);
102 int Top(Rect rec);
103 int Right(Rect rec);
104 int Bottom(Rect rec);
105 Scalar grayWhite();
106 cv::Scalar pinkColor();
107 cv::Scalar pinkMeloColor();
108 Scalar whiteColor();
109 Scalar redColor();
110 Scalar darkOrangeColor();
111 Scalar redMeloColor();
112 Scalar greenColor();
113 Scalar yellowColor();
114 Scalar blueColor();
115 Scalar blueMeloColor();
116 Scalar blackColor();
117 cv::Scalar blackGary();
118 
119 Scalar randColor();
120 Point2d GetCenter(Rect rec);
121 bool MinXPoint(vector<Point2f> con, Point2f &res);
122 bool MinYPoint(vector<Point2f> con, Point2f &res);
123 bool MaxXPoint(vector<Point2f> con, Point2f &res);
124 bool MaxYPoint(vector<Point2f> con, Point2f &res);
125 bool MinXPoint(vector<Point> con, Point &res);
126 bool MinYPoint(vector<Point> con, Point &res);
127 bool MaxXPoint(vector<Point> con, Point &res);
128 bool MaxYPoint(vector<Point> con, Point &res);
129 
130 double AngleBetween0to180(RotatedRect calculatedRect);
131 vector<Point> convert(const vector<Point2f> &con, vector<Point> res);
132 
133 vector<Point2f> convert(const vector<Point> &con, vector<Point2f> res);
134 Point convert(const Point2f &in);
135 Point2f convert(const Point &in);
136 float
137 dist3D_Segment_to_Segment(LineSegment S1, LineSegment S2);
138 bool checkBox_Size_Scale_Angle(const vector<Point2f> &con, hsvRangeC type);
139 
140 float DistanceFromLineSegment(LineSegment line, Point2f p);
141 double Distance2point(Point2f begin, Point2f end);
142 
143 Point2f GetAverage(Point2f p0, Point2f p1);
144 Point2f GetWeigthedAverage(Point2f p0, Point2f p1, float w0, float w1);
145 float GetWeigthedAverage(float p0, float p1, float w0, float w1);
146 Point2f AddP(Point2f a, float b);
147 //Normalize to [0,360):
148 double CorrectAngleDegree360(double x);
149 //Normalize to [-180,180)
150 double CorrectAngleDegree180(double x);
151 //Normalize to [0,360):
152 double CorrectAngleRadian360(double x);
153 //Normalize to [-180,180)
154 double CorrectAngleRadian180(double x);
155 //Normalize to [-180,180)
156 float AngleDiffDegree180(float first, float second);
157 //Normalize to [-180,180)
158 float AngleDiffRadian180(float first, float second);
159 
160 bool MergeLinesMax(vector<LineSegment> resLinesReal, double maxDegree,
161  double maxDistance, vector<LineSegment> &clusteredLines, Rect box,
162  bool useBounding = false);
163 
164 bool MergeLinesOnce(vector<LineSegment> resLinesReal, double maxDegree,
165  double maxDistance, vector<LineSegment> &clusteredLines, Rect box,
166  bool useBounding = false);
167 void drawLine(Mat img, Vec4f line, int thickness, Scalar color);
168 bool calcHist3Channels(cv::Mat &colorMatRoi, cv::Mat &justObjMask,
169  cv::Mat hist_base[3]);
170 
171 cv::Rect clipRect(cv::Rect _in, cv::Rect _box);
172 bool minDimentionRect(int _min, cv::Rect &_in);
173 bool maxDimentionRect(int _max, cv::Rect &_in);
174 bool mergeRect(cv::Rect &_in, cv::Rect _in2);
175 cv::Rect asymetricScaleRec(double _xLeftRatio, double _xRightRatio,
176  double _yTopRatio, double _yDownRatio, cv::Rect _in);
177 cv::Rect asymetricScaleRec(double _xLeftRatio, double _xRightRatio,
178  double _yTopRatio, double _yDownRatio, cv::Rect _in, cv::Rect _box);
179 cv::Rect scaleRec(double _xRatio, double _yRatio, cv::Rect _in);
180 cv::Rect scaleRec(double _xRatio, double _yRatio, cv::Rect _in, cv::Rect _box);
181 
182 template<class _Tp> bool write2File(const string& filename,
183  const vector<_Tp> &items)
184 {
185  try
186  {
187  FileStorage fs(params.configPath + filename, FileStorage::WRITE);
188 // ROS_INFO("write2File=%s%s",params.configPath.c_str(), filename.c_str());
189 // ROS_INFO("write2File Size=%d",(int)items.size());
190  ostringstream oss;
191 
192  fs << "Count" << (int) items.size();
193 
194  for (size_t i = 0; i < items.size(); ++i)
195  {
196  oss << "Element" << i;
197  fs << oss.str() << items[i];
198  // clear, because eof or other bits may be still set.
199  oss.clear();
200  oss.str("");
201  }
202  fs.release();
203  return true;
204  } catch (Exception &e)
205  {
206  ROS_ERROR("Problem writing %s |-> what = %s", filename.c_str(),
207  e.what());
208  return false;
209  }
210 }
211 
212 template<class _Tp> bool readFromFile(const string& filename,
213  vector<_Tp> &myVec)
214 {
215  try
216  {
217  FileStorage fs(params.configPath + filename, FileStorage::READ);
218 
219  ostringstream oss;
220  int num = 0;
221  num = (int) fs["Count"];
222  myVec.resize(num);
223  for (int i = 0; i < num; ++i)
224  {
225  oss << "Element" << i;
226  fs[oss.str()] >> myVec[i];
227  oss.clear();
228  oss.str("");
229  }
230  fs.release();
231  return num > 0;
232  } catch (Exception &e)
233  {
234  ROS_ERROR("Problem parsing %s |-> what = %s", filename.c_str(),
235  e.what());
236  return false;
237  }
238 }
239 
240 template<class _Tp> bool add2File(const string& filename, const _Tp &item)
241 {
242  vector<_Tp> readVec;
243  bool res = readFromFile(filename, readVec);
244  readVec.push_back(item);
245  res &= write2File(filename, readVec);
246  return res;
247 }
248 
249 template<typename T>
250 string to_string(T pNumber)
251 {
252  ostringstream oOStrStream;
253  oOStrStream << pNumber;
254  return oOStrStream.str();
255 }
A class representing line segments.
Definition: LineSegment.hpp:17
HSV range class.
Definition: Parameters.hpp:126