Assign multiple random colors to Kinect users on screen

This is a school project and I am having trouble. I want to add more than one color, for now, I am only able to add pink color(255, 0, 140); when a person is n front of the Kinect. I would want random colors to appear each time a person appears. Also is it possible to make it gradient colors? Thank you!

import org.openkinect.processing.*;

Kinect kinect;

float minT = 300;
float maxT = 800;
PImage img;

void setup() {
  size(640,480);
  frameRate(3);
  kinect = new Kinect(this);
  kinect.initDepth();
  kinect.enableMirror(true);
  img = createImage(kinect.width, kinect.height, RGB);
}

void draw() {
  background(0);
  img.loadPixels();
  
  int[] depth = kinect.getRawDepth();
  
  float sumX = 0;
  float sumY = 0;
  float totalPixels = 0;
  
  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++) {
      int offset = x + y * kinect.width;
      int d = depth[offset];
      
      if (d > minT && d < maxT && x > 100) {
        img.pixels[offset] = color(255, 0, 140);
        
        sumX += x;
        sumY += y;
        totalPixels++;
       
      } else {
        img.pixels[offset] = color(0);
    }
  }
  img.updatePixels();
  image(img, 0, 0);
  
  float avgX = sumX / totalPixels;
  float avgY = sumY / totalPixels;
  //fill(150, 0, 255);
  textSize(20);
  text("you're hot", avgX, avgY);
  } 
}

Thanks again!

This is my updated one, but I am not sure if I am doing this right. Please help thank you!

import org.openkinect.processing.*;

Kinect kinect;

float minT = 300;
float maxT = 800;
PImage img;

color c1;
color c2;

void setup() {
  size(640,480);
  frameRate(3);
  kinect = new Kinect(this);
  kinect.initDepth();
  kinect.enableMirror(true);
  colorMode(HSB, 100);
  img = createImage(kinect.width, kinect.height, RGB);

  c1 = color(random(100), 100, 100);
  c2 = color(random(100), 100, 30);

  for(int y = 0; y < img.height; y++) {
    float n = map(y, 0, img.height, 0, 1);
    color newc = lerpColor(c1, c2, n);
    stroke(newc);
    line(0, y, img.width, y);
  }
}

void draw() {
  background(0);
  img.loadPixels();

  int[] depth = kinect.getRawDepth();

  float sumX = 0;
  float sumY = 0;
  float totalPixels = 0;

  for (int x = 0; x < img.width; x++) {
    for (int y = 0; y < img.height; y++) {
      int offset = x + y * kinect.width;
      int d = depth[offset];

      if (d > minT && d < maxT && x > 100) {
        img.pixels[offset] = lerpColor(c1, c2, 140);

        sumX += x;
        sumY += y;
        totalPixels++;

      } else {
        img.pixels[offset] = color(0);
    }
  }
  img.updatePixels();
  image(img, 0, 0);

  float avgX = sumX / totalPixels;
  float avgY = sumY / totalPixels;
  //fill(150, 0, 255);
  textSize(20);
  text("you're hot", avgX, avgY);
  }
}

Hi

Yes, your code is ok but it doesn’t demonstrate how you handle multiple users. Right now you are selecting pixels based on a depth range and for x > 100. What is your approach to detect multiple users? Can your kinect version do it? Or are you willing to implement this part yourself? If you make this clear in your post, you might start addressing your question properly.

Did you check previous code like this one: Changing opacity of silhouettes - Processing 2.x and 3.x Forum

More links:

[Kinect] How to set transparent background except user's silhouette? - Processing 2.x and 3.x Forum
Text in silhouette using kinect - Processing 2.x and 3.x Forum

For the gradient color, check your code:

  c1 = color(random(100), 100, 100);
  c2 = color(random(100), 100, 30);

  for(int y = 0; y < img.height; y++) {
    float n = map(y, 0, img.height, 0, 1);
    color newc = lerpColor(c1, c2, n);
    stroke(newc);
    line(0, y, img.width, y);
  }

If you move the definitions of c1 + c2 inside the for loop, I bet you get a gradient.

For setting a gradient within a shape, consider learning about mask on images. If you don’t know how to do this, ask this as a separate post.

My advice: Before you work with gradients and masking, focus on detecting multiple users and assigning them unique colors. Keep in mind not many ppl in the community have a kinect to test, so you might be on your own. Do your testing and come back and provide specific questions and MCVEs and you will get more promptly responses.

Also check these other links as they could be relevant:

https://forum.processing.org/two/categories/kinect
https://forum.processing.org/two/profile/comments/45527/totovr76

Kf

Thank you @kfrajer I would have to use the SimpleOpenNi library to detect different users so, that different users can have different colors. My kinect version is able to detect multiple users i have the Kinect version 1.

For this part of my code I am able to get the gradient color but it would show up first and then it’ll start running the other part of my code which would then get the min and max threshold and color the person pink if they are within the min and max threshold.

c1 = color(random(100), 100, 100);
 c2 = color(random(100), 100, 30);

 for(int y = 0; y < img.height; y++) {
   float n = map(y, 0, img.height, 0, 1);
   color newc = lerpColor(c1, c2, n);
   stroke(newc);
   line(0, y, img.width, y);
 }

Do you know if openkinect is able to detect multiple users or do I have to use SimpleOpenNi library?

Sorry, I don’t know this. If you check previous posts, you might find an answer as it was discussed before or maybe some member in the community could confirm this. A side note: Notice that the resources provided by @totovr76 have only been tested in Mac OS.

While you wait for an answer, go ahead and do some online search and see if you can find the answer. If you are able to detect users, then it will save you some work. Or you could start looking into implementing a user detection algorithm. I believe @jeremydouglass was doing this in this post: Changing opacity of silhouettes - Processing 2.x and 3.x Forum

Kf