[Kinect] How to set transparent background except user's silhouette?

edited December 2017 in Kinect

Hi, I am quite new to the forum.

I get a lot of help from this forum. And I hope to be able to give back when I will get better with programming:)

I am making a kinect game for my university project, and now I am testing some basic features.

What I want to do is to display only a user's silhouette over a background(game stage etc.)

So I tried to color the pixels of depthImage within a threshold with one color and other pixels with transparent color

color(0, 0, 0, 0);

But when I run the code, transparent pixels are displayed at first, but as a person moves around, the silhouette color remains and overlaps, creating the "Painting" effect.

Demo video: https://youtu.be/KXebF0i0FKg

It would be great if you can tell me what I am doing wrong, or just guide me to the right direction?

Thanks in advance. Here's the code:

import org.openkinect.freenect.*;
import org.openkinect.processing.*;

Kinect kinect;

// Depth image
PImage depthImg;

// Threshold
int minDepth = 450;
int maxDepth = 890;

void setup() {
  size(1280, 720);
  background(145, 170, 180);

  kinect = new Kinect(this);
  kinect.initDepth();
  kinect.enableMirror(true);

  // Silhouette image
  depthImg = new PImage(kinect.width, kinect.height, ARGB);
}

void draw() {

  // Draw the raw image
  //image(kinect.getDepthImage(), 0, 0);

  depthImg.loadPixels();

  // Threshold the depth image
  int[] rawDepth = kinect.getRawDepth();

  for (int x = 0; x < kinect.width; x++) {
    for (int y = 0; y < kinect.height; y++) {
      int index = x + y * kinect.width;
      int p = rawDepth[index];

      if (p > minDepth && p < maxDepth) {
        depthImg.pixels[index] = color(62, 96, 111); //Silhouette color
      } else {
        depthImg.pixels[index] = color(0, 0, 0, 0); // background color
      }
    }
  }

  // Draw the thresholded image
  depthImg.updatePixels();
  imageMode(CENTER);
  image(depthImg, width/2, height - kinect.height / 2);
}

OS: Mac OS Sierra v10.12.6

Processing: v3.3.6

Library: Open Kinect

Kinect Hardware: Microsoft Kinect v1, 1414!

Answers

  • edited December 2017

    The issue has nothing to do with your kinect - all that code looks right.

    The problem is when you draw the silhouette image on line 51. Some parts of that image are the silhouette color (darker blue), but the points that aren't in the silhouette are TRANSPARENT - so when you draw them to your sketch, they don't effect the color that was drawn there previously.

    Ironically, you have the hard part right an are just missing the easy part: Drawing whatever background image (your game) that you want behind the person's silhouette!

    Add this code to the start of draw() and see what I mean:

    background( random(255), random(255), random(255) );
    

    Replace drawing this random background with drawing whatever scene you want behind your person and you're good to go! Also, look into mask() if you want to draw what the person looks like, instead of their silhouette.

  • Wow! that was really a basic stuff lol Redrawing a background in draw()!

    Yeah, these small things you always miss.. You're the man.

    And with the mask() you mean that if I use the 'silhouette' PImage as a mask to the RGB image I will get only the person's image masked out?

Sign In or Register to comment.