How to draw a static PointCloud from Kinect?

Hey guys, I’m working on a project that use Kinect. My idea is when event happened, the canvas will draw the PointCloud at that moment. I was trying to make an object , and when mouse clicked, pass the current depth array to the constructor and create the object which include the algorithm to draw the PointCloud. But it doesn’t working, the canvas just show me a single point when I running the sketch.

Can anyone help me with this?

import org.openkinect.processing.*;
Kinect2 kinect2;

ArrayList <drawPoints> images;
int[]depth;

void setup(){
  size(800,600,P3D);

  kinect2 = new Kinect2(this);
  kinect2.initDepth();
  kinect2.initDevice();
  smooth(16);
  
  //use arrayList to add/delete pointCloud
  images = new ArrayList<drawPoints>();
}

void draw(){
  background(0);
  
  if(images.size() > 0){
    println("should draw");
    for(drawPoints p : images){
      p.drawPersona();
    }
  }
}

void mouseClicked(){
  //when mouse Clicked, pass the depth data and create object;
  depth = kinect2.getRawDepth();
  images.add(new drawPoints(0,0,50,depth));
}

//calculate the xyz camera position based on the depth data
PVector depthToPointCloudPos(int x, int y, float depthValue) {
    PVector point = new PVector();
    point.z = (depthValue);// / (1.0f); // Convert from mm to meters
    point.x = (x - CameraParams.cx) * point.z / CameraParams.fx;
    point.y = (y - CameraParams.cy) * point.z / CameraParams.fy;
    return point;
  }

following is the class I created:

class drawPoints{
  
  PVector loc;
  int[] depth;
  
  drawPoints(int _x, int _y, int _z, int[] _depth){
    loc = new PVector(_x,_y,_z);
    depth = _depth;
  }

  void drawPersona(){
    pushMatrix();
    translate(loc.x,loc.y,loc.z);
    stroke(255);
    strokeWeight(2);

    //printArray(depth), the depth data seems right here, but the following code doesn't work;

    beginShape(POINTS);
    for(int x = 0; x < 512; x += 2){
      for(int y = 0; y < 424; y += 2){
        int index = x + y * 512;
        
        PVector point = depthToPointCloudPos(x, y, depth[index]);
        
        //println(point.x);
        vertex(point.x, point.y, point.z);
      }
    }
    endShape();
    popMatrix();
  }

}

Thank you so much if anyone can help me with this.

1 Like

Investigate this line: point.x = (x - CameraParams.cx) * point.z / CameraParams.fx; Sample a few points and make sure they are not zero. I am guessing point.z could be zero. Second thought is that the division is producing values close to zero and having an integer div will reduce it only to the whole part of the number.

You can use the debug mode in the PDE to read these values, or use println statements.

Kf

1 Like

Thanks Kfrajer, I learned a lot for the posts you give others, thank you. I will try your suggestion soon today.