Loading...
Logo
Processing Forum


Copy code
  1. import SimpleOpenNI.*; 

  2. SimpleOpenNI kinect; 

  3. PImage head;


  4. PImage resultImage;
  5. color trans = color(0, 0, 0, 0);

  6. int[] userMap; // declare our images 

  7. boolean calibrated;


  8. void setup() {
  9.   kinect = new SimpleOpenNI(this);
  10.   kinect.setMirror(true);
  11.   kinect.enableDepth();
  12.   kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);

  13.   size(640, 480);

  14.   resultImage = new PImage(640, 480, ARGB);


  15.   head = loadImage("Head_red.png");
  16.   
  17.   
  18.   /////****

  19.   ////

  20. }

  21. void draw() {
  22.   kinect.update();
  23. ////


  24.   if (!calibrated) {
  25.     image(kinect.depthImage(), 0, 0);
  26.   }  

  27.   IntVector userList = new IntVector();
  28.   kinect.getUsers(userList);

  29.   if (userList.size() > 0) {
  30.     int userId = userList.get(0);

  31.     if ( kinect.isTrackingSkeleton(userId)) {

  32.       userMap = kinect.getUsersPixels(SimpleOpenNI.USERS_ALL);
  33.       for (int i =0; i < userMap.length; i++) {
  34.         // if the pixel is part of the user
  35.         if (userMap[i] != 0) {
  36.           // set the pixel to the color pixel
  37.           resultImage.pixels[i] = kinect.depthImage().pixels[i];
  38.         }
  39.         else {
  40.           //set it to the background
  41.           resultImage.pixels[i] = trans;
  42.         }
  43.       }
  44.       //update the pixel from the inner array to image
  45.       resultImage.updatePixels();
  46.       image(resultImage, 0, 0);

  47.       drawSkeleton(userId);
  48.     }

  49.    
  50.     PVector headPos = new PVector();
  51.     kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_HEAD, headPos);
  52.     kinect.convertRealWorldToProjective(headPos, headPos);
  53.     println(headPos.z);
  54.     float newImageWidth = map(headPos.z, 500, 2000, head.width, head.width/2);
  55.     float newImageHeight = map(headPos.z, 500, 2000, head.width, head.width/2);
  56.     image(head, headPos.x - newImageWidth/2 - 25, headPos.y - newImageHeight/2 - 50, newImageWidth, newImageHeight );
  57.     
  58.  
  59.     
  60.   }
  61.   //c
  62. }
  63. void drawSkeleton(int userId) {
  64.   stroke(0);
  65.   strokeWeight(25);

  66.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
  67.   //image(head, SimpleOpenNI.SKEL_HEAD.x - head.width/2, SimpleOpenNI.SKEL_HEAD.y - head.height);
  68.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
  69.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
  70.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
  71.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
  72.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
  73.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
  74.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  75.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  76.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
  77.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
  78.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
  79.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
  80.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
  81.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
  82.   kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_LEFT_HIP);

  83.   noStroke();
  84.  fill(0, 255, 0);
  85.   /*
  86.   drawJoint(userId, SimpleOpenNI.SKEL_HEAD);  

  87.    drawJoint(userId, SimpleOpenNI.SKEL_NECK);
  88.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER);
  89.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
  90.    drawJoint(userId, SimpleOpenNI.SKEL_NECK);
  91.    drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
  92.    drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW);
  93.    drawJoint(userId, SimpleOpenNI.SKEL_TORSO);
  94.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
  95.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_KNEE);
  96.    drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_HIP);
  97.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_FOOT);
  98.    drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_KNEE);
  99.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
  100.    drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_FOOT);
  101.    drawJoint(userId, SimpleOpenNI.SKEL_RIGHT_HAND);
  102.    drawJoint(userId, SimpleOpenNI.SKEL_LEFT_HAND);
  103.    */
  104. }

  105. float getJointDistance(int userId, int jointID1, int jointID2) {
  106.   PVector joint1 = new PVector();
  107.   PVector joint2 = new PVector();
  108.   kinect.getJointPositionSkeleton(userId, jointID1, joint1);
  109.   kinect.getJointPositionSkeleton(userId, jointID2, joint2);
  110.   return PVector.dist(joint1, joint2);

  111. void drawJoint(int userId, int jointID) {
  112.   PVector joint = new PVector();
  113.   float confidence = kinect.getJointPositionSkeleton(userId, jointID, joint);
  114.   if (confidence < 0.5) {
  115.     return;
  116.   }
  117.   PVector convertedJoint = new PVector();
  118.   kinect.convertRealWorldToProjective(joint, convertedJoint);
  119.   ellipse(convertedJoint.x, convertedJoint.y, 5, 5);
  120. }
  121. // user-tracking callbacks! 
  122. void onNewUser(int userId) {
  123.   println("start pose detection");
  124.   kinect.startPoseDetection("Psi", userId);
  125. }

  126. void onEndCalibration(int userId, boolean successful) {
  127.   if (successful) {
  128.     println(" User calibrated !!!");
  129.     kinect.startTrackingSkeleton(userId);
  130.     calibrated = true;
  131.   }
  132.   else {
  133.     println(" Failed to calibrate user !!!");
  134.     kinect.startPoseDetection("Psi", userId);
  135.   }
  136. }

  137. void onStartPose(String pose, int userId) {
  138.   println("Started pose for user");
  139.   kinect.stopPoseDetection(userId);
  140.   kinect.requestCalibrationSkeleton(userId, true);
  141. }

Hi everyone, 

I'm trying to learn about skeleton tracking, and as you can see from the picture above, I wonder how can I fix this to make the picture and the skeleton updated rather than being so. I'd be much appreciated if any of you could help me with this. 


Replies(3)

No one in this forum has an idea about kinect and this problem? 
erm

Copy code
  1. background(0);

in the draw ?


I don't have a kinect myself, so I cannot experiment with it, but it's always good to call background(), if you want to start with a clean screen every frame.