-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectTracker.cpp
executable file
·297 lines (239 loc) · 9.92 KB
/
KinectTracker.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
287
288
289
290
291
292
293
294
295
296
297
//
// KinectTracker.cpp
// basicExample
//
// Created by Tangible Media Group on 2/16/14.
//
//
#include "KinectTracker.h"
void KinectTracker::setup(int pNearThreshold, int pFarThreshold, int pContourMinimumSize) {
if(kinect.numAvailableDevices() > 0) {
kinect.setRegistration(true); // enable depth->video image calibration
kinect.init();
kinect.open();
}
// print the intrinsic IR sensor values
if(kinect.isConnected()) {
ofLogNotice() << "sensor-emitter dist: " << kinect.getSensorEmitterDistance() << "cm";
ofLogNotice() << "sensor-camera dist: " << kinect.getSensorCameraDistance() << "cm";
ofLogNotice() << "zero plane pixel size: " << kinect.getZeroPlanePixelSize() << "mm";
ofLogNotice() << "zero plane dist: " << kinect.getZeroPlaneDistance() << "mm";
}
colorImg.allocate(kinect.width, kinect.height);
depthImg.allocate(kinect.width, kinect.height);
grayThreshNear.allocate(kinect.width, kinect.height);
grayThreshFar.allocate(kinect.width, kinect.height);
depthThreshed.allocate(kinect.width, kinect.height);
lastDepthThreshed.allocate(kinect.width, kinect.height);
depthThreshedDiff.allocate(kinect.width, kinect.height);
fbo.allocate(kinect.width*2, kinect.height, GL_RGB);
recordingImage.allocate(kinect.width*2, kinect.height, OF_IMAGE_COLOR);
playingImage.allocate(kinect.width*2, kinect.height, OF_IMAGE_COLOR);
mNearThreshold = pNearThreshold;
mFarThreshold = pFarThreshold;
mContourMinimumSize = pContourMinimumSize;
isCurrentlyRecording = false;
playFromRecording = false;
loadAlphaMaskAndPrepForCvProcessing();
}
//--------------------------------------------------------------
//
// Update
//
// If playing from a recording, we set
// the colorImg and depthImg from the video
// and the rest of the class continues to reference
// colorImg and depthImg, rather then needing to
// reference different images depending on the input.
//
//--------------------------------------------------------------
void KinectTracker::update() {
kinect.update();
// there is a new frame and we are connected
if(kinect.isFrameNew() || playFromRecording) {
// update from kinect OR recroding depending on mode
if(playFromRecording) updateImagesFromRecording();
else updateImagesFromKinect();
lastDepthThreshed.setFromPixels(depthThreshed.getPixels(), kinect.width, kinect.height);
// always update the depth image
depthThreshed.setFromPixels(depthImg.getPixels(), kinect.width, kinect.height);
// updates a buffer for the recorder
// if we are recording
if(isCurrentlyRecording) {
fbo.begin();
ofBackground(0);
kinect.draw(0, 0, 640, 480);
kinect.drawDepth(640, 0, 640, 480);
fbo.end();
fbo.readToPixels(ofPixels);
recordingImage.setFromPixels(ofPixels);
}
// subtract mask which is png alpha image called "mask.png"
if(useMask) subtractMask();
// threshold calcutations convery depth map into black and white images
calculateThresholdsAndModifyImages();
// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.
// if find holes is set to true, we will get interior contours as well.
contourFinder.findContours(depthImg, mContourMinimumSize, (kinect.width*kinect.height)/2, 10, false);
depthThreshedDiff.absDiff(lastDepthThreshed, depthThreshed);
}
}
// we are using kinect
void KinectTracker::updateImagesFromKinect() {
colorImg.setFromPixels(kinect.getPixels(), kinect.width, kinect.height);
depthImg.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);
}
// we are using recording
void KinectTracker::updateImagesFromRecording() {
// video will be double wide, grab each side
imageLeft.cropFrom(playingImage, 0, 0, 640, 480);
imageRight.cropFrom(playingImage, 640, 0, 640, 480);
// @todo this is lossy we should convery in another way
// might cause pixelation around the edges of black / white bg
imageRight.setImageType(OF_IMAGE_GRAYSCALE);
colorImg.setFromPixels(imageLeft.getPixelsRef());
depthImg.setFromPixels(imageRight.getPixelsRef());
}
// used by the player class to set the current frame from now playing movie
void KinectTracker::updateCurrentFrame(unsigned char * pixels, int w, int h) {
playingImage.setFromPixels(pixels, w, h, OF_IMAGE_COLOR);
}
void KinectTracker::flagImagesAsChanged() {
colorImg.flagImageChanged();
depthImg.flagImageChanged();
depthThreshed.flagImageChanged();
}
void KinectTracker::subtractMask() {
cvAnd(depthImg.getCvImage(), maskCv.getCvImage(), depthImg.getCvImage(), NULL);
//cvAnd(grayThreshNear.getCvImage(), grayThreshFar.getCvImage(), depthImg.getCvImage(), NULL);
}
// loads png mask and converts to cv grayscale which we need to cvAnd method
void KinectTracker::loadAlphaMaskAndPrepForCvProcessing() {
// type is OF_IMAGE_COLOR_ALPHA
mask.loadImage("mask.png");
// simple way to convert to differe image type,
// changing the transparent areas to white
ofImage image;
image.setFromPixels(mask.getPixelsRef());
image.setImageType(OF_IMAGE_COLOR);
maskColorCv.setFromPixels(image.getPixels(), mask.getWidth(), mask.getHeight());
maskCv = maskColorCv;
}
void KinectTracker::calculateThresholdsAndModifyImages() {
depthImg.erode_3x3();
depthImg.dilate_3x3();
// we do two thresholds - one for the far plane and one for the near plane
// we then do a cvAnd to get the pixels which are a union of the two thresholds
grayThreshNear = depthImg;
grayThreshFar = depthImg;
grayThreshNear.threshold(mNearThreshold, true);
grayThreshFar.threshold(mFarThreshold);
cvAnd(grayThreshNear.getCvImage(), grayThreshFar.getCvImage(), depthImg.getCvImage(), NULL);
// find depth map excluding thresholded data
// this causes the 10 finger effect and could be related to our discussion
// today about dynamic thresholding
//
// if we threshold with the near value, and the user moves the hand just past the near point
// and thus out of range
// their hand will be black (since black is used for out of range areas)
// however since their hands shadow is also black this will cause the 10 finger effect.
//
//cvAnd(grayThreshNear.getCvImage(), depthThreshed.getCvImage(), depthThreshed.getCvImage(), NULL);
cvAnd(grayThreshFar.getCvImage(), depthThreshed.getCvImage(), depthThreshed.getCvImage(), NULL);
// ofPixelsRef depthPixels = depthThreshed.getPixelsRef();
// for (int x = 0; x < depthPixels.getWidth(); x++) {
// for (int y = 0; y < depthPixels.getHeight(); y++) {
// depthPixels.setColor((depthPixels.getColor(x,y).getBrightness() + mFarThreshold) * 255.f / (mNearThreshold - mFarThreshold));
// }
// }
}
//--------------------------------------------------------------
//
// Draw various images
//
//--------------------------------------------------------------
// color image
void KinectTracker::drawColorImage(int x, int y, int width, int height) {
ofSetColor(255);
colorImg.draw(x, y, width, height);
}
// gray image with contour blobs drawn on top
void KinectTracker::drawThresholdImage(int x, int y, int width, int height){
ofSetColor(255);
depthImg.draw(x, y, width, height);
contourFinder.draw(x, y, width, height);
}
// draw from the live kinect
void KinectTracker::drawDepthImage(int x, int y, int width, int height){
ofSetColor(255);
depthImg.draw(x, y, width, height);
}
// black and white image from within threshold range
void KinectTracker::drawDepthThreshedImage(int x, int y, int width, int height) {
ofSetColor(255);
depthThreshed.draw(x, y, width, height);
}
// black and white difference image from within threshold range
// (only data from movements)
void KinectTracker::drawDepthThreshedDiff(int x, int y, int width, int height) {
ofSetColor(255);
depthThreshedDiff.draw(x,y, width, height);
}
//--------------------------------------------------------------
//
// Get various kinect images as pixels
//
//--------------------------------------------------------------
// standard color image from kinect video camera
unsigned char * KinectTracker::getColorPixels() {
return colorImg.getPixels();
}
// kinect depth map
// grayscale image where white = near, black = far
unsigned char * KinectTracker::getDepthPixels() {
return kinect.getDepthPixels();
}
// kinect depth map WHERE
// areas outside near and far threshold range are black.
// This is currently used by the wave animation
unsigned char * KinectTracker::depthThreshedPixels() {
return depthThreshed.getPixels();
}
// kinect depth map WHERE
// areas outside near and far threshold range are black.
// This is currently used by the wave animation
ofPixels KinectTracker::depthThresholdOFPixels() {
return depthThreshed.getPixelsRef();
}
// kinect depth map WHERE
// only black OR white (0 OR 255) is used.
// calculated using using pContourMinimumSize
unsigned char * KinectTracker::grayImagePixels() {
return depthImg.getPixels();
}
// returns pixels
// color image is on left and depth is on right
unsigned char * KinectTracker::getRecordingPixels() {
return recordingImage.getPixels();
}
// get a reference to the contour finder
ofxCvContourFinder * KinectTracker::getContourFinder() {
return &contourFinder;
}
//--------------------------------------------------------------
//
// Kinect pass through getters
//
// Since we wrap the kinect class we no longer have access to
// its methods unless we add them to KinectTracker too.
//
//--------------------------------------------------------------
int KinectTracker::numAvailableDevices(){
return kinect.numAvailableDevices();
}
bool KinectTracker::isFrameNew() {
return kinect.isFrameNew();
}
bool KinectTracker::isConnected() {
return kinect.isConnected();
}