util.h
Go to the documentation of this file.
1 /*
2  * This file is part of Laustracker.
3  *
4  * Copyright (C) 2013 Christian Jann <christian.jann@ymail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef HEADER_LAUSTRACKER_UTIL_H_INCLUDED
21 #define HEADER_LAUSTRACKER_UTIL_H_INCLUDED
22 
28 #include <opencv2/core/core.hpp>
29 #include <opencv2/imgproc/imgproc.hpp>
30 #include <opencv2/highgui/highgui.hpp>
31 #include <boost/lexical_cast.hpp>
32 #include <iostream>
33 #include <iterator>
34 #include <vector>
35 #include <boost/property_tree/ptree.hpp>
36 
37 using namespace cv;
38 using namespace std;
39 
46 void laustracker_imshow(const string& windoname, const Mat& image);
47 
55 void laustracker_imshow_croped(const string& windoname, const Mat& image,
56  vector <Point> rect);
57 
67 double angle(Point pt1, Point pt2, Point pt0);
68 
69 
80 bool intersection(Point o1, Point p1, Point o2, Point p2, Point &r);
81 
88 void scale_rect(vector <Point>& rect, int pixels = 15);
89 
90 
100 int countColor(cv::Mat image, int color = 255);
101 
106 string get_selfpath();
107 
113 string get_selfdir(bool debug = false);
114 
119 float DotProductPoints(Point2f const &a, Point2f const &b, Point2f const &c);
120 
125 float CrossProductPoints(Point2f const &a, Point2f const &b, Point2f const &c);
126 
133 float DistancePointPoint(Point2f const &a, Point2f const &b);
134 
139 float DistanceLinePoint(Point2f const &a, Point2f const &b, Point2f const &c, bool isSegment);
140 
147 Point2f centerPointPoint(Point2f a, Point2f b);
148 
154 float degrees(float radians);
155 
156 template<typename T>
157 std::vector<T> to_array(const std::string& s)
158 {
159  std::vector<T> result;
160  std::stringstream ss(s);
161  std::string item;
162  while (ss >> item)
163  {
164  //cout << "item: " << item<<"\n";
165  result.push_back(boost::lexical_cast<T>(item));
166  }
167  return result;
168 }
169 
170 template<typename T>
171 std::ostream &operator <<(std::ostream &os, const std::vector<T> &v)
172 {
173  using namespace std;
174  copy(v.begin(), v.end(), ostream_iterator<T>(os, "\n"));
175  return os;
176 }
177 
182 class FpS
183 {
184 
185 private:
186 
187  int64_t timespecDiff(struct timespec *timeA_p, struct timespec *timeB_p);
188  int fps_counter;
189  struct timespec fps_time_start, fps_time_end;
190 
191 public:
192 
197  FpS();
198 
200  float fps;
201 
203  void measure();
204 
206  void print();
207 
209  bool isNew();
210 
211 };
212 
220 Scalar get_gimp_color(boost::property_tree::ptree conf, string color_name);
221 
222 
223 
224 #endif
double angle(Point pt1, Point pt2, Point pt0)
Calculate the cosine between two vectors.
void laustracker_imshow(const string &windoname, const Mat &image)
Like imshow(), resizes the image on smaller screens.
void scale_rect(vector< Point > &rect, int pixels=15)
Scale a rectangle.
float DistanceLinePoint(Point2f const &a, Point2f const &b, Point2f const &c, bool isSegment)
Calculate the Distance between a straight line a a point.
string get_selfdir(bool debug=false)
Determine the directory of the current executable.
float CrossProductPoints(Point2f const &a, Point2f const &b, Point2f const &c)
Calculate the cross product of three points.
Scalar get_gimp_color(boost::property_tree::ptree conf, string color_name)
Get a HSV color from a Boost property_tree and convert the color from Gimp&#39;s color space into OpenCV&#39;...
int countColor(cv::Mat image, int color=255)
Count the number of pixels of a specified color within a image region.
float DotProductPoints(Point2f const &a, Point2f const &b, Point2f const &c)
Calculate the dot product of three points.
Point2f centerPointPoint(Point2f a, Point2f b)
Calculate the center point between two points.
float degrees(float radians)
Convert radians to degrees.
void laustracker_imshow_croped(const string &windoname, const Mat &image, vector< Point > rect)
Like imshow(), resizes the image on smaller screens and crops the image if the labyrinth position is ...
Class to measure frames per second.
Definition: util.h:182
bool intersection(Point o1, Point p1, Point o2, Point p2, Point &r)
Calculate the intersection point of two straight lines.
float DistancePointPoint(Point2f const &a, Point2f const &b)
Calculate the distance between two points.
float fps
Current frame rate.
Definition: util.h:200
string get_selfpath()
Get the path of the current executable.