-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandGesture.cpp
286 lines (266 loc) · 7.57 KB
/
handGesture.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "handGesture.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
HandGesture::HandGesture(){
frameNumber=0;
nrNoFinger=0;
fontFace = FONT_HERSHEY_PLAIN;
}
void HandGesture::initVectors(){
hullI=vector<vector<int> >(contours.size());
hullP=vector<vector<Point> >(contours.size());
defects=vector<vector<Vec4i> > (contours.size());
}
void HandGesture::analyzeContours(){
bRect_height=bRect.height;
bRect_width=bRect.width;
}
string HandGesture::bool2string(bool tf){
if(tf)
return "true";
else
return "false";
}
string HandGesture::intToString(int number){
stringstream ss;
ss << number;
string str = ss.str();
return str;
}
void HandGesture::printGestureInfo(Mat src){
int fontFace = FONT_HERSHEY_PLAIN;
Scalar fColor(245,200,200);
int xpos=src.cols/1.5;
int ypos=src.rows/1.6;
float fontSize=0.7f;
int lineChange=14;
string info= "Figure info:";
putText(src,info,Point(ypos,xpos),fontFace,fontSize,fColor);
xpos+=lineChange;
info=string("Number of defects: ") + string(intToString(nrOfDefects)) ;
putText(src,info,Point(ypos,xpos),fontFace,fontSize ,fColor);
xpos+=lineChange;
info=string("bounding box height, width ") + string(intToString(bRect_height)) + string(" , ") + string(intToString(bRect_width)) ;
putText(src,info,Point(ypos,xpos),fontFace,fontSize ,fColor);
xpos+=lineChange;
info=string("Is hand: ") + string(bool2string(isHand));
putText(src,info,Point(ypos,xpos),fontFace,fontSize ,fColor);
}
bool HandGesture::detectIfHand(){
analyzeContours();
double h = bRect_height;
double w = bRect_width;
isHand=true;
if(fingerTips.size() > 5 ){
isHand=false;
}else if(h==0 || w == 0){
isHand=false;
}else if(h/w > 4 || w/h >4){
isHand=false;
}else if(bRect.x<20){
isHand=false;
}
return isHand;
}
float HandGesture::distanceP2P(Point a, Point b){
float d= sqrt(fabs( pow(a.x-b.x,2) + pow(a.y-b.y,2) )) ;
return d;
}
// remove fingertips that are too close to
// eachother
void HandGesture::removeRedundantFingerTips(){
vector<Point> newFingers;
for(int i=0;i<fingerTips.size();i++){
for(int j=i;j<fingerTips.size();j++){
if(distanceP2P(fingerTips[i],fingerTips[j])<10 && i!=j){
}else{
newFingers.push_back(fingerTips[i]);
break;
}
}
}
fingerTips.swap(newFingers);
}
void HandGesture::computeFingerNumber(){
std::sort(fingerNumbers.begin(), fingerNumbers.end());
int frequentNr;
int thisNumberFreq=1;
int highestFreq=1;
frequentNr=fingerNumbers[0];
for(int i=1;i<fingerNumbers.size(); i++){
if(fingerNumbers[i-1]!=fingerNumbers[i]){
if(thisNumberFreq>highestFreq){
frequentNr=fingerNumbers[i-1];
highestFreq=thisNumberFreq;
}
thisNumberFreq=0;
}
thisNumberFreq++;
}
if(thisNumberFreq>highestFreq){
frequentNr=fingerNumbers[fingerNumbers.size()-1];
}
mostFrequentFingerNumber=frequentNr;
}
void HandGesture::addFingerNumberToVector(){
int i=fingerTips.size();
fingerNumbers.push_back(i);
}
// add the calculated number of fingers to image m->src
void HandGesture::addNumberToImg(MyImage *m){
int xPos=10;
int yPos=10;
int offset=30;
float fontSize=1.5f;
int fontFace = FONT_HERSHEY_PLAIN;
for(int i=0;i<numbers2Display.size();i++){
rectangle(m->src,Point(xPos,yPos),Point(xPos+offset,yPos+offset),numberColor, 2);
putText(m->src, intToString(numbers2Display[i]),Point(xPos+7,yPos+offset-3),fontFace,fontSize,numberColor);
xPos+=40;
if(xPos>(m->src.cols-m->src.cols/3.2)){
yPos+=40;
xPos=10;
}
}
}
// calculate most frequent numbers of fingers
// over 20 frames
void HandGesture::getFingerNumber(MyImage *m){
removeRedundantFingerTips();
if(bRect.height > m->src.rows/2 && nrNoFinger>12 && isHand ){
numberColor=Scalar(0,200,0);
addFingerNumberToVector();
if(frameNumber>12){
nrNoFinger=0;
frameNumber=0;
computeFingerNumber();
numbers2Display.push_back(mostFrequentFingerNumber);
fingerNumbers.clear();
}else{
frameNumber++;
}
}else{
nrNoFinger++;
numberColor=Scalar(200,200,200);
}
addNumberToImg(m);
}
float HandGesture::getAngle(Point s, Point f, Point e){
float l1 = distanceP2P(f,s);
float l2 = distanceP2P(f,e);
float dot=(s.x-f.x)*(e.x-f.x) + (s.y-f.y)*(e.y-f.y);
float angle = acos(dot/(l1*l2));
angle=angle*180/PI;
return angle;
}
void HandGesture::eleminateDefects(MyImage *m){
int tolerance = bRect_height/5;
float angleTol=95;
vector<Vec4i> newDefects;
int startidx, endidx, faridx;
vector<Vec4i>::iterator d=defects[cIdx].begin();
while( d!=defects[cIdx].end() ) {
Vec4i& v=(*d);
startidx=v[0]; Point ptStart(contours[cIdx][startidx] );
endidx=v[1]; Point ptEnd(contours[cIdx][endidx] );
faridx=v[2]; Point ptFar(contours[cIdx][faridx] );
if(distanceP2P(ptStart, ptFar) > tolerance && distanceP2P(ptEnd, ptFar) > tolerance && getAngle(ptStart, ptFar, ptEnd ) < angleTol ){
if( ptEnd.y > (bRect.y + bRect.height -bRect.height/4 ) ){
}else if( ptStart.y > (bRect.y + bRect.height -bRect.height/4 ) ){
}else {
newDefects.push_back(v);
}
}
d++;
}
nrOfDefects=newDefects.size();
defects[cIdx].swap(newDefects);
removeRedundantEndPoints(defects[cIdx], m);
}
// remove endpoint of convexity defects if they are at the same fingertip
void HandGesture::removeRedundantEndPoints(vector<Vec4i> newDefects,MyImage *m){
Vec4i temp;
float avgX, avgY;
float tolerance=bRect_width/6;
int startidx, endidx, faridx;
int startidx2, endidx2;
for(int i=0;i<newDefects.size();i++){
for(int j=i;j<newDefects.size();j++){
startidx=newDefects[i][0]; Point ptStart(contours[cIdx][startidx] );
endidx=newDefects[i][1]; Point ptEnd(contours[cIdx][endidx] );
startidx2=newDefects[j][0]; Point ptStart2(contours[cIdx][startidx2] );
endidx2=newDefects[j][1]; Point ptEnd2(contours[cIdx][endidx2] );
if(distanceP2P(ptStart,ptEnd2) < tolerance ){
contours[cIdx][startidx]=ptEnd2;
break;
}if(distanceP2P(ptEnd,ptStart2) < tolerance ){
contours[cIdx][startidx2]=ptEnd;
}
}
}
}
// convexity defects does not check for one finger
// so another method has to check when there are no
// convexity defects
void HandGesture::checkForOneFinger(MyImage *m){
int yTol=bRect.height/6;
Point highestP;
highestP.y=m->src.rows;
vector<Point>::iterator d=contours[cIdx].begin();
while( d!=contours[cIdx].end() ) {
Point v=(*d);
if(v.y<highestP.y){
highestP=v;
cout<<highestP.y<<endl;
}
d++;
}int n=0;
d=hullP[cIdx].begin();
while( d!=hullP[cIdx].end() ) {
Point v=(*d);
cout<<"x " << v.x << " y "<< v.y << " highestpY " << highestP.y<< "ytol "<<yTol<<endl;
if(v.y<highestP.y+yTol && v.y!=highestP.y && v.x!=highestP.x){
n++;
}
d++;
}if(n==0){
fingerTips.push_back(highestP);
}
}
void HandGesture::drawFingerTips(MyImage *m){
Point p;
int k=0;
for(int i=0;i<fingerTips.size();i++){
p=fingerTips[i];
putText(m->src,intToString(i),p-Point(0,30),fontFace, 1.2f,Scalar(200,200,200),2);
circle( m->src,p, 5, Scalar(100,255,100), 4 );
}
}
void HandGesture::getFingerTips(MyImage *m){
fingerTips.clear();
int i=0;
vector<Vec4i>::iterator d=defects[cIdx].begin();
while( d!=defects[cIdx].end() ) {
Vec4i& v=(*d);
int startidx=v[0]; Point ptStart(contours[cIdx][startidx] );
int endidx=v[1]; Point ptEnd(contours[cIdx][endidx] );
int faridx=v[2]; Point ptFar(contours[cIdx][faridx] );
if(i==0){
fingerTips.push_back(ptStart);
i++;
}
fingerTips.push_back(ptEnd);
d++;
i++;
}
if(fingerTips.size()==0){
checkForOneFinger(m);
}
}