SimpleOpenNI / Kinect:sceneMap / depthMap颜色控制

时间:2012-05-03 14:06:10

标签: java kinect processing openni simple-openni

[背景]我正在帮助一名艺术项目的研究生(MFA):

我正在尝试控制depthMap生成的输出颜色,以便红色,绿色和&蓝色值可以独立控制。期望的最终游戏是每个depthMap生成的值可以更改为各种色调的洋红色(255,0,255),这样每个SimpleOpenNI用户都会出现差异化。

到目前为止,我还没有找到在pde文件中执行此操作的方法,也没有找到修改SimpleOpenNI类以适应相同目标的正确方法。 - 无论出于何种原因,tint()调用会更改画布的alpha值,似乎不会调整颜色值。

任何适用的建议都将受到赞赏。

资源:SimpleOpenNI Souce 我的pde:

import SimpleOpenNI.*;


SimpleOpenNI  context;

void setup()
{
  context = new SimpleOpenNI(this);

  // enable depthMap generation
  if(context.enableScene() == false)
  {
     println("Can't open the sceneMap, maybe the camera is not connected!"); 
     exit();
     return;
  }

  frameRate(30);
  background(128);
  size(context.sceneWidth() , context.sceneHeight()); 
}

void draw()
{

  // update the cam
  context.update(); 

  // draw irImageMap && Sets frame size.
  image(context.sceneImage(),25,25, 590,430);

  // "blend" shapes BG & previous the frame
   tint(#ED145B, 125);

}

1 个答案:

答案 0 :(得分:0)

除了sceneImage()之外,SimpleOpenNI还提供了一个名为sceneMap()的方法,它以int []作为输入。它使用与该用户对应的数字填充映射到特定用户的数组中的值。

例如,如果没有用户,则sceneMap数组将填充零(例如[0,0,0.....0]), 如果您有1个用户,则对于属于该用户的像素(例如[0,0,0,1,1,...1,0,0,0,1,0...etc.]),某些元素将填充为1,等等。

您可以尝试在PImage中进行一些颜色替换,但这不是很灵活(如果你也启用了深度图怎么办?)并且你依赖于每个用户的硬编码值(R,B, G,C,M,Y,其中订单可能在SimpleOpenNI中,因此sceneMap()是更灵活的选项。

这是一个关于如何使用它的注释示例:

import SimpleOpenNI.*;

SimpleOpenNI  context;

int[] sceneMap;//this will store data about the scene (bg pixels will be 0, and if there are any users, they will have the value of the user id - e.g. if there are no users, the array will be filled with zeros, if there is one user, some array entries will be equal to 1, etc. the size of the array is the same as the number of pixels in scene image, so it's easy to use with the pixels[] of a PImage
PImage myUserImage;//this is where we'll draw the user
int user1Colour = color(180,130,30);//change to whatever you like

void setup()
{
  context = new SimpleOpenNI(this);
  context.enableScene();

  background(200,0,0);
  size(context.sceneWidth() , context.sceneHeight());
  //set scene map array
  sceneMap = new int[context.sceneWidth()*context.sceneHeight()];
  //create the image to draw the user into, by default it will be filled black
  myUserImage = createImage( context.sceneWidth() , context.sceneHeight(), RGB );
}

void draw()
{
  context.update(); 

  // // gives you a label map, 0 = no person, 0+n = person n - tell OpenNI to update the numbers in the array
  context.sceneMap(sceneMap);
  //clear myUserImage - fill everything with black
  Arrays.fill(myUserImage.pixels,color(0));//We've never used Arrays.fill() before, but all it does is it loops through all elements on an array you pass and sets a value you want for each element - fills an array with a value
  for(int i = 0 ; i < myUserImage.pixels.length; i++){
    //check if there is a user for the current pixel, if so, use our custom colour for the pixel at this index
    if(sceneMap[i] > 0) myUserImage.pixels[i] = user1Colour;
  }
  myUserImage.updatePixels();

  //display image
  image(myUserImage,0,0);
}

从长远来看,如果你想为多个用户提供服务,那么值得编写一个基本的实用程序类:

import SimpleOpenNI.*;

SimpleOpenNI  context;

SceneMapper sceneMap;

void setup()
{
  context = new SimpleOpenNI(this);
  context.enableScene();

  background(200,0,0);
  size(context.sceneWidth() , context.sceneHeight());

  sceneMap = new SceneMapper(context);
}

void draw()
{
  context.update(); 
  sceneMap.update();
  //display image
  image(sceneMap.scene,0,0);
}
class SceneMapper{
  PImage scene;//this is a PImage where we'll actually draw the user with what colour we want
  int[] sceneMap;//this will store scene data - an array of ints which has the same length as context.sceneImage().pixels, the only difference is, sceneImage already has colours set, while scene map has numbers representing user(1,2,3etc.) on top of background(0s)
  int numPixels;//total number of pixels, we only store it so we can reuse it
  color bg;//background colour
  color[] users = {color(255),color(192),color(127),color(64),color(32)};//fill colours for users

  SceneMapper(SimpleOpenNI context){
    numPixels = context.sceneWidth()*context.sceneHeight();
    sceneMap = new int[numPixels];//init scene nap array
    scene = createImage( context.sceneWidth(), context.sceneHeight(), RGB );//create a PImage to display scene data
    scene.loadPixels();  
    bg = color(0);
  }
  void update(){
    context.sceneMap(sceneMap);//ask SimpleOpenNI to store scene map data into our array
    Arrays.fill(scene.pixels, bg);//clear the image - fill it with the background colours
    for(int i = 0 ; i < numPixels ; i++){//loop through all pixels
      for(int u = 0 ; u < users.length; u++){//loop through user colours
        if(sceneMap[i] > 0) scene.pixels[i] = users[u];//if there are user pixels, use set user colour for those pixels (e.g. pixels with value 1 will use colours stored in users[0], pixels with value 2 will use colour from users[1], etc.) 
      } 
    }
    scene.updatePixels();//we've use pixels, so update the image at the end
  }
}

但这取决于学生是否熟悉课程,并且鉴于它是MFA,对清洁代码的压力较小,而且我认为完成工作的压力更大。