-
Notifications
You must be signed in to change notification settings - Fork 12
/
trackbar.h
80 lines (63 loc) · 2.14 KB
/
trackbar.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
#ifndef TRACKBAR_HPP
#define TRACKBAR_HPP
#include <opencv2/opencv.hpp>
class DoubleTrackManager{
struct DoubleTrack{
public:
int int_value = 0;
double precision = 100;
double max_value;
double curr_value = 0;
bool change = false;
void setup(const std::string& field_name, const std::string& window_name, double max_value, double start_value = 0, double default_value = 0, unsigned precision = 100){
int_value = default_value * precision;
cv::createTrackbar(field_name, window_name, &int_value, max_value * precision, DoubleTrack::callback, this);
this->precision = precision;
this->max_value = max_value;
cv::setTrackbarPos(field_name, window_name, (int)((max_value * precision)/2) + (int)(start_value*precision));
}
double getCurrVal(){
change = false;
return curr_value;
}
bool changeOccured(){
return change;
}
static void callback(int, void* object){
DoubleTrack* pObject = static_cast<DoubleTrack*>(object);
double val = (pObject->int_value / pObject->precision) - pObject->max_value/2;
if(pObject->curr_value != val) pObject->change = true;
pObject->curr_value = val;
}
};
std::map<std::string, DoubleTrack> tracks;
std::string winname;
public:
DoubleTrackManager(){
}
void setupWindow(std::string winname){
this->winname = winname;
cv::namedWindow(winname);
}
void addTrack(std::string name, double maxVal, double startVal = 0){
tracks[name] = DoubleTrack();
tracks[name].setup(name, winname, maxVal, startVal);
}
double getTrackValue(std::string name){
return tracks[name].getCurrVal();
}
bool changeOccured(){
bool change = false;
for (auto& kv : tracks) {
if(kv.second.changeOccured()) change = true;
}
for (auto& kv : tracks) {
kv.second.change = false;
}
return change;
}
void spin(int ms = 15){
cv::waitKey(ms);
}
};
#endif