-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjust_kinect_algos_3.pde
270 lines (223 loc) · 5.77 KB
/
just_kinect_algos_3.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
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
import org.openkinect.freenect.*;
import org.openkinect.freenect2.*;
import org.openkinect.processing.*;
Kinect2 kinect2;
PImage depthImg;
int segCt = 1;
float segThresStart = 500;
float segThresIncr = 300;
float[] segThres = new float[segCt*2];
Segment[] segs = new Segment[segCt];
color[] segCols = {
color(255, 0, 0, 100),
color(255, 175, 0, 100),
color(255, 255, 0, 100),
color(0, 255, 0, 100),
color(0, 255, 255, 100),
color(0, 55, 255, 100),
color(135, 0, 255, 100)
};
int activeSegment;
void setup() {
background(255);
size(1024, 848, P2D);
kinect2 = new Kinect2(this);
kinect2.initDepth();
kinect2.initDevice();
depthImg = new PImage(kinect2.depthWidth, kinect2.depthHeight, ARGB);
float thresMin = segThresStart;
int tIdx = 0;
int colIdx = 0;
for (int i = 0; i < segCt; i++) {
if (i >= segCols.length) {
colIdx = 0;
}
segs[i] = new Segment(segCols[colIdx], thresMin, thresMin+segThresIncr);
segThres[tIdx] = thresMin;
segThres[tIdx+1] = thresMin+segThresIncr;
thresMin += segThresIncr;
tIdx += 2;
colIdx++;
}
activeSegment = 0;
}
void draw(){
fill(255, 100);
rect(0,0,width,height);
for (int i=0; i < segs.length; i++){
segs[i].newFrame();
}
depthImg.loadPixels();
int[] rawDepth = kinect2.getRawDepth();
for (int x = 0; x < kinect2.depthWidth; x++) {
for (int y = 0; y < kinect2.depthHeight; y++) {
int index = x + y * kinect2.depthWidth;
int p = rawDepth[index];
int segIdx = 0;
boolean setColor = false;
for (int j = 0; j < (segCt*2); j+=2) {
if (p >= segThres[j] && p < segThres[j+1]) {
segs[segIdx].updateForFrame(x,y);
depthImg.pixels[index] = segs[segIdx].sColor;
setColor = true;
}
segIdx++;
}
if (!setColor) {
depthImg.pixels[index] = color(255, 0);
}
}
}
for (int i=0; i < segs.length; i++){
segs[i].updateAfterWholeFrameChecked();
}
depthImg.updatePixels();
imageMode(CORNER);
image(depthImg, 0, 0, depthImg.width*2, depthImg.height*2);
activeSegment = findMaxIdx(segs);
fill(segs[activeSegment].sColor);
ellipse(200, 200, (activeSegment+1)*10, (activeSegment+1)*10);
PVector top = segs[activeSegment].getTop();
PVector btm = segs[activeSegment].getBtm();
PVector left = segs[activeSegment].getLeft();
PVector right = segs[activeSegment].getRight();
noFill();
//quad(top.x, top.y, right.x, right.y, btm.x, btm.y, left.x, left.y);
//draw box
rectMode(CORNER);
rect(left.x*2, top.y*2, right.x*2-left.x*2, btm.y*2-top.y*2);
PVector center = segs[activeSegment].getCenter();
ellipse(center.x*2, center.y*2, 10, 10);
}
int findMaxIdx(Segment[] segs) {
float max = segs[0].getTotalPixels();
int maxIdx = 0;
for (int i=1; i < segs.length; i++){
float num = segs[i].getTotalPixels();
if (num > max) {
max = num;
maxIdx = i;
}
}
return maxIdx;
}
class Segment {
color sColor;
float minDepth, maxDepth;
Record edges;
PVector center;
PVector centerOld;
float xDiff;
float yDiff;
float velocity;
boolean isActive;
float totalPixelsInSeg;
Segment(color col, float min, float max) {
sColor = col;
minDepth = min;
maxDepth = max;
edges = new Record();
isActive = false;
totalPixelsInSeg = 0;
centerOld = new PVector(0,0);
center = new PVector(0,0);
xDiff = 0;
yDiff = 0;
velocity = 0;
}
void newFrame() {
totalPixelsInSeg = 0;
centerOld.set(center);
edges.resetRecord();
}
void updateForFrame(float x, float y) {
edges.check(x,y);
totalPixelsInSeg+=1;
}
void updateAfterWholeFrameChecked() {
float cx = ((edges.highestXPt.x - edges.lowestXPt.x) / 2)+ edges.lowestXPt.x;
float cy = ((edges.highestYPt.y - edges.lowestYPt.y) / 2) + edges.lowestYPt.y;
center = new PVector(cx, cy);
velocity = dist(center.x, center.y, centerOld.x, centerOld.y);
xDiff = center.x - centerOld.x;
yDiff = center.y - centerOld.y;
}
void setIsActive(boolean active) {
isActive = active;
}
boolean getIsActive() {
return isActive;
}
PVector getTop() {
return edges.lowestYPt;
}
PVector getBtm() {
return edges.highestYPt;
}
PVector getRight() {
return edges.highestXPt;
}
PVector getLeft() {
return edges.lowestXPt;
}
PVector getCenter() {
return center;
}
float getVelocity() {
return velocity;
}
float getXDiff() {
return xDiff;
}
float getTotalPixels() {
return totalPixelsInSeg;
}
}
class Record {
float lowestY; //"top"
PVector lowestYPt;
float highestY; //"bottom"
PVector highestYPt;
float lowestX; // "left"
PVector lowestXPt;
float highestX; // "right"
PVector highestXPt;
Record() {
lowestY = kinect2.depthHeight;
lowestYPt = new PVector(0,0);
highestY = 0;
highestYPt = new PVector(0,0);
lowestX = kinect2.depthWidth;
lowestXPt = new PVector(0,0);
highestX = 0;
highestXPt = new PVector(0,0);
}
void check(float newX, float newY){
if (newY <= lowestY) {
lowestY = newY;
lowestYPt = new PVector(newX, newY);
}
if (newY > highestY) {
highestY = newY;
highestYPt = new PVector(newX, newY);
}
if (newX <= lowestX) {
lowestX = newX;
lowestXPt = new PVector(newX, newY);
}
if (newX > highestX) {
highestX = newX;
highestXPt = new PVector(newX, newY);
}
}
void resetRecord() {
lowestY = kinect2.depthHeight;
lowestYPt = new PVector(0,0);
highestY = 0;
highestYPt = new PVector(0,0);
lowestX = kinect2.depthWidth;
lowestXPt = new PVector(0,0);
highestX = 0;
highestXPt = new PVector(0,0);
}
}