Kinect PointCloudDepthOGL display delay

edited April 2017 in Kinect

Hi everyone, I have been trying to achieve a 3-6 sec delay on the live display from my kinect 2's point cloud feed, I am new to all this, so I have been basing my work on the KinectPV2 library by Langeling, I also came across a post here (https://forum.processing.org/two/discussion/15800/how-to-delay-the-feed-of-the-kinect-2-point-cloud) where it is solved but i am having a hard time applying it to the solution I have in hands. this is the code i have in hands

import java.nio.*;
import KinectPV2.*;

KinectPV2 kinect;

int Vloc, z = -200, VBOID;
float scale = 260, pointScale = 100.0, a = 0.1;

int distanceMax = 4500, distanceMin = 0;

PGL pgl;
PShader psh;
ArrayList<ArrayList> clouds;
  int limit = 120;

public void setup ()
{
  size(1280, 720, P3D);
  kinect = new KinectPV2(this);
  kinect.enableDepthImg(true);
  kinect.enablePointCloud(true);
  kinect.setLowThresholdPC(distanceMin);
  kinect.setHighThresholdPC(distanceMax);

  kinect.init();

  psh = loadShader("frag.glsl", "vert.glsl");
  PGL pgl = beginPGL();
  IntBuffer buffer = IntBuffer.allocate(1);
  pgl.genBuffers(1, buffer);
  VBOID = buffer.get(0);
  endPGL();

  clouds = new ArrayList<ArrayList>();
  }

  public void draw ()
  {
    background(0);
    translate(width/2, height/2, z);
    scale(scale, -1*scale, scale);
    rotate(a, 0.0f, 1.0f, 0.0f);
    kinect.setLowThresholdPC(distanceMin);
    kinect.setHighThresholdPC(distanceMax);

    FloatBuffer cloudBuf=kinect.getPointCloudPos();

    pgl=beginPGL();
    psh.bind();

    Vloc=pgl.getAttribLocation(psh.glProgram, "vertex");
    pgl.enableVertexAttribArray(Vloc);
    int vData=kinect.WIDTHDepth * kinect.HEIGHTDepth *3;

    {
     pgl.bindBuffer(PGL.ARRAY_BUFFER, VBOID);
     pgl.bufferData(PGL.ARRAY_BUFFER, Float.BYTES*vData,cloudBuf, PGL.DYNAMIC_DRAW);
     pgl.vertexAttribPointer(Vloc, 3, PGL.FLOAT, false, Float.BYTES*3,0);
    }

   pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);

   //draw point buffer as set of points
   //this is where the delay-draw mechanis should be?
   //or what it should encapsulate?
   pgl.drawArrays(PGL.POINTS, 0, vData);

   pgl.disableVertexAttribArray(Vloc);

   psh.unbind();
   endPGL();

   stroke(255,0,0);

  }
Tagged:

Answers

  • Please edit your post, select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

  • Thank you kfrajer, can you help me now?

  • Do you need to post your shader code?

  • um no, and this is the entire undelayed thing

  • I worked on this code using other libs but it cannot open or detect kinect

    import org.openkinect.freenect.*;
    import org.openkinect.processing.*;
    
    
    
    static class CameraParams {
      static float cx = 254.878f;
      static float cy = 205.395f;
      static float fx = 365.456f;
      static float fy = 365.456f;
      static float k1 = 0.0905474;
      static float k2 = -0.26819;
      static float k3 = 0.0950862;
      static float p1 = 0.0;
      static float p2 = 0.0;
    }
    
    
    Kinect2 kin;
    float a=0;
    float[] dlu=new float[2048];
    ArrayList <ArrayList> clouds;
    int count=120;
    
    
    public void setup ()
    {
     size ( 1280, 720, P3D);
     kin = new Kinect2(this);
     kin.initDevice();
     kin.initDepth();
    
     for (int i=0; i<dlu.length; i++)
     {
       dlu[i]=rawDepthToMeters(i);
     }
    
     clouds=new ArrayList<ArrayList>();
    }
    
    public void draw ()
    {
      background(0);
      ArrayList cloud=new ArrayList();
      if (clouds.size()>count)
      {
        clouds.remove(0);
      }
      clouds.add(cloud);
    
      int[] d=kin.getRawDepth();
      for (int i=0; i<kin.depthWidth; i+=2)
      {
        for (int j=0; j<kin.depthHeight; j+=2)
        {
          int offset=i+j*kin.depthWidth;
          int dep=d[offset];
          PVector p=depthToPointCloudPos(i,j,dep);
          cloud.add(p);
        }
      }
      pushMatrix();
      stroke(255);
      beginShape(POINTS);
    
      if (clouds.size()>count)
      {
        ArrayList currCloud=clouds.get(0);
        for (int i=0; i<currCloud.size();i++)
        {
          PVector vec=(PVector)currCloud.get(i);
          vertex(-vec.x, vec.y, vec.z);
        }
      }
      endShape();
      popMatrix();
    }
    
    PVector depthToPointCloudPos(int x, int y, float depVal)
    {
      PVector p=new PVector();
      p.z=(depVal);
      p.x=(X- CameraParams.cx)*p.z/CameraParams.fx;
      p.y=(Y-CameraParams.cy)*p.z/CameraParams.fy;
      return p;
    }
    
    // These functions come from: http://graphics.stanford.edu/~mdfisher/Kinect.html
    float rawDepthToMeters(int depthValue) {
      if (depthValue < 2047) {
        return (float)(1.0 / ((double)(depthValue) * -0.0030711016 + 3.3309495161));
      }
      return 0.0f;
    }
    
    // Only needed to make sense of the ouput depth values from the kinect
    PVector depthToWorld(int x, int y, int depthValue) {
    
      final double fx_d = 1.0 / 5.9421434211923247e+02;
      final double fy_d = 1.0 / 5.9104053696870778e+02;
      final double cx_d = 3.3930780975300314e+02;
      final double cy_d = 2.4273913761751615e+02;
    
    // Drawing the result vector to give each point its three-dimensional space
      PVector result = new PVector();
      double depth =  dlu[depthValue];//rawDepthToMeters(depthValue);
      result.x = (float)((x - cx_d) * depth * fx_d);
      result.y = (float)((y - cy_d) * depth * fy_d);
      result.z = (float)(depth);
      return result;
    }
    
Sign In or Register to comment.