This repository was archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathskeltrack-skeleton.c
2035 lines (1753 loc) · 66.2 KB
/
skeltrack-skeleton.c
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* skeltrack-skeleton.c
*
* Skeltrack - A Free Software skeleton tracking library
* Copyright (C) 2012 Igalia S.L.
* Copyright (C) 2013 Joaquim Rocha
*
* Authors:
* Joaquim Rocha <me@joaquimrocha.com>
* Eduardo Lima Mitev <elima@igalia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License at http://www.gnu.org/licenses/lgpl-3.0.txt
* for more details.
*/
/**
* SECTION:skeltrack-skeleton
* @short_description: Object that tracks the joints in a human skeleton
*
* This object tries to detect joints of the human skeleton.
*
* To track the joints, first create an instance of #SkeltrackSkeleton using
* skeltrack_skeleton_new() and then set a buffer from where the joints will
* be retrieved using the asynchronous function
* skeltrack_skeleton_track_joints() and get the list of joints using
* skeltrack_skeleton_track_joints_finish().
*
* A common use case is to use this library together with a Kinect device so
* an easy way to retrieve the needed buffer is to use the GFreenect library.
*
* It currently tracks the joints identified by #SkeltrackJointId .
*
* Tracking the skeleton joints can be computational heavy so it is advised that
* the given buffer's dimension is reduced before setting it. To do it,
* simply choose the reduction factor and loop through the original buffer
* (using this factor as a step) and set the reduced buffer's values accordingly.
* The #SkeltrackSkeleton:dimension-reduction property holds this reduction
* value and should be changed to the reduction factor used (alternatively you
* can retrieve its default value and use it in the reduction, if it fits your
* needs).
*
* The skeleton tracking uses a few heuristics that proved to work well for
* tested cases but they can be tweaked by changing the following properties:
* #SkeltrackSkeleton:graph-distance-threshold ,
* #SkeltrackSkeleton:graph-minimum-number-nodes ,
* #SkeltrackSkeleton:hands-minimum-distance ,
* #SkeltrackSkeleton:shoulders-arc-start-point ,
* #SkeltrackSkeleton:shoulders-arc-length ,
* #SkeltrackSkeleton:shoulders-circumference-radius ,
* #SkeltrackSkeleton:shoulders-search-step .
**/
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "skeltrack-skeleton.h"
#include "skeltrack-smooth.h"
#include "skeltrack-util.h"
#define SKELTRACK_SKELETON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
SKELTRACK_TYPE_SKELETON, \
SkeltrackSkeletonPrivate))
#define DIMENSION_REDUCTION 16
#define GRAPH_DISTANCE_THRESHOLD 150
#define GRAPH_MINIMUM_NUMBER_OF_NODES 5
#define HANDS_MINIMUM_DISTANCE 550
#define SHOULDERS_CIRCUMFERENCE_RADIUS 300
#define SHOULDERS_ARC_START_POINT 100
#define SHOULDERS_ARC_LENGTH 250
#define SHOULDERS_SEARCH_STEP 0.05
#define JOINTS_PERSISTENCY_DEFAULT 3
#define SMOOTHING_FACTOR_DEFAULT .5
#define ENABLE_SMOOTHING_DEFAULT TRUE
#define DEFAULT_FOCUS_POINT_Z 1000
#define TORSO_MINIMUM_NUMBER_NODES_DEFAULT 16.0
#define EXTREMA_SPHERE_RADIUS 300
/* private data */
struct _SkeltrackSkeletonPrivate
{
guint16 *buffer;
guint buffer_width;
guint buffer_height;
GAsyncResult *track_joints_result;
GMutex track_joints_mutex;
GList *graph;
GList *labels;
Node **node_matrix;
gint *distances_matrix;
GList *main_component;
guint16 dimension_reduction;
guint16 distance_threshold;
guint16 min_nr_nodes;
guint16 hands_minimum_distance;
guint16 shoulders_circumference_radius;
guint16 shoulders_arc_start_point;
guint16 shoulders_arc_length;
gfloat shoulders_search_step;
guint16 extrema_sphere_radius;
Node *focus_node;
gboolean enable_smoothing;
SmoothData smooth_data;
gfloat torso_minimum_number_nodes;
SkeltrackJoint *previous_head;
};
/* Currently searches for head and hands */
static const guint NR_EXTREMAS_TO_SEARCH = 3;
/* properties */
enum
{
PROP_0,
PROP_DIMENSION_REDUCTION,
PROP_GRAPH_DISTANCE_THRESHOLD,
PROP_GRAPH_MIN_NR_NODES,
PROP_HANDS_MINIMUM_DISTANCE,
PROP_SHOULDERS_CIRCUMFERENCE_RADIUS,
PROP_SHOULDERS_ARC_START_POINT,
PROP_SHOULDERS_ARC_LENGTH,
PROP_SHOULDERS_SEARCH_STEP,
PROP_EXTREMA_SPHERE_RADIUS,
PROP_SMOOTHING_FACTOR,
PROP_JOINTS_PERSISTENCY,
PROP_ENABLE_SMOOTHING,
PROP_TORSO_MINIMUM_NUMBER_NODES
};
static void skeltrack_skeleton_class_init (SkeltrackSkeletonClass *class);
static void skeltrack_skeleton_init (SkeltrackSkeleton *self);
static void skeltrack_skeleton_finalize (GObject *obj);
static void skeltrack_skeleton_dispose (GObject *obj);
static void skeltrack_skeleton_set_property (GObject *obj,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void skeltrack_skeleton_get_property (GObject *obj,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void clean_tracking_resources (SkeltrackSkeleton *self);
G_DEFINE_TYPE (SkeltrackSkeleton, skeltrack_skeleton, G_TYPE_OBJECT)
static void
skeltrack_skeleton_class_init (SkeltrackSkeletonClass *class)
{
GObjectClass *obj_class;
obj_class = G_OBJECT_CLASS (class);
obj_class->dispose = skeltrack_skeleton_dispose;
obj_class->finalize = skeltrack_skeleton_finalize;
obj_class->get_property = skeltrack_skeleton_get_property;
obj_class->set_property = skeltrack_skeleton_set_property;
/* install properties */
/**
* SkeltrackSkeleton:dimension-reduction:
*
* The value by which the dimension of the buffer was reduced
* (in case it was).
**/
g_object_class_install_property (obj_class,
PROP_DIMENSION_REDUCTION,
g_param_spec_uint ("dimension-reduction",
"Dimension reduction",
"The dimension reduction value",
1,
1024,
DIMENSION_REDUCTION,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:graph-distance-threshold:
*
* The value (in mm) for the distance threshold between each node and its
* neighbors. This means that a node in the graph will only be connected
* to another if they aren't farther apart then this value.
**/
g_object_class_install_property (obj_class,
PROP_GRAPH_DISTANCE_THRESHOLD,
g_param_spec_uint ("graph-distance-threshold",
"Graph's distance threshold",
"The distance threshold between "
"each node.",
1,
G_MAXUINT16,
GRAPH_DISTANCE_THRESHOLD,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:graph-minimum-number-nodes:
*
* The minimum number of nodes each of the graph's components
* should have (when it is not fully connected).
**/
g_object_class_install_property (obj_class,
PROP_GRAPH_MIN_NR_NODES,
g_param_spec_uint ("graph-minimum-number-nodes",
"Graph's minimum number of nodes",
"The minimum number of nodes "
"of the graph's components ",
1,
G_MAXUINT16,
GRAPH_MINIMUM_NUMBER_OF_NODES,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:hands-minimum-distance:
*
* The minimum distance (in mm) that each hand should be from its
* respective shoulder.
**/
g_object_class_install_property (obj_class,
PROP_HANDS_MINIMUM_DISTANCE,
g_param_spec_uint ("hands-minimum-distance",
"Hands' minimum distance from the "
"shoulders",
"The minimum distance (in mm) that "
"each hand should be from its "
"respective shoulder.",
300,
G_MAXUINT,
HANDS_MINIMUM_DISTANCE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:shoulders-circumference-radius:
*
* The radius of the circumference (in mm) from the head with which
* to look for the shoulders.
**/
g_object_class_install_property (obj_class,
PROP_SHOULDERS_CIRCUMFERENCE_RADIUS,
g_param_spec_uint ("shoulders-circumference-radius",
"Shoulders' circumference radius",
"The radius of the circumference "
"(in mm) from the head with which "
"to look for the shoulders.",
1,
G_MAXUINT16,
SHOULDERS_CIRCUMFERENCE_RADIUS,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:shoulders-arc-start-point:
*
* The starting point (in mm) of the arc (from the bottom of the
* shoulders' circumference) where the shoulders will be searched for.
* This point is used together with the
* SkeltrackSkeleton::shoulders-arc-length to determine the arc
* where the shoulders' points will be looked for.
**/
g_object_class_install_property (obj_class,
PROP_SHOULDERS_ARC_START_POINT,
g_param_spec_uint ("shoulders-arc-start-point",
"Shoulders' arc start point",
"The starting point (in mm) of the "
"arc from the bottom of the "
"shoulders' circumference where "
"the shoulders will be searched for.",
1,
G_MAXUINT16,
SHOULDERS_ARC_START_POINT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:shoulders-arc-length:
*
* The length (in mm) of the arc where the shoulders will be searched.
* This length is used together with the
* SkeltrackSkeleton::shoulders-arc-start-point to determine the arc
* where the shoulders' points will be looked for.
**/
g_object_class_install_property (obj_class,
PROP_SHOULDERS_ARC_LENGTH,
g_param_spec_uint ("shoulders-arc-length",
"Shoulders' arc length",
"The length (in mm) of the arc "
"where the shoulders will be "
"searched.",
1,
G_MAXUINT16,
SHOULDERS_ARC_LENGTH,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:shoulders-search-step:
*
* The step considered for sampling the shoulders' circumference
* when searching for the shoulders.
**/
g_object_class_install_property (obj_class,
PROP_SHOULDERS_SEARCH_STEP,
g_param_spec_float ("shoulders-search-step",
"Shoulders' search step",
"The step considered for sampling "
"the shoulders' circumference "
"when searching for the shoulders.",
.01,
M_PI,
.01,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:smoothing-factor:
*
* The factor by which the joints should be smoothed. This refers to
* Holt's Double Exponential Smoothing and determines how the current and
* previous data and trend will be used. A value closer to 0 will produce smoother
* results but increases latency.
**/
g_object_class_install_property (obj_class,
PROP_SMOOTHING_FACTOR,
g_param_spec_float ("smoothing-factor",
"Smoothing factor",
"The factor by which the joints values"
"should be smoothed.",
.0,
1.0,
.5,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:joints-persistency:
*
* The number of times that a joint can be null until its previous
* value is discarded. For example, if this property is 3, the last value for
* a joint will keep being used until the new value for this joint is null for
* 3 consecutive times.
*
**/
g_object_class_install_property (obj_class,
PROP_JOINTS_PERSISTENCY,
g_param_spec_uint ("joints-persistency",
"Joints persistency",
"The number of times that a joint "
"can be null until its previous "
"value is discarded",
0,
G_MAXUINT16,
JOINTS_PERSISTENCY_DEFAULT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:enable-smoothing:
*
* Whether smoothing the joints should be applied or not.
*
**/
g_object_class_install_property (obj_class,
PROP_ENABLE_SMOOTHING,
g_param_spec_boolean ("enable-smoothing",
"Enable smoothing",
"Whether smoothing should be "
"applied or not",
ENABLE_SMOOTHING_DEFAULT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:torso-minimum-number-nodes:
*
* Minimum number of nodes for a component to be considered torso.
*
**/
g_object_class_install_property (obj_class,
PROP_TORSO_MINIMUM_NUMBER_NODES,
g_param_spec_float ("torso-minimum-number-nodes",
"Torso minimum number of nodes",
"Minimum number of nodes for a "
"component to be considered "
"torso",
0,
G_MAXUINT16,
TORSO_MINIMUM_NUMBER_NODES_DEFAULT,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/**
* SkeltrackSkeleton:extrema-sphere-radius:
*
* The radius of the sphere around the extremas (in mm).
*
* Points inside this sphere are considered for calculating the average position
* of the extrema. If the value is 0, no averaging is done.
**/
g_object_class_install_property (obj_class,
PROP_EXTREMA_SPHERE_RADIUS,
g_param_spec_uint ("extrema-sphere-radius",
"Extrema sphere radius",
"The radius of the sphere around "
"the extremas (in mm).",
0,
G_MAXUINT16,
EXTREMA_SPHERE_RADIUS,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/* add private structure */
g_type_class_add_private (obj_class, sizeof (SkeltrackSkeletonPrivate));
}
static void
skeltrack_skeleton_init (SkeltrackSkeleton *self)
{
guint i;
SkeltrackSkeletonPrivate *priv;
priv = SKELTRACK_SKELETON_GET_PRIVATE (self);
self->priv = priv;
priv->buffer = NULL;
priv->buffer_width = 0;
priv->buffer_height = 0;
priv->graph = NULL;
priv->labels = NULL;
priv->main_component = NULL;
priv->node_matrix = NULL;
priv->distances_matrix = NULL;
priv->dimension_reduction = DIMENSION_REDUCTION;
priv->distance_threshold = GRAPH_DISTANCE_THRESHOLD;
priv->min_nr_nodes = GRAPH_MINIMUM_NUMBER_OF_NODES;
priv->hands_minimum_distance = HANDS_MINIMUM_DISTANCE;
priv->shoulders_circumference_radius = SHOULDERS_CIRCUMFERENCE_RADIUS;
priv->shoulders_arc_start_point = SHOULDERS_ARC_START_POINT;
priv->shoulders_arc_length = SHOULDERS_ARC_LENGTH;
priv->shoulders_search_step = SHOULDERS_SEARCH_STEP;
priv->extrema_sphere_radius = EXTREMA_SPHERE_RADIUS;
priv->focus_node = g_slice_new0 (Node);
priv->focus_node->x = 0;
priv->focus_node->y = 0;
priv->focus_node->z = DEFAULT_FOCUS_POINT_Z;
priv->track_joints_result = NULL;
g_mutex_init (&priv->track_joints_mutex);
priv->enable_smoothing = ENABLE_SMOOTHING_DEFAULT;
priv->smooth_data.smoothing_factor = SMOOTHING_FACTOR_DEFAULT;
priv->smooth_data.smoothed_joints = NULL;
priv->smooth_data.trend_joints = NULL;
priv->smooth_data.joints_persistency = JOINTS_PERSISTENCY_DEFAULT;
for (i = 0; i < SKELTRACK_JOINT_MAX_JOINTS; i++)
priv->smooth_data.joints_persistency_counter[i] = JOINTS_PERSISTENCY_DEFAULT;
priv->torso_minimum_number_nodes = TORSO_MINIMUM_NUMBER_NODES_DEFAULT;
priv->previous_head = NULL;
}
static void
skeltrack_skeleton_dispose (GObject *obj)
{
/* TODO: cancel any cancellable to interrupt joints tracking operation */
G_OBJECT_CLASS (skeltrack_skeleton_parent_class)->dispose (obj);
}
static void
skeltrack_skeleton_finalize (GObject *obj)
{
SkeltrackSkeleton *self = SKELTRACK_SKELETON (obj);
g_mutex_clear (&self->priv->track_joints_mutex);
skeltrack_joint_list_free (self->priv->smooth_data.smoothed_joints);
skeltrack_joint_list_free (self->priv->smooth_data.trend_joints);
skeltrack_joint_free (self->priv->previous_head);
clean_tracking_resources (self);
g_slice_free (Node, self->priv->focus_node);
G_OBJECT_CLASS (skeltrack_skeleton_parent_class)->finalize (obj);
}
static void
skeltrack_skeleton_set_property (GObject *obj,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
SkeltrackSkeleton *self;
self = SKELTRACK_SKELETON (obj);
switch (prop_id)
{
case PROP_DIMENSION_REDUCTION:
self->priv->dimension_reduction = g_value_get_uint (value);
break;
case PROP_GRAPH_DISTANCE_THRESHOLD:
self->priv->distance_threshold = g_value_get_uint (value);
break;
case PROP_GRAPH_MIN_NR_NODES:
self->priv->min_nr_nodes = g_value_get_uint (value);
break;
case PROP_HANDS_MINIMUM_DISTANCE:
self->priv->hands_minimum_distance = g_value_get_uint (value);
break;
case PROP_SHOULDERS_CIRCUMFERENCE_RADIUS:
self->priv->shoulders_circumference_radius = g_value_get_uint (value);
break;
case PROP_SHOULDERS_ARC_START_POINT:
self->priv->shoulders_arc_start_point = g_value_get_uint (value);
break;
case PROP_SHOULDERS_ARC_LENGTH:
self->priv->shoulders_arc_length = g_value_get_uint (value);
break;
case PROP_SHOULDERS_SEARCH_STEP:
self->priv->shoulders_search_step = g_value_get_float (value);
break;
case PROP_EXTREMA_SPHERE_RADIUS:
self->priv->extrema_sphere_radius = g_value_get_uint (value);
break;
case PROP_SMOOTHING_FACTOR:
self->priv->smooth_data.smoothing_factor = g_value_get_float (value);
break;
case PROP_JOINTS_PERSISTENCY:
self->priv->smooth_data.joints_persistency = g_value_get_uint (value);
reset_joints_persistency_counter (&self->priv->smooth_data);
break;
case PROP_ENABLE_SMOOTHING:
self->priv->enable_smoothing = g_value_get_boolean (value);
break;
case PROP_TORSO_MINIMUM_NUMBER_NODES:
self->priv->torso_minimum_number_nodes = g_value_get_float (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
skeltrack_skeleton_get_property (GObject *obj,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
SkeltrackSkeleton *self;
self = SKELTRACK_SKELETON (obj);
switch (prop_id)
{
case PROP_DIMENSION_REDUCTION:
g_value_set_uint (value, self->priv->dimension_reduction);
break;
case PROP_GRAPH_DISTANCE_THRESHOLD:
g_value_set_uint (value, self->priv->distance_threshold);
break;
case PROP_GRAPH_MIN_NR_NODES:
g_value_set_uint (value, self->priv->min_nr_nodes);
break;
case PROP_HANDS_MINIMUM_DISTANCE:
g_value_set_uint (value, self->priv->hands_minimum_distance);
break;
case PROP_SHOULDERS_CIRCUMFERENCE_RADIUS:
g_value_set_uint (value, self->priv->shoulders_circumference_radius);
break;
case PROP_SHOULDERS_ARC_START_POINT:
g_value_set_uint (value, self->priv->shoulders_arc_start_point);
break;
case PROP_SHOULDERS_ARC_LENGTH:
g_value_set_uint (value, self->priv->shoulders_arc_length);
break;
case PROP_SHOULDERS_SEARCH_STEP:
g_value_set_float (value, self->priv->shoulders_search_step);
break;
case PROP_EXTREMA_SPHERE_RADIUS:
g_value_set_uint (value, self->priv->extrema_sphere_radius);
break;
case PROP_SMOOTHING_FACTOR:
g_value_set_float (value, self->priv->smooth_data.smoothing_factor);
break;
case PROP_JOINTS_PERSISTENCY:
g_value_set_uint (value, self->priv->smooth_data.joints_persistency);
break;
case PROP_ENABLE_SMOOTHING:
g_value_set_boolean (value, self->priv->enable_smoothing);
break;
case PROP_TORSO_MINIMUM_NUMBER_NODES:
g_value_set_float (value, self->priv->torso_minimum_number_nodes);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static gint
join_neighbor (SkeltrackSkeleton *self,
Node *node,
Label **neighbor_labels,
gint index,
gint i,
gint j)
{
Node *neighbor;
if (i < 0 || i >= self->priv->buffer_width ||
j < 0 || j >= self->priv->buffer_height)
{
return index;
}
neighbor = self->priv->node_matrix[self->priv->buffer_width * j + i];
if (neighbor != NULL)
{
gint distance;
distance = get_distance (neighbor, node);
if (distance < self->priv->distance_threshold)
{
neighbor->neighbors = g_list_prepend (neighbor->neighbors,
node);
node->neighbors = g_list_prepend (node->neighbors,
neighbor);
neighbor_labels[index] = neighbor->label;
index++;
}
}
return index;
}
GList *
make_graph (SkeltrackSkeleton *self, GList **label_list)
{
SkeltrackSkeletonPrivate *priv;
gint i, j;
Node *node;
GList *nodes = NULL;
GList *labels = NULL;
GList *current_label;
GList *current_node;
Label *main_component_label = NULL;
gint index = 0;
gint next_label = -1;
guint16 value;
guint16 *buffer;
gint width, height;
buffer = self->priv->buffer;
width = self->priv->buffer_width;
height = self->priv->buffer_height;
priv = self->priv;
if (priv->node_matrix == NULL)
{
priv->node_matrix = g_slice_alloc0 (width * height * sizeof (Node *));
}
else
{
memset (self->priv->node_matrix,
0,
width * height * sizeof (Node *));
}
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
gint south, north, west;
Label *lowest_index_label = NULL;
Label *neighbor_labels[4] = {NULL, NULL, NULL, NULL};
value = buffer[j * width + i];
if (value == 0)
continue;
node = g_slice_new0 (Node);
node->i = i;
node->j = j;
node->z = value;
convert_screen_coords_to_mm (self->priv->buffer_width,
self->priv->buffer_height,
self->priv->dimension_reduction,
i, j,
node->z,
&(node->x),
&(node->y));
node->neighbors = NULL;
node->linked_nodes = NULL;
index = 0;
south = j + 1;
north = j - 1;
west = i - 1;
/* West */
index = join_neighbor (self,
node,
neighbor_labels,
index,
west, j);
/* South West*/
index = join_neighbor (self,
node,
neighbor_labels,
index,
west, south);
/* North */
index = join_neighbor (self,
node,
neighbor_labels,
index,
i, north);
/* North West */
index = join_neighbor (self,
node,
neighbor_labels,
index,
west, north);
lowest_index_label = get_lowest_index_label (neighbor_labels);
/* No neighbors */
if (lowest_index_label == NULL)
{
Label *label;
next_label++;
label = new_label (next_label);
labels = g_list_prepend (labels, label);
lowest_index_label = label;
}
else
{
for (index = 0; index < 4; index++)
{
if (neighbor_labels[index] != NULL)
{
label_union (neighbor_labels[index], lowest_index_label);
}
}
}
node->label = lowest_index_label;
nodes = g_list_prepend(nodes, node);
priv->node_matrix[width * node->j + node->i] = node;
}
}
for (current_node = g_list_first (nodes);
current_node != NULL;
current_node = g_list_next (current_node))
{
Node *node = (Node *) current_node->data;
node->label = label_find (node->label);
node->label->nodes = g_list_prepend (node->label->nodes,
node);
/* Assign lower node so we can extract the
lower graph's component */
if (node->label->lower_screen_y == -1 ||
node->j > node->label->lower_screen_y)
{
node->label->lower_screen_y = node->j;
}
/* Assign farther to the camera node so we
can extract the main graph component */
if (node->label->higher_z == -1 ||
node->z > node->label->higher_z)
{
node->label->higher_z = node->z;
}
/* Assign closer to the camera node so we
can extract the main graph component */
if (node->label->lower_z == -1 ||
node->z < node->label->lower_z)
{
node->label->lower_z = node->z;
}
}
for (current_label = g_list_first (labels);
current_label != NULL;
current_label = g_list_next (current_label))
{
Label *label;
GList *current_nodes;
label = (Label *) current_label->data;
current_nodes = label->nodes;
label->normalized_num_nodes = g_list_length (current_nodes) *
((label->higher_z - label->lower_z)/2 +
label->lower_z) *
(pow (DIMENSION_REDUCTION, 2)/2) /
1000000;
}
main_component_label = get_main_component (nodes,
priv->focus_node,
priv->torso_minimum_number_nodes);
current_label = g_list_first (labels);
while (current_label != NULL)
{
Label *label;
label = (Label *) current_label->data;
/* Remove label if number of nodes is less than
the minimum required */
if (g_list_length (label->nodes) < priv->min_nr_nodes)
{
nodes = remove_nodes_with_label (nodes,
priv->node_matrix,
priv->buffer_width,
label);
GList *link = current_label;
current_label = g_list_next (current_label);
labels = g_list_delete_link (labels, link);
free_label (label);
continue;
}
current_label = g_list_next (current_label);
}
if (main_component_label)
{
join_components_to_main (labels,
main_component_label,
priv->distance_threshold,
priv->hands_minimum_distance,
priv->distance_threshold);
current_label = g_list_first (labels);
while (current_label != NULL)
{
Label *label;
label = (Label *) current_label->data;
if (label == main_component_label)
{
current_label = g_list_next (current_label);
continue;
}
if (label->bridge_node == NULL)
{
nodes = remove_nodes_with_label (nodes,
priv->node_matrix,
priv->buffer_width,
label);
GList *link = current_label;
current_label = g_list_next (current_label);
labels = g_list_delete_link (labels, link);
free_label (label);
continue;
}
label->bridge_node->neighbors =
g_list_prepend (label->bridge_node->neighbors, label->to_node);
label->to_node->neighbors = g_list_prepend (label->to_node->neighbors,
label->bridge_node);
current_label = g_list_next (current_label);
}
priv->main_component = main_component_label->nodes;
}
*label_list = labels;
return nodes;
}
Node *
get_centroid (SkeltrackSkeleton *self)
{
gint avg_x = 0;
gint avg_y = 0;
gint avg_z = 0;
gint length;
GList *node_list;
Node *cent = NULL;
Node *centroid = NULL;
if (self->priv->main_component == NULL)
return NULL;
for (node_list = g_list_first (self->priv->main_component);
node_list != NULL;
node_list = g_list_next (node_list))
{
Node *node;
node = (Node *) node_list->data;
avg_x += node->x;
avg_y += node->y;
avg_z += node->z;
}
length = g_list_length (self->priv->main_component);
cent = g_slice_new0 (Node);
cent->x = avg_x / length;
cent->y = avg_y / length;
cent->z = avg_z / length;
cent->linked_nodes = NULL;
centroid = get_closest_node (self->priv->graph, cent);
g_slice_free (Node, cent);
return centroid;
}
static Node *
get_lowest (SkeltrackSkeleton *self, Node *centroid)
{
Node *lowest = NULL;
/* @TODO: Use the node_matrix instead of the lowest
component to look for the lowest node as it's faster. */
if (self->priv->main_component != NULL)
{
GList *node_list;
for (node_list = g_list_first (self->priv->main_component);
node_list != NULL;
node_list = g_list_next (node_list))
{
Node *node;
node = (Node *) node_list->data;