forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathutils.h
executable file
·156 lines (117 loc) · 3.88 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <boost/shared_ptr.hpp>
#include <iostream>
#include "Point.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QMainWindow>
namespace Utils {
typedef boost::shared_ptr<const cv::Mat> SharedImage;
#define xForEach(iter, container) \
for (typeof(container.begin()) iter = container.begin(); iter != container.end(); iter++)
#define xForEachBack(iter, container) \
for (typeof(container.rbegin()) iter = container.rbegin(); iter != container.rend(); iter++)
struct QuitNow: public std::exception {
using std::exception::what;
QuitNow() { }
virtual ~QuitNow() throw() { }
virtual const char* what() throw() {
return "QuitNow: request normal termination of program.\n(You should not see this message. Please report it if you do.)";
}
};
template <class T> inline T square(T a) {
return a * a;
}
template <class T> std::ostream &operator<< (std::ostream &out, std::vector<T> const &vec) {
out << vec.size() << std::endl;
xForEach(iter, vec) {
out << *iter << std::endl;
}
return out;
}
template <class T> std::istream &operator>> (std::istream &in, std::vector<T> &vec) {
int size;
T element;
vec.clear();
in >> size;
for (int i = 0; i < size; i++) {
in >> element;
vec.push_back(element);
}
return in;
}
template <class T> T teeFunction(T source, char *prefix, char *postfix="\n") {
std::cout << prefix << source << postfix;
return source;
}
#define debugTee(x) teeFunction(x, #x ": ")
class ConstancyDetector {
public:
ConstancyDetector(int maxCounter):
_value(-1),
_counter(0),
_maxCounter(maxCounter)
{
}
bool isStable() {
return _counter >= _maxCounter;
}
bool isStableExactly() {
return _counter == _maxCounter;
}
bool observe(int newValue) {
if (newValue != _value || newValue < 0) {
_counter = 0;
} else {
_counter++;
}
_value = newValue;
return isStable();
}
void reset() {
_counter = 0;
_value = -1;
}
private:
int _value;
int _counter;
int _maxCounter;
};
// #define output(X) { std::cout << #X " = " << X << std::endl; }
template <class T> int maxAbsIndex(T const &vec, int size) {
int maxIndex = 0;
for (int i = 0; i < size; i++) {
if (fabs(vec[i]) > fabs(vec[maxIndex])) {
maxIndex = i;
}
}
return maxIndex;
}
boost::shared_ptr<cv::Mat> createImage(const CvSize &size, int type);
void releaseImage(cv::Mat *image);
cv::Rect* getMonitorGeometryByIndex(int screenIndex);
cv::Rect* getSecondMonitorGeometry();
cv::Rect* getFirstMonitorGeometry();
void mapToFirstMonitorCoordinates(Point monitor2Point, Point &monitor1Point);
cv::Point mapFromCameraToDebugFrameCoordinates(cv::Point point);
cv::Point mapFromSecondMonitorToDebugFrameCoordinates(cv::Point point);
void mapToVideoCoordinates(Point monitor2Point, double resolution, Point &videoPoint, bool reverseX=true);
void mapToNeuralNetworkCoordinates(Point point, Point &nnPoint);
void mapFromNeuralNetworkToScreenCoordinates(Point nnPoint, Point &point);
void boundToScreenArea(Point &estimate);
std::string getUniqueFileName(std::string directory, std::string baseFileName);
std::string getParameter(std::string name);
double getParameterAsDouble(std::string name, double defaultValue=0.0);
// Functions for reading calibration and testing targets
std::vector<Point> readAndScalePoints(std::ifstream &in);
std::vector<Point> loadPoints(std::ifstream &in);
std::vector<Point> scaled(const std::vector<Point> &points, double x, double y);
void normalizeGrayScaleImage(cv::Mat *image, double standardMean=127, double standardStd=50);
//void normalizeGrayScaleImage2(cv::Mat *image, double standardMean=127, double standardStd=50);
void convertAndResize(const cv::Mat &src, cv::Mat& gray, cv::Mat& resized, double scale);
void printMat(CvMat *mat);
void printMat(cv::Mat mat);
}
namespace boost {
template <> void checked_delete(cv::Mat *image);
}