-
Notifications
You must be signed in to change notification settings - Fork 0
/
handanalyzer.cpp.orig
199 lines (165 loc) · 5.92 KB
/
handanalyzer.cpp.orig
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
#include "handanalyzer.h"
#include <opencv2/opencv.hpp>
#include <QDebug>
#include "midiparametercontroller.h"
using namespace cv;
HandAnalyzer::HandAnalyzer():
midiParameterController(new MidiParameterController())
{
}
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 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){
biggestArea = area;
indexOfBiggestContour = count;
}
}
if (indexOfBiggestContour != -1){
<<<<<<< HEAD
Rect rect = boundingRect(contours[indexOfBiggestContour]);
midiParameterController->setMidiController(rect.y);
rectangle(conv,rect,(255,255,255),1,8,0);
=======
this->defineFingerDepth(contours[indexOfBiggestContour], copy);
>>>>>>> 69ee5927536be6f385ea763f6a406a072167dfc4
}
// 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 (indexOfBiggestContour != -1){
//size_t count = contours[i].size();
size_t count = contours[indexOfBiggestContour].size();
if( count >=300 )
{
vector<Vec4i>::iterator d=defects[indexOfBiggestContour].begin();
while( d!=defects[indexOfBiggestContour].end() ) {
Vec4i& v=(*d);
int startidx=v[0]; Point ptStart( contours[indexOfBiggestContour][startidx] );
int endidx=v[1]; Point ptEnd( contours[indexOfBiggestContour][endidx] );
int faridx=v[2]; Point ptFar( contours[indexOfBiggestContour][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 > 5){
finger = 5;
}
}
}
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.25;
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 = 12;
this->fingerValues[bound] = n;
for(int i = 0; i < bound; i++){
this->fingerValues[i] = this->fingerValues[i+1];
}
int sum = 0;
for(int i = 0; i < bound + 1; i++){
//qDebug() << "Fingervalue #" << i << " is " << this->fingerValues[i];
sum += this->fingerValues[i];
}
//qDebug() << "Summe: " << sum;
this->numberOfFingers = sum / bound;
}
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;
}
}