-
Notifications
You must be signed in to change notification settings - Fork 0
/
handanalyzer.cpp
251 lines (213 loc) · 7.66 KB
/
handanalyzer.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
#include "handanalyzer.h"
#include <opencv2/opencv.hpp>
#include <QDebug>
#include "midiparametercontroller.h"
using namespace cv;
HandAnalyzer::HandAnalyzer():
midiParameterController(new MidiParameterController())
,midiNoteController(new MidiNoteController())
{
midiNoteController->setNoteForNoteWithOctave("C", 4);
midiNoteController->initPreset();
}
cv::Mat HandAnalyzer::getResultMatFromMat(const Mat &input){
Mat copy;
Mat output;
Mat conv;
input.convertTo(output, CV_8UC1);
Mat in[] = {output, output, output};
merge(in,3,conv);
input.copyTo(copy);
erode(copy,copy, Mat());
erode(copy,copy, Mat());
dilate(copy,copy, Mat());
dilate(copy,copy, Mat());
vector<vector<Point> > contours;
Mat converted;
copy.convertTo(converted, CV_8UC1);
findContours(converted, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
int indexOfLeftBiggestContour = -1;
int indexOfRightBiggestContour = -1;
int leftArea = 0;
int rightArea = 0;
int xBorder = copy.cols / 4;
int index = 0;
for (vector<Point> contour : contours){
int area = contourArea(contour);
Rect rect = boundingRect(contour);
int xCenter = (rect.x + rect.width) / 2;
//qDebug() << "xborder: " << xBorder << "xcenter: " << xCenter;
if (xCenter < xBorder){
//Left side
if (area > leftArea){
leftArea = area;
indexOfLeftBiggestContour = index;
}
} else {
//Right side
if (area > rightArea){
rightArea = area;
indexOfRightBiggestContour = index;
}
}
index++;
}
if (indexOfRightBiggestContour != -1){
//qDebug() << "Rightside";
Rect rect = boundingRect(contours[indexOfRightBiggestContour]);
midiParameterController->setMidiController(rect.y);
rectangle(conv,rect,(255,255,255),1,8,0);
}
if (indexOfLeftBiggestContour != -1){
//qDebug() << "löööftside";
this->defineFingerDepth(contours[indexOfLeftBiggestContour], copy);
}
/*
int secondBiggestArea=0;
int indexOfSecondBiggestContour = -1;
int biggestArea = 0;
int indexOfBiggestContour = -1;
for (int count = 0; count < contours.size(); count++){
vector<Point>contour = contours[count];
int area = contourArea(contour);
if (area > biggestArea){
secondBiggestArea=biggestArea;
biggestArea = area;
indexOfSecondBiggestContour = indexOfBiggestContour;
indexOfBiggestContour = count;
}
}
if (indexOfSecondBiggestContour != -1){
Rect rect = boundingRect(contours[indexOfSecondBiggestContour]);
midiParameterController->setMidiController(rect.y);
this->defineFingerDepth(contours[indexOfBiggestContour], copy);
rectangle(conv,rect,(255,255,255),1,8,0);
} */
// vector<vector<int> > hullsI(0);
// vector<vector<Point> > hullsP(0);
// vector<vector<Vec4i> > defects(0);
//if (contours.size() > 0){
/// Find the convex hull object for each contour
vector<vector<int> > hullsI(contours.size());
vector<vector<Point> > hullsP(contours.size());
vector<vector<Vec4i> > defects(contours.size());
//vector<CvConvexityDefect> defects(contours.size());
// }
for(int i = 0; i <contours.size(); ++i){
//find the hulls
convexHull(contours[i], hullsI[i], false, false);
convexHull(contours[i], hullsP[i], false, true);
//find the defects
if (contours[i].size() >3 )
{
convexityDefects(contours[i], hullsI[i], defects[i]);
}
}
Scalar color1(255,255,0);
//The next line causes an error
//drawContours(output, hullsP, indexOfBiggestContour, color1, 1);
Scalar color2(0,255,0);
Scalar color3(0,0,255);
Scalar color4(255,0,0);
Scalar color5(0,255,255);
/// Draw convexityDefects
//for( int i = 0; i< contours.size(); i++ )
//{
int finger = 0;
if (indexOfLeftBiggestContour != -1){
//size_t count = contours[i].size();
size_t count = contours[indexOfLeftBiggestContour].size();
if( count >=300 )
{
vector<Vec4i>::iterator d=defects[indexOfLeftBiggestContour].begin();
while( d!=defects[indexOfLeftBiggestContour].end() ) {
Vec4i& v=(*d);
int startidx=v[0]; Point ptStart( contours[indexOfLeftBiggestContour][startidx] );
int endidx=v[1]; Point ptEnd( contours[indexOfLeftBiggestContour][endidx] );
int faridx=v[2]; Point ptFar( contours[indexOfLeftBiggestContour][faridx] );
float depth = v[3] / 256;
line(conv, ptStart, ptEnd, color2, 1 );
line(conv, ptStart, ptFar, color3, 1 );
line(conv, ptEnd, ptFar, color3, 1 );
if (depth > depthThreshold){
finger++;
circle(conv, ptFar, 10, color5, 2 );
circle(conv, ptStart, 12, color4, 4);
circle(conv, ptEnd, 12, color4, 4);
}
d++;
}
if (finger < 0){
finger = 0;
} else if (finger > 4){
finger = 4;
}
}
}
this->setNumberOfFinger(finger);
return conv;
}
//Neu von Finn
bool HandAnalyzer::isSchlag(){
return this->schlag;
}
void HandAnalyzer::defineFingerDepth(cv::vector<Point> cnt, const Mat &input){
int width = input.cols;
int height = input.rows;
float fingerhoehe = height * 0.15;
Rect rect = boundingRect(cnt);
int contourWidth = rect.width;
int contourHeight = rect.height;
// qDebug() << "Rectwidth: " << contourWidth << ".Recthieght: " << contourHeight;
float widthVerhaeltnis = width / contourWidth;
float heightVerhaeltnis = height / contourHeight;
float deltaAverage = (widthVerhaeltnis + heightVerhaeltnis) / 2;
depthThreshold = fingerhoehe / deltaAverage;
// qDebug() << "Neue Tiefe für Finger:" << depthThreshold << " Fingerhoehe: " << fingerhoehe << " deltaAverage: " << deltaAverage;
int contourSizeAverage = (contourHeight + contourWidth) / 2;
this->setRectAverage(contourSizeAverage);
}
void HandAnalyzer::setNumberOfFinger(int n){
int bound = 6;
this->fingerValues[bound] = n;
for(int i = 0; i < bound; i++){
this->fingerValues[i] = this->fingerValues[i+1];
}
int sum = 0;
int divider = 0;
for(int i = 0; i < bound + 1; i++){
//qDebug() << "Fingervalue #" << i << " is " << this->fingerValues[i];
if (this->fingerValues[i] != 0){
sum += this->fingerValues[i];
divider++;
}
}
//qDebug() << "Summe: " << sum;
if ( sum > 0 && divider > 0){
this->numberOfFingers = sum / divider;
//qDebug() << "Neue Fingeranzahl: " << this->numberOfFingers;
}
}
int HandAnalyzer::getNumberOfFingers(){
return this->numberOfFingers;
}
void HandAnalyzer::setRectAverage(int rectA){
float rectVerhaeltnis = 1.5;
int bound = 7;
for(int i = 0; i < bound; i++){
this->rectAverages[i] = this->rectAverages[i+1];
}
this->rectAverages[bound] = rectA;
int sum = 0;
bound -= 2;
for(int i = 0; i < bound + 1; i++){
sum += this->rectAverages[i];
}
int averageOfTheOlderRects = sum / bound;
if (rectA > (averageOfTheOlderRects * rectVerhaeltnis)){
this->schlag = true;
//qDebug() << "Schlag mit " << this->numberOfFingers << " Finger.";
} else {
this->schlag = false;
}
}