Problem loading image loop

edited May 2014 in Kinect

Hello,

I want to ask for help for something that I think is not too complicated, but It seems that I am stuck and can't continue. Also I'm very new to Processing and Kinect - this is just the second sketch I'm developing for an academy project. For now what I have is the canvas divided in two parts - on the right is the rgbImage from Kinect and on the left I am doing a background removal, so it is only the silhouette of the captured users. And when a user is tracked I'm saving a sequence of images of the captured silhouette on the left. I want after that to display these images as a loop on top of the rgb image on the right. And this is what can't manage to do for now. You can see the code below.
Can you please help with some ideas and advises? I know that is something that I'm missing, but I can't resolve this problem.
I'll be very grateful. thanks :)

also I'm using SimpleOpenNI 1.96 on windows 7, 32bit, Processing version 2.0.3

import processing.opengl.*;
import SimpleOpenNI.*;

SimpleOpenNI kinect;

// for the image loop
int numFrames = 24;
int frame = 0;
PImage[] images = new PImage[numFrames];
int frameCounter = 0;

//for the tracking
boolean tracking = false;
int userID; 
int[] userMap;

// declare our images
PImage rgbImage;
PImage resultImage;

PImage transparentImage;//displayng images with backgroung removed on the rgb image

//for controling when the tracking is finished boolean trackingComplete = false;

void setup() {
  size(640*2, 480);
 // frameRate(30);
  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 = createImage(640, 480, RGB);
}
void draw() {
  background(0);

  println(frameCounter);

  kinect.update();

  // get the Kinect color image
  rgbImage = kinect.rgbImage();

 if (frame>=numFrames) {
   frame = 0;
 }
 transparentImage = loadImage( "manOne" + frame + ".png" );

 image(rgbImage, 640, 0);
// image(images[frame], 640, 0);

if(trackingComplete){
 transparentImage.loadPixels();

 for(int i = 0; i < 640*480; i++){
    //here i check which pixels are black
    //and make them equal to the rgbImage
   if(transparentImage.pixels[i] == color(0, 0, 0)){
     transparentImage.pixels[i] = rgbImage.pixels[i];
   } else {
     transparentImage.pixels[i] = transparentImage.pixels[i];
   }     
 }

 transparentImage.updatePixels();
 image(transparentImage, 640, 0);

}

 frame+=2;

  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] = color(0, 0, 0, 0); 
      }
    }

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

         if( frameCounter<numFrames*2) {
   if( frameCounter % 2 == 0){
     String imageName = "manOne" + frameCounter + ".png";
   resultImage.save(imageName);
   }
   frameCounter ++;
 }

    image(resultImage, 0, 0);
//  updatePixels();
  }
 if (frameCounter == 48){
   trackingComplete = true;
 }
}

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

void onLostUser(SimpleOpenNI curContext, int userId)
{
//  frameCounter = 0;
  trackingComplete = false;
  println("tracking complete");
//  println("onLostUser - userId: " + userId);
}

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

Answers

  • mukmuk
    edited April 2014 Answer ✓

    you need to draw your image on top of the ohter using alpha masking.... its not yet what you want, but i dont have time right now to get deeper into the masking and blendingmodes... i guess you´ll figure out. if not, tell me.

    import processing.opengl.*;
    import SimpleOpenNI.*;
    
    SimpleOpenNI kinect;
    
    int numFrames = 24;
    int frame = 0;
    PImage[] images = new PImage[numFrames];
    
    int frameCounter = 0;
    boolean tracking = false;
    int userID;
    int[] userMap;
    
    // declare our images
    PImage rgbImage;
    PImage resultImage;
    
    void setup() {
    
      size(640*2, 480, P3D);
    
      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();
    
      // turn on depth/color alignment
      kinect.alternativeViewPointDepthToImage();
    
      //create a buffer image to work with instead of using sketch pixels
      resultImage = createImage(640, 480, RGB);
    }
    void draw() {
    
      background(0);
    
      blendMode(BLEND);
    
      kinect.update();
    
      // get the Kinect color image
      PImage rgbImage = kinect.rgbImage();
    
      if (frame>=numFrames) frame=0;
    
      images[frame] = loadImage( "test" + frame + ".png" );
    
      rgbImage.mask(images[frame]);
      image(rgbImage,640,0);
      //image(images[frame], 640, 0);
      //image(images[frame], 640, 0);
    
      frame+=2;
    
      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] = color(0, 0, 0, 0);
          }
        }
    
        //update the pixel from the inner array to image
        resultImage.updatePixels();
        image(resultImage, 0, 0);
    
        recordUser();
      }
    
        //resultImage.blend(rgbImage, 640,0,640,480,640,0,640,480,ADD);
    
        //  updatePixels();
    
      //displayImage();
    }
    
    void onNewUser(SimpleOpenNI curContext, int userId)
    {
      userID = userId;
      tracking = true;
      println("tracking");
      //curContext.startTrackingSkeleton(userId);
    }
    
    void onLostUser(SimpleOpenNI curContext, int userId)
    {
      frameCounter = 0;
      println("onLostUser - userId: " + userId);
    }
    
    void onVisibleUser(SimpleOpenNI curContext, int userId)
    {
      //println("onVisibleUser - userId: " + userId);
    }
    
    void recordUser() {
      // saving a sequence of pictures
      if ( frameCounter<60) {
        // every second frame
        if ( frameCounter % 2 == 0) {
          String imageName = "test" + frameCounter + ".png";
          resultImage.save(imageName);
        }
        frameCounter ++;
      }
    }
    /*
    void displayImage(){
     // here I'm trying to display the above saved images in a loop
     for (int i = 0; i <= numFrames; i++) {
     images[i] = loadImage( "test" + i + ".png" );
      image(images[i], 640, 0);
    
     }
     //frame = (frame+1) % numFrames;
     //image(images[numFrames], 640, 0);
     }
    */
    
Sign In or Register to comment.