How can I get center point of each a bunch of red points

Hi
I’m using processing to make some good project.

explanation of project:
Detect hand based on depth and draw the circle on each hand.

Progress:
I can find center but there’s issue.
If two bunch of pixels exist, it’s not what i want it. I want to draw circle on the each hand.
22

souce code:

// Daniel Shiffman
// Depth thresholding example
// https://github.com/shiffman/OpenKinect-for-Processing
// http://shiffman.net/p5/kinect/
// Original example by Elie Zananiri
// http://www.silentlycrashing.net
import org.openkinect.freenect.*;
import org.openkinect.processing.*;

Kinect kinect;

// Depth image
PImage depthImg;

// Which pixels do we care about?
int minDepth =  60;
int maxDepth = 650;
int forX=0;

// What is the kinect's angle
float angle;
float avgX=0;
float avgY=0;
void setup() {
  size(640, 480);  //kinect v1 depth re: 320x240

  kinect = new Kinect(this);
  kinect.initDepth();
  angle = kinect.getTilt();

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

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

  float sumX=0,sumY=0;
  float total=0;

  for(int x=0; x<kinect.width; x++)
  {
    for(int y=0; y<kinect.height; y++)
    {
      int offset = x+y*kinect.width;
     
      if (rawDepth[offset] >= minDepth && rawDepth[offset] <= maxDepth) //detect care pixels. 
      {
          depthImg.pixels[offset] = color(255,0,100); 
          sumX+=x;
          sumY+=y;
          total++;
      }
      else 
      {
        depthImg.pixels[offset] = color(0);
      }
    }
  }
  
  // Draw the thresholded image
  depthImg.updatePixels();
  image(depthImg, 0, 0);
 
  avgX = sumX / total;
  avgY = sumY / total;
  fill(255,255,100);
  ellipse(avgX,avgY,54,54);
  
  
  fill(255,255,0);
  text("TILT: " + angle, 10, 20);
  text("THRESHOLD: [" + minDepth + ", " + maxDepth + "]", 10, 36);
  text("avgX: "+int(avgX)+"  avgY: " +int(avgY) ,10,60);
}

thank you for reading my question. and Sorry about my English skills… hope you understand.

1 Like

hi Ieereset,

What you are asking is really not clear and it is not because of your english.

We miss 3 things:

  • An example of your source data (the thing that you want to analyze)
  • A clear explanation of what you want to acheive based on that example
  • The code of what you have so far

Rigth now on the picture you posted we have no idea of what is the yellow circle, what are the red parts and what the top left values represent.

1 Like

Okay, it’s a bit more clear now. Still not the best but good enough :slight_smile:

I guess that the red parts are where your hand are?
And I guess that the problem that you have is that for the right hand, you have 3 sets of pixels instead of only one right?

If this is the case, there are several things you can try:

  • Tweak your threshold value to get better result so you end up with the proper number of area
  • The problem with the previous point is that you can still end up with more than 2 area if the condition of use are not the same. So in addition, you can count the number of pixels of each area and keep the 2 biggest one.

Thank you for comment.
how do I count the number of pixels of each area?

You would need some sort of flood algorithm.
Check this discussion, I wrote an example that is perfect for your problem.

1 Like

An easy solution (relatively) is to use the libraries OpenCV or BoofCV. BoofCV have an example called ElipseFitting, which i think is exactly what you are looking for.

1 Like