Kinect - Background removal with video

edited April 2014 in Kinect

Hello everyone ! I'm a french student in communication and i have a problem : I need to use the kinect to remove a background behind someone, and put a video (in loop) as new background :) I'm working with processing 2.1.1. and SimpleOpenNI Version 1.96

I find this code, who remove the background and put an image, but i need a video :/

If someone has any idea ! :)

import processing.opengl.*;
import SimpleOpenNI.*;
//install this on your machine and also the library in processing
//http://code.google.com/p/simple-openni/wiki/Installation


SimpleOpenNI kinect;
//based on Greg's Book Making things see.
boolean tracking = false;
int userID; int[] userMap;
// declare our images
PImage backgroundImage;
PImage resultImage;
void setup() {
  size(640*2, 480);
  // load the background image
  backgroundImage = loadImage("http://www.eheart.com/SHASTA/wallpaper/alpenglow-640x480.jpg");
  kinect = new SimpleOpenNI(this);
  if(kinect.isInit() == false)
  {
     println("Can't init SimpleOpenNI, maybe the camera is not connected!");
     exit();
     return;
  }

  // enable depthMap generation
 kinect.enableDepth();

  // enable skeleton generation for all joints
  kinect.enableUser();
  // enable color image from the Kinect
  kinect.enableRGB();
  //enable the finding of users but dont' worry about skeletons

  // turn on depth/color alignment
  kinect.alternativeViewPointDepthToImage();
  //create a buffer image to work with instead of using sketch pixels
  resultImage = new PImage(640, 480, RGB);
}
void draw() {
  kinect.update();
  // get the Kinect color image
  PImage rgbImage = kinect.rgbImage();

  image(rgbImage, 640, 0);
  if (tracking) {
    //ask kinect for bitmap of user pixels
    loadPixels();
    userMap = kinect.userMap();
    for (int i =0; i < userMap.length; i++) {
      // if the pixel is part of the user
      if (userMap[i] != 0) {
        // set the pixel to the color pixel
        resultImage.pixels[i] = rgbImage.pixels[i];
      }
      else {
        //set it to the background
        resultImage.pixels[i] = backgroundImage.pixels[i];
      }
    }

    //update the pixel from the inner array to image
     resultImage.updatePixels();
    image(resultImage, 0, 0);
  }
}


void onNewUser(SimpleOpenNI curContext, int userId)
{
 userID = userId;
  tracking = true;
  println("tracking");
  //curContext.startTrackingSkeleton(userId);
}

void onLostUser(SimpleOpenNI curContext, int userId)
{
  println("onLostUser - userId: " + userId);
}

void onVisibleUser(SimpleOpenNI curContext, int userId)
{
  //println("onVisibleUser - userId: " + userId);
}

Love,

Azeb

Tagged:

Answers

  • edited April 2014

    You can try this but I am not sure whether it would work or not. I don't have Kinect so I can test it by myself so tell me if you get any success.

    import processing.opengl.*;
    import processing.video.*; // I have added : importing processing video library
    import SimpleOpenNI.*;
    //install this on your machine and also the library in processing
    //<a href="http://code.google.com/p/simple-openni/wiki/Installation" target="_blank" rel="nofollow">http://code.google.com/p/simple-openni/wiki/Installation</a>;
    
    
    SimpleOpenNI kinect;
    Movie mov; // I have added: Creating movie object
    //based on Greg's Book Making things see.
    boolean tracking = false;
    int userID; 
    int[] userMap;
    // declare our images
    PImage backgroundImage;
    PImage resultImage;
    
    void movieEvent(Movie m) { // movie reading
      m.read();
    }
    
    void setup() {
      size(640*2, 480, P3D); // use P3D with size. It will make your program a bit faster becuase it uses system GPU for calculation and graphics rendering
      // load the background image
      //  backgroundImage = loadImage("Test.jpg"); // I have commented it becuase you don't want it
      // 
      mov = new Movie(this, "transit.mov"); // I have added: load a movie file  
      kinect = new SimpleOpenNI(this);
      if (kinect.isInit() == false)
      {
        println("Can't init SimpleOpenNI, maybe the camera is not connected!");
        exit();
        return;
      }
    
      // enable depthMap generation
      kinect.enableDepth();
    
      // enable skeleton generation for all joints
      kinect.enableUser();
      // enable color image from the Kinect
      kinect.enableRGB();
      //enable the finding of users but dont' worry about skeletons
    
      // turn on depth/color alignment
      kinect.alternativeViewPointDepthToImage();
      //create a buffer image to work with instead of using sketch pixels
      resultImage = new PImage(640, 480, RGB);
    }
    void draw() {
      kinect.update();
      // get the Kinect color image
      mov.play(); // I have added: play movie 
      PImage rgbImage = kinect.rgbImage();
      //  image(rgbImage, 0, 0);
      image(mov, 0, 0); 
      if (tracking) {
        //ask kinect for bitmap of user pixels
        loadPixels();
        resultImage.userMap = kinect.userMap();
        for (int i =0; i < userMap.length; i++) {
          // if the pixel is part of the user
          if (userMap[i] != 0) {
            // set the pixel to the color pixel
            resultImage.pixels[i] = rgbImage.pixels[i];
          }
          else {
            //set it to the background
            // resultImage.pixels[i] = backgroundImage.pixels[i];
            resultImage.pixels[i] = mov.pixels[i];
          }
        }
    
        //update the pixel from the inner array to image
        resultImage.updatePixels();
        image(resultImage, 0, 0);
      }
    }
    
    
    void onNewUser(SimpleOpenNI curContext, int userId)
    {
      userID = userId;
      tracking = true;
      println("tracking");
      //curContext.startTrackingSkeleton(userId);
    }
    
    void onLostUser(SimpleOpenNI curContext, int userId)
    {
      println("onLostUser - userId: " + userId);
    }
    
    void onVisibleUser(SimpleOpenNI curContext, int userId)
    {
      //println("onVisibleUser - userId: " + userId);
    }
    
Sign In or Register to comment.