-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpticalFlowManager.cpp
97 lines (84 loc) · 2.77 KB
/
OpticalFlowManager.cpp
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
#include "OpticalFlowManager.h"
#include "Utils.h"
void OpticalFlowManager::updateFlow(Mat prev,Mat next){
this->features_prev = this->features_next;
Mat err;
if(features_prev.size() >0 ){
if(features_next.size() > 0){
calcOpticalFlowPyrLK(prev, next,this->features_prev,this->features_next,this->status,err);
}
}
}
bool OpticalFlowManager::isHandAlreadyTracked(Rect hand){
Rect handSup = Rect(hand.x-10,hand.y-10,hand.width+10,hand.height+10);
int nbPoints = 0;
int index = -1;
for(int i = 0 ; i < this->features_next.size() ; i++){
if(this->features_next.at(i).inside(handSup)){
nbPoints++;
index = i;
}
}
if(nbPoints == 1){
this->features_next[index] = Point2f(hand.x+(hand.width/2),hand.y+(hand.height/2));
}
return nbPoints > 0;
}
void OpticalFlowManager::addFeatureToTrack(Point2f p){
vector<Scalar> colors;
colors.push_back(Scalar(0,0,255));
colors.push_back(Scalar(0,255,0));
colors.push_back(Scalar(0,255,255));
for(int i = 0 ; i < this->features_next.size() ; i++){
for(int j=0;j< colors.size();j++){
if( this->features_color[i] == colors[j]){
colors.erase(colors.begin()+j);
break;
}
}
}
this->features_next.push_back(p);
//Scalar color = Utils::getRandomColor();
this->features_color.push_back(colors[0]);
}
void OpticalFlowManager::addFeatureToTrack(Point2f p,Scalar color){
this->features_next.push_back(p);
this->features_color.push_back(color);
}
vector<Point2f> OpticalFlowManager::getFeatureNext(){
return this->features_next;
}
vector<Point2f> OpticalFlowManager::getFeaturePrevious(){
return this->features_prev;
}
vector<uchar> OpticalFlowManager::getStatus(){
return this->status;
}
vector<Scalar> OpticalFlowManager::getFeaturesColor(){
return this->features_color;
}
void OpticalFlowManager::setFeatureColor(int index,Scalar color){
this->features_color[index]=color;
}
vector<Point2f> OpticalFlowManager::getPointsInRect(Rect rect){
vector<Point2f> points;
Rect rectSup = Rect(rect.x-15,rect.y-15,rect.width+15,rect.height+15);
for(int i = 0 ; i < this->features_next.size() ; i++){
if(this->features_next.at(i).inside(rectSup)){
points.push_back(this->features_next.at(i));
}
}
return points;
}
int OpticalFlowManager::getPointIndex(Point2f p){
for(int i = 0 ; i < this->features_next.size() ; i++){
if(this->features_next[i] == p){
return i;
}
}
return -1;
}
void OpticalFlowManager::removeFeature(int index){
this->features_next.erase(this->features_next.begin()+index);
this->features_color.erase(this->features_color.begin()+index);
}