Using Openkinect - how to change the location of a text string when theres different readings taken

edited May 2016 in Kinect

My set up - kinect pointing downwards from ceiling with objects placed on a table, when an object is picked up, text displays, when two objects are stacked on eachother, more text is displayed, etc, the more objects the more text. What I'm trying to do is instead of assigning them an exact x,y location - is to position the first bit of text to appear in the center, then when another is read, for the two bits of text to spread out a little bit, and when alot of text is on screen for them to all be laid out better than assigned x,y values.. hope this makes sense? here's my current code:

//import openkinect library
import org.openkinect.*;
import org.openkinect.processing.*;

PFont font;

// Kinect Library object
Kinect kinect;
boolean rgb = true;
boolean depth = false;
boolean ir = false;

// Start at 0 degrees
float deg = 0; 

//rotate
float angle;

// Declare variable 'a' of type float
float a = 0;

// Size of kinect image
int w = 640;
int h = 480;

// Calling the grid refs
int a1,a2,a3,
    b1,b2,b3,
    c1,c2,c3,
    d1,d2,d3,

    a12,a22,a32,
    b12,b22,b32,
    c12,c22,c32,
    d12,d22,d32,

    a13,a23,a33,
    b13,b23,b33,
    c13,c23,c33,
    d13,d23,d33,

    a14,a24,a34,
    b14,b24,b34,
    c14,c24,c34,
    d14,d24,d34;

// We'll use a lookup table so that we don't have to repeat the math over and over
float[] depthLookUp = new float[2048];

PImage bg;

String a1content1 = "hello a1";
String a2content1 = "hello this is a2";
String a3content1 = "hello this is a3 content";
String b1content1 = "hello b1";
String b2content1 = "hello this is b2";
String b3content1 = "hello this is b3 content";
String c1content1 = "hello c1";
String c2content1 = "hello this is c2";
String c3content1 = "hello this is c3 content";
String d1content1 = "hello d1";
String d2content1 = "hello this is d2";
String d3content1 = "hello this is d3 content";

String a1content2 = "hello a1 stack2";
String a2content2=  "hello this is a2 stack2";
String a3content2 = "hello this is a3 content stack2";
String b1content2 = "hello b1 stack2";
String b2content2 = "hello this is b2 stack2";
String b3content2 = "hello this is b3 content stack2";
String c1content2 = "hello c1 stack2";
String c2content2 = "hello this is c2 stack2";
String c3content2 = "hello this is c3 content stack2";
String d1content2 = "hello d1 stack2";
String d2content2 = "hello this is d2 stack2";
String d3content2 = "hello this is d3 content stack2";

String a1content3 = "hello a1 stack3";
String a2content3 = "hello this is a2 stack3";
String a3content3 = "hello this is a3 content stack3";
String b1content3 = "hello b1 stack3";
String b2content3 = "hello this is b2 stack3";
String b3content3 = "hello this is b3 content stack3";
String c1content3 = "hello c1 stack3";
String c2content3 = "hello this is c2 stack3";
String c3content3 = "hello this is c3 content stack3";
String d1content3 = "hello d1 stack3";
String d2content3 = "hello this is d2 stack3";
String d3content3 = "hello this is d3 content stack3";

String a1content4 = "hello a1 stack4";
String a2content4 = "hello this is a2 stack4";
String a3content4 = "hello this is a3 content stack4";
String b1content4 = "hello b1 stack4";
String b2content4 = "hello this is b2 stack4";
String b3content4 = "hello this is b3 content stack4";
String c1content4 = "hello c1 stack4";
String c2content4 = "hello this is c2 stack4";
String c3content4 = "hello this is c3 content stack4";
String d1content4 = "hello d1 stack4";
String d2content4 = "hello this is d2 stack4";
String d3content4 = "hello this is d3 content stack4";

//set up  
void setup() {
  size(1280,760);

  // load and set the font
  font = createFont("Arial", 24);
  textFont(font);
  //strokeWeight(2.5);;

  //set up kinect for sketch
  kinect = new Kinect(this);
  kinect.start();
  kinect.enableDepth(true);
  // We don't need the grayscale image in this example
  // so this makes it more efficient
  kinect.processDepthImage(false);
  kinect.enableRGB(rgb);
  kinect.enableIR(ir);

  // tilt ability
  kinect.tilt(deg);

  // Lookup table for all possible depth values (0 - 2047)
  for (int i = 0; i < depthLookUp.length; i++) {
    depthLookUp[i] = rawDepthToMeters(i);
      }

        bg = loadImage("nano.jpg");
      }

//draw
void draw() {

  background(bg);

  textMode(SCREEN);
 // text("Kinect FR: " + (int)kinect.getDepthFPS() + "\nProcessing FR: " + (int)frameRate,10,16);
  //image(kinect.getVideoImage(),0,0);
  // Get the raw depth as array of integers
  int[] depth = kinect.getRawDepth();
  String test = "";
  // We're just going to calculate and draw every 4th pixel (equivalent of 160x120)
  //int skip = 160;
  int skipx = 24;
  int skipy = 24;
  // Translate and rotate
  //translate(width/2,height/2,200);
  //rotateY(a);

  for(int x=225; x<w-327; x+=skipx) {   
    for(int y=185; y<h-233; y+=skipy) {

      int offset = (x+12)+(y)*w;

      // Convert kinect data to world xyz coordinate
      int rawDepth = depth[offset];
      PVector v = depthToWorld(x,y,rawDepth);

      //stroke(255);
      // pushMatrix();
      // Scale up by 200
      float factor = 50;
      //translate(v.x*factor,v.y*factor,factor-v.z*factor);
      ////println(x + " " + y + " " + v.x*factor + " " + v.y*factor + " " + v.z*factor);

      //z = distance from kinect
      //println(int(v.z*factor));
      stroke(255);
      //fill(v.z*factor,v.z*factor,v.z*factor,200);
      //rect(x, y, skipx, skipy);

      //Draw a point
      //point(0,0);
      //popMatrix();









      int[] values = {a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3};

   String[] content1 = {a1content1,a2content1,a3content1,b1content1,b2content1,b3content1,c1content1,c2content1,c3content1,d1content1,d2content1,d3content1};
   String[] content2 = {a1content2,a2content2,a3content2,b1content2,b2content2,b3content2,c1content2,c2content2,c3content2,d1content2,d2content2,d3content2};
   String[] content3 = {a1content3,a2content3,a3content3,b1content3,b2content3,b3content3,c1content3,c2content3,c3content3,d1content3,d2content3,d3content3};
   String[] content4 = {a1content4,a2content4,a3content4,b1content4,b2content4,b3content4,c1content4,c2content4,c3content4,d1content4,d2content4,d3content4};

      int[] xcoord1 = {10,20,30,40,50,60,70,80,90,100,110,120};
      int[] xcoord2 = {10,20,30,40,50,60,70,80,90,100,110,120};
      int[] xcoord3 = {10,20,30,40,50,60,70,80,90,100,110,120};
      int[] xcoord4 = {10,20,30,40,50,60,70,80,90,100,110,120};

      int[] ycoord1 = {10,20,30,40,50,60,70,80,90,100,110,120};
      int[] ycoord2 = {10,20,30,40,50,60,70,80,90,100,110,120};
      int[] ycoord3 = {10,20,30,40,50,60,70,80,90,100,110,120};
      int[] ycoord4 = {10,20,30,40,50,60,70,80,90,100,110,120};


      for (int i=0; i<values.length; i++){
        if ((values[i]<=74)&&(values[i]>=70)){
              fill(0,0,0);
              rect(200,400,0,0);
              fill(255,255,255);
              text(content1[i],xcoord1[i],ycoord1[i]);
                    }

        if ((values[i]<=69)&&(values[i]>=66)){
              fill(0,0,0);
              rect(200,400,0,0);
              fill(255,255,255);
              text(content2[i],xcoord2[i],ycoord2[i]);
                    }

        if ((values[i]<=65)&&(values[i]>=62)){
              fill(0,0,0);
              rect(200,400,0,0);
              fill(255,255,255);
              text(content3[i],xcoord3[i],ycoord3[i]);
                    }

        if ((values[i]<=61)&&(values[i]>=57)){
              fill(0,0,0);
              rect(200,400,0,0);
              fill(255,255,255);
              text(content4[i],xcoord4[i],ycoord4[i]);
                    }
      }

// Rotate a += 0.015f;

}

// 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; }

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;

PVector result = new PVector(); double depth = depthLookUp[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; }

//tilt ability by pressing up/down keys void keyPressed() { if (key == 'd') { depth = !depth; kinect.enableDepth(depth); } else if (key == 'r') { rgb = !rgb; if (rgb) ir = false; kinect.enableRGB(rgb); } else if (key == 'i') { ir = !ir; if (ir) rgb = false; kinect.enableIR(ir); } }

// STOP void stop() { kinect.quit(); super.stop(); }

Sign In or Register to comment.