-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKinnect.m
More file actions
278 lines (233 loc) · 9.15 KB
/
Kinnect.m
File metadata and controls
278 lines (233 loc) · 9.15 KB
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
//
// Kinnect.m
// Caffe
//
// Created by Nicholas Peretti on 5/3/16.
// Copyright © 2016 Venture Media. All rights reserved.
//
#import "Kinnect.h"
#import <libfreenect/libfreenect_sync.h>
@implementation Kinnect
{
NSImage *_depth;
int picindex;
NSSize croppedSize;
NSImage *_lastHD;
uint16_t *lastDepth;
}
-(instancetype)init
{
self = [super init];
if (self) {
picindex = 0;
croppedSize = NSMakeSize(64, 64);
}
return self;
}
-(uint16_t *)rawDepth
{
return lastDepth;
}
-(uint16_t *)justDepth
{
uint16_t *depthData = malloc(sizeof(uint16_t)*(640*480));
unsigned int timestampDepth;
freenect_sync_get_depth_with_res((void**)(&depthData), ×tampDepth, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM);
return depthData;
}
-(NSImage *)highRes
{
return _lastHD;
}
-(NSImage *)traceImages
{
int windowSize = 480;
float minDepth = 0.7;
float maxDepth = 1.5;
uint16_t *depthData = malloc(sizeof(uint16_t)*(640*480));
unsigned int timestampDepth;
freenect_sync_get_depth_with_res((void**)(&depthData), ×tampDepth, 0, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_MM);
lastDepth = depthData;
SCNVector3 vecs[windowSize*windowSize];
int left = 320-(windowSize/2);
int top = 240-(windowSize/2);
int totalx = 0;
int totaly = 0;
int points = 0;
int tip = 9999;
CGFloat minZ = 9999;
CGFloat maxZ = -99999;
for (int y = 0;y<windowSize;y++) {
for (int x = 0;x<windowSize;x++) {
int i = ((top+y)*640)+(left+x);
float depth = depthData[i];
SCNVector3 vec = [self world:depth x:i%640 y:(i/640)];
vecs[y*windowSize+x] = vec;
if (vec.z>minDepth&&vec.z<maxDepth) {
totalx+=x;
totaly+=y;
points++;
if (y<tip) {
tip = y;
}
}
}
}
int cropSize = 128;
UInt8 *data = malloc(cropSize*cropSize*3);
if (points>0) {
int offset = 0;
int averagX = totalx/points;
int averageY = tip+60;
if (averagX<cropSize/2) {
averagX=cropSize/2;
}
if (averagX>640-(cropSize/2)) {
averagX=640-(cropSize/2);
}
if (averageY<cropSize/2) {
averageY=cropSize/2;
}
if (averageY>480-(cropSize/2)) {
averageY=480-(cropSize/2);
}
int sleft = averagX-(cropSize/2);
int stop = averageY-(cropSize/2);
//CGFloat dictanceTotals = 0;
SCNVector3 newMidPoint = SCNVector3Make(0, 0, 0);
points = 0;
for (int y = 0;y<cropSize;y++) {
for (int x = 0;x<cropSize;x++) {
SCNVector3 vec = vecs[((stop+y)*windowSize)+(x+sleft)];
if (vec.z>minDepth&&vec.z<maxDepth) {
newMidPoint = vectorAdd(newMidPoint, vec);
points++;
minZ = vec.z<minZ?vec.z:minZ;
maxZ = vec.z>maxZ?vec.z:maxZ;
}
}
}
newMidPoint = SCNVector3Make(newMidPoint.x/points, newMidPoint.y/points, newMidPoint.z/points);
for (int y = 0;y<cropSize;y++) {
for (int x = 0;x<cropSize;x++) {
SCNVector3 vec = vecs[((stop+y)*windowSize)+(x+sleft)];
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
if (vec.z>minDepth&&vec.z<maxDepth) {
//float normDepth = (vec.z-minDepth)/(largestZ-minDepth);
CGFloat distance = lengthOf(vectorSubtract(vec,newMidPoint));
float normDistanc = distance/0.1;
[self hsv2RGB:normDistanc sat:1.0 val:1.0 red:&red green:&green blue:&blue];
}
data[offset++]= red;
data[offset++]= green;
data[offset++]= blue;
}
}
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CFDataRef rgbData = CFDataCreate(NULL, data, cropSize*cropSize*3);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);
CGImageRef rgbImageRef = CGImageCreate(cropSize, cropSize, 8, 24, cropSize * 3, colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);
CFRelease(rgbData);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorspace);
NSImage *videoFrame = [[NSImage alloc]initWithCGImage:rgbImageRef size:NSMakeSize(cropSize, cropSize)];
_lastHD = videoFrame;
NSImage *small = [self resizeImage:videoFrame size:croppedSize];
_depth = small;
CGImageRelease(rgbImageRef);
free(data);
}
return _depth;
}
- (NSImage*)resizeImage:(NSImage*)sourceImage size:(NSSize)size
{
NSRect targetFrame = NSMakeRect(0, 0, size.width, size.height);
NSImage* targetImage = [[NSImage alloc] initWithSize:size];
[targetImage lockFocus];
[sourceImage drawInRect:targetFrame
fromRect:NSZeroRect //portion of source image to draw
operation:NSCompositeCopy //compositing operation
fraction:1.0 //alpha (transparency) value
respectFlipped:YES //coordinate system
hints:@{NSImageHintInterpolation:
[NSNumber numberWithInt:NSImageInterpolationMedium]}];
[targetImage unlockFocus];
return targetImage;
}
-(SCNVector3)world:(float)depth x:(float)x y:(float)y
{
double fx_d = 1.0 / 5.9421434211923247e+02;
double fy_d = 1.0 / 5.9104053696870778e+02;
double cx_d = 3.3930780975300314e+02;
double cy_d = 2.4273913761751615e+02;
depth = depth/1000.0;
SCNVector3 result = SCNVector3Make((float)((x - cx_d) * depth * fx_d), (float)((y - cy_d) * depth * fy_d), (float)(depth));
return result;
}
-(void)hsv2RGB:(float)h sat:(float)s val:(float)v red:(uint8_t *)r green:(uint8_t *)g blue:(uint8_t *)b
{
NSColor *col = [NSColor colorWithCalibratedHue:h saturation:s brightness:v alpha:1.0];
CGFloat red,green,blue,alph;
[col getRed:&red green:&green blue:&blue alpha:&alph];
*r = (uint8_t)(red*255);
*g = (uint8_t)(green*255);
*b = (uint8_t)(blue*255);
}
-(void)addToTrainingSet:(int)index
{
NSRect rect = NSMakeRect(0, 0, [_depth size].width, [_depth size].height);
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:[_depth CGImageForProposedRect:&rect context:[NSGraphicsContext currentContext] hints:nil]];
[newRep setSize:[_depth size]]; // if you want the same resolution
//NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.25] forKey:NSImageCompressionFactor]; // any number betwwen 0 to 1
NSData *jpgdata = [newRep representationUsingType:NSJPEGFileType properties:@{}];
if (picindex%5==0) {
[jpgdata writeToFile:[[@"~/Developer/signs/validationData" stringByExpandingTildeInPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%i-%i.jpeg",index,picindex]] atomically:YES];
} else {
[jpgdata writeToFile:[[@"~/Developer/signs/trainingData" stringByExpandingTildeInPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%i-%i.jpeg",index,picindex]] atomically:YES];
}
picindex++;
}
-(void)addToTrainingSet:(NSImage *)image atIndex:(int)index
{
image = [self resizeImage:image size:NSMakeSize(64, 64)];
NSRect rect = NSMakeRect(0, 0, [image size].width, [image size].height);
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:[image CGImageForProposedRect:&rect context:[NSGraphicsContext currentContext] hints:nil]];
[newRep setSize:[image size]]; // if you want the same resolution
NSData *jpgdata = [newRep representationUsingType:NSJPEGFileType properties:@{}];
if (picindex%5==0) {
[jpgdata writeToFile:[[@"~/Developer/signs/validationData" stringByExpandingTildeInPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%i-%i.jpeg",index,picindex]] atomically:YES];
} else {
[jpgdata writeToFile:[[@"~/Developer/signs/trainingData" stringByExpandingTildeInPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%i-%i.jpeg",index,picindex]] atomically:YES];
}
picindex++;
}
-(NSImage *)depthImage
{
return _depth;
}
#pragma mark - Vector math
SCNVector3 crossProduct(SCNVector3 a, SCNVector3 b)
{
return SCNVector3Make(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}
SCNVector3 normalize(SCNVector3 v)
{
CGFloat len = sqrt(pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2));
return SCNVector3Make(v.x/len, v.y/len, v.z/len);
}
SCNVector3 vectorSubtract(SCNVector3 a, SCNVector3 b)
{
return SCNVector3Make(a.x-b.x, a.y-b.y, a.z-b.z);
}
SCNVector3 vectorAdd(SCNVector3 a, SCNVector3 b)
{
return SCNVector3Make(a.x+b.x, a.y+b.y, a.z+b.z);
}
CGFloat lengthOf(SCNVector3 v)
{
CGFloat len = sqrt(pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2));
return len;
}
@end