Skip to content

Hand Tracking vs. Skeleton Tracking

February 6, 2012

I decided to improve on my hand tracking assignment so that it (roughly) tracks the angle of my hand and rotates the superimposed foam hand appropriately. I’ve included before and after footage above. Code below:

import SimpleOpenNI.*;
SimpleOpenNI kinect;

PImage handImage;

void setup() {
  kinect = new SimpleOpenNI(this);

  size(640, 480);

  handImage = loadImage("Hand.png");
void draw() {
  image(kinect.rgbImage(), 0, 0);
  IntVector userList = new IntVector();

  if (userList.size() &rt; 0) {
    int userId = userList.get(0);

    float rightArmAngle = getJointAngle(userId, SimpleOpenNI.SKEL_RIGHT_HAND, SimpleOpenNI.SKEL_RIGHT_ELBOW);
    PVector handPos = new PVector();
    kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, handPos);
    PVector convertedHandPos = new PVector();
    kinect.convertRealWorldToProjective(handPos, convertedHandPos);
    float newImageWidth = map(convertedHandPos.z, 500, 2000, handImage.width/6, handImage.width/12);
    float newImageHeight = map(convertedHandPos.z, 500, 2000, handImage.height/6, handImage.height/12);
    //float newImageWidth = convertedHandPos.z*0.1;
    //float newImageHeight = convertedHandPos.z*0.2;
    newImageWidth = constrain(newImageWidth, 75, 100);
      translate(convertedHandPos.x, convertedHandPos.y);
      image(handImage, -1 * (newImageWidth/2), 0 - (newImageHeight/2), newImageWidth, newImageHeight);

float getJointAngle(int userId, int jointID1, int jointID2) {
  PVector joint1 = new PVector();
  PVector joint2 = new PVector();
  kinect.getJointPositionSkeleton(userId, jointID1, joint1);
  kinect.getJointPositionSkeleton(userId, jointID2, joint2);
  return atan2(joint1.y-joint2.y, joint1.x-joint2.x);

// user-tracking callbacks:
void onNewUser(int userId) {
  kinect.startPoseDetection("Psi", userId);
  println("Started pose detection.");

void onEndCalibration(int userId, boolean successful) {
  if (successful) {
    println("User calibrated.");
  else {
    println("Failed to calibrate user.");
    kinect.startPoseDetection("Psi", userId);

void onStartPose(String pose, int userId) {
  println("Started pose for user.");
  kinect.requestCalibrationSkeleton(userId, true);

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: