-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kinect.pde
139 lines (123 loc) · 5.3 KB
/
Kinect.pde
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
// Vary these to calibrate the depth detection on the right half of the screen
float rightMinThresh = 800;
float rightMaxThresh = 880;
// Vary these to calibrate the depth detection on the left half of the screen
float leftMinThresh = 800;
float leftMaxThresh = 885;
// Vary these to calibrate the depth detection on the entire screen
float handMinThresh = 800;
float handMaxThresh = 880;
float totalPixels;
// Variables to detect movement on the left side of the screen
int[] leftDepth;
int leftOffset;
float leftSumX = 0;
float leftSumY = 0;
float leftTotalPixels = 0;
float leftAvgX = 0;
float leftAvgY = 0;
// Variables to detect movement on the right side of the screen
int[] rightDepth;
int rightOffset;
float rightSumX = 0;
float rightSumY = 0;
float rightTotalPixels = 0;
float rightAvgX = 0;
float rightAvgY = 0;
// Variables to detect movement all over the screen
int[] handDepth;
int handOffset;
float handSumX = 0;
float handSumY = 0;
float handTotalPixels = 0;
float handAvgX = 0;
float handAvgY = 0;
// Function to initialise the kinect
void loadKinect() {
kinect.initDepth();
}
// Function to detect the movement of the left hand/left side of the screen
void leftHand() {
// Get the raw depth as array of integers
leftDepth = kinect.getRawDepth();
leftSumX = 0;
leftSumY = 0;
leftTotalPixels = 0;
for (int x = 180; x < kinect.width/2 + 50; x++) { //Vary these limits to change the width portion of the screen you want to analyse
for (int y = 180; y < 420; y++) { //Vary these limits to change the height portion of the screen you want to analyse
leftOffset = x + y * kinect.width;
int d = leftDepth[leftOffset];
if (d > leftMinThresh && d < leftMaxThresh) {
leftSumX += x;
leftSumY += y;
leftTotalPixels++;
} else {
}
}
}
leftAvgX = leftSumX / leftTotalPixels;
leftAvgY = leftSumY / leftTotalPixels;
if(!Float.isNaN(leftAvgY) && !Float.isNaN(leftAvgY)) { //To prevent calculation when the value is null
leftAvgX = map(leftAvgX,180,kinect.width/2 + 50,0,width/2); //Done to map the detected average of the left half of 640x480 kinect screen width over to the left half of fullscreen width.
leftAvgY = map(leftAvgY,180,420,0,height); //Done to map the detected average of the right half of 640x480 kinect screen height over to the right half of fullscreen height.
lastTimeLeftHandMoved = millis(); // To calculate the last time hand was detected. For screensaver state
fill(255);
ellipse(leftAvgX, leftAvgY, 30, 30);
}
}
// Function to detect the movement of the right hand/right side of the screen
void rightHand() {
rightDepth = kinect.getRawDepth();
rightTotalPixels = 0;
rightSumX = 0;
rightSumY = 0;
for (int x = kinect.width/2 + 100; x < kinect.width; x++) { //Vary these limits to change the width portion of the screen you want to analyse
for (int y = 180; y < 420; y++) { //Vary these limits to change the height portion of the screen you want to analyse
rightOffset = x + y * kinect.width;
int d = rightDepth[rightOffset];
if (d > rightMinThresh && d < rightMaxThresh) {
rightSumX += x;
rightSumY += y;
rightTotalPixels++;
}
}
}
rightAvgX = rightSumX / rightTotalPixels;
rightAvgY = rightSumY / rightTotalPixels;
if(!Float.isNaN(rightAvgX) && !Float.isNaN(rightAvgY)) { //To prevent calculation when the value is null
rightAvgX = map(rightAvgX,kinect.width/2 + 100,kinect.width,width/2,width); //Done to map the detected average of the right half of 640x480 kinect screen width over to the right half of fullscreen width.
rightAvgY = map(rightAvgY,180,420,0,height); //Done to map the detected average of the right half of 640x480 kinect screen height over to the right half of fullscreen height.
lastTimeRightHandMoved = millis(); // To calculate the last time hand was detected. For screensaver state
fill(255);
ellipse(rightAvgX, rightAvgY, 30, 30);
}
}
// Function to detect the movement all over the screen.
void hands() {
handDepth = kinect.getRawDepth();
handTotalPixels = 0;
handSumX = 0;
handSumY = 0;
for (int x = 180; x < kinect.width; x++) { //Vary these limits to change the width portion of the screen you want to analyse
for (int y = 180; y < 420; y++) { //Vary these limits to change the height portion of the screen you want to analyse
handOffset = x + y * kinect.width;
int d = handDepth[handOffset];
if (d > handMinThresh && d < handMaxThresh) {
handSumX += x;
handSumY += y;
handTotalPixels++;
}
}
}
handAvgX = handSumX / handTotalPixels;
handAvgY = handSumY / handTotalPixels;
if(!Float.isNaN(handAvgX) && !Float.isNaN(handAvgY)) { //To prevent calculation when the value is null
handAvgX = map(handAvgX,180,kinect.width,0,width); //Done to map the detected average of the 640x480 kinect screen width over to the fullscreen width.
handAvgY = map(handAvgY,180,420,0,height); //Done to map the detected average of the 640x480 kinect screen height over to the fullscreen height.
lastTimeHandMoved = millis(); // To calculate the last time hand was detected. For screensaver state
fill(255);
if(!(projectState == "art")){
ellipse(handAvgX, handAvgY, 30, 30); //Don't draw an ellipse for hand detection when the project state is art.
}
}
}