Kinect。我如何与多个质量中心合作,而不会相互影响其他功能?

问题描述:

我目前在美术学士学位课程,最近我开始尝试通过阅读Greg Borenstein的“Making Things See”来学习编程。Kinect。我如何与多个质量中心合作,而不会相互影响其他功能?

我正在开发的作品是试图追踪多个质心,因为观众在使用Kinect时在画廊空间中移动。我希望观众在屏幕上留下痕迹,当他们靠近某些区域时,他们的接近将通过线或其他方式显示出来。我设法做出一条线索,但一旦有人进入视线,他们的关键点就会突然相连。 '邻近线'也似乎只适用于当前用户,而不适用于以前的用户。

我想我的问题真的归结为如何隔离每个新用户,以便我可以创建适用于所有这些函数或类但不相互干扰的函数或类。

这里的程序至今...

import processing.opengl.*; 
import SimpleOpenNI.*; 
import peasy.*; 

PeasyCam cam; 
SimpleOpenNI kinect; 

ArrayList<PVector> trails; 

Hotpoint piece; 

PVector currentPosition; 
PVector previousPosition; 

int pieceX = 0; 
int pieceY = 0; 
int pieceZ = 2000; 
int pieceSize = 500; 

void setup() { 
    size(1280, 680, OPENGL); 
    kinect = new SimpleOpenNI(this); 
    kinect.enableDepth(); 
    kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_NONE); 
    kinect.setMirror(true); 

    trails = new ArrayList(); 

    piece = new Hotpoint(pieceX, pieceY, pieceZ, pieceSize); 

    cam = new PeasyCam(this, 0, 0, 0, 1000); 
} 

void draw() { 
    background(255); 
    kinect.update(); 
    rotateX(radians(180)); 

    lights(); 

    stroke(0); 
    strokeWeight(3); 

    IntVector userList = new IntVector(); 
    kinect.getUsers(userList); 

    piece.draw(); 

    for (int i=0; i<userList.size(); i++) { 
    int userId = userList.get(i); 

    PVector positionCenter = new PVector(); 
    kinect.getCoM(userId, positionCenter); 

    trails.add(positionCenter); 

    createTrail(); 

    piece.check(positionCenter); 

    if(piece.check(positionCenter) == true) { 
     stroke(255, 0, 0); 
     line(positionCenter.x, positionCenter.y, positionCenter.z, 
      pieceX, pieceY, pieceZ); 
     stroke(0); 
    } 
    } 
} 

void createTrail() { 
    for (int e=1; e < trails.size(); e++) { 
    currentPosition = trails.get(e); 
    previousPosition = trails.get(e-1); 
    if (currentPosition.z < 1) { 
     trails.clear(); 
    } 
    else { 
     stroke(0); 
     line(previousPosition.x, previousPosition.y, previousPosition.z, 
     currentPosition.x, currentPosition.y, currentPosition.z); 
    } 
    } 
} 

这是HOTPOINT类部分...

class Hotpoint { 
    PVector center; 
    color fillColor; 
    color strokeColor; 
    int size; 
    int pointsIncluded; 
    int maxPoints; 
    boolean wasJustHit; 
    int threshold; 


    Hotpoint(float centerX, float centerY, float centerZ, int boxSize) { 
    center = new PVector(centerX, centerY, centerZ); 
    size = boxSize; 
    pointsIncluded = 0; 
    maxPoints = 1000; 
    threshold = 0; 

    strokeColor = color(random(255), random(255), random(255)); 
    fillColor = 0; 
    } 

    void setThreshold(int newThreshold){ 
    threshold = newThreshold; 
    } 

    void setMaxPoints(int newMaxPoints){ 
    maxPoints = newMaxPoints; 
    } 

    void setColor(float red, float blue, float green){ 
    fillColor = strokeColor = color(red, blue, green); 
    } 

    boolean check(PVector point) { 
    boolean result = false; 

    if (point.x > center.x - size/2 && point.x < center.x + size/2) { 
     if (point.y > center.y - size/2 && point.y < center.y + size/2) { 
     if (point.z > center.z - size/2 && point.z < center.z + size/2) { 
      result = true; 
      pointsIncluded++; 
     } 
     } 
    } 

    return result; 
    } 

    void draw() { 
    pushMatrix(); 
     translate(center.x, center.y, center.z); 
     shapeMode(LINES); 
     noFill(); 
     stroke(red(strokeColor), blue(strokeColor), green(strokeColor), 255); 
     box(size); 
    popMatrix(); 
    } 


    float percentIncluded() { 
    return map(pointsIncluded, 0, maxPoints, 0, 1); 
    } 


    boolean currentlyHit() { 
    return (pointsIncluded > threshold); 
    } 


    boolean isHit() { 
    return currentlyHit() && !wasJustHit; 
    } 

    void clear() { 
    wasJustHit = currentlyHit(); 
    pointsIncluded = 0; 
    } 
} 

任何帮助将非常感激!


编辑:

谢谢你这么多的时间和你的答案@ jesses.co.tt但我一直无法理解它... 例如,是在环userList与用户数组不一样吗? 我很担心我一次只问一件事,所以我已经把它分解开来,试图首先了解没有人联系的多条路线图。

import processing.opengl.*; 
import SimpleOpenNI.*; 
import peasy.*; 

SimpleOpenNI kinect; 
PeasyCam cam; 

ArrayList<PVector> trails1; 
ArrayList<PVector> trails2; 

PVector currentPosition; 
PVector previousPosition; 

void setup() { 
    size(1280, 800, OPENGL); 

    kinect = new SimpleOpenNI(this); 
    kinect.enableDepth(); 
    kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_NONE); 
    kinect.setMirror(true); 

    trails1 = new ArrayList(); 
    trails2 = new ArrayList(); 

    cam = new PeasyCam(this, 0, 0, 0, 1000); 
} 

void draw() { 
    kinect.update(); 
    rotateX(radians(180)); 
    background(255); 

    IntVector userList = new IntVector(); 
    kinect.getUsers(userList); 

    //println(userList); 

    for (int i=0; i<userList.size(); i++) { 
    int userId = userList.get(i); 
    //println(userId); 
    PVector positionCenter = new PVector(); 
    kinect.getCoM(userId, positionCenter); 

    stroke(0); 
    strokeWeight(10); 
    point(positionCenter.x, positionCenter.y, positionCenter.z); 

    if (userId == 1) { 

     trails1.add(positionCenter); 
     createTrail1(); 
    } 
    else if (userId == 2) { 
     trails2.add(positionCenter); 
     createTrail2(); 
    } 
    } 
} 

void createTrail1() { 
    for (int e=1; e < trails1.size(); e++) { 
    currentPosition = trails1.get(e); 
    previousPosition = trails1.get(e-1); 
    if (currentPosition.z < 1) { // [possibly x or y or all?] 
     trails1.clear(); 
    } 
    else { 
     // if (currentPosition.x != 0 || previousPosition.x != 0) { // [not working] 
     stroke(0); 
     line(previousPosition.x, previousPosition.y, previousPosition.z, 
     currentPosition.x, currentPosition.y, currentPosition.z); 
     //trails.clear(); 
    } 
    } 
} 

void createTrail2() { 
    for (int e=1; e < trails2.size(); e++) { 
    currentPosition = trails2.get(e); 
    previousPosition = trails2.get(e-1); 
    if (currentPosition.z < 1) { // [possibly x or y or all?] 
     trails2.clear(); 
    } 
    else { 
     // if (currentPosition.x != 0 || previousPosition.x != 0) { // [not working] 
     stroke(0); 
     line(previousPosition.x, previousPosition.y, previousPosition.z, 
     currentPosition.x, currentPosition.y, currentPosition.z); 
     //trails.clear(); 
    } 
    } 
} 

那么,这会工作的两个人,我可以写一个很长的窘况方案,为大型有限数量的人工作,但我真的希望是,它是动态的... 'if(userId == 1){',我希望它适用于每个人,然后在步道部分,需要有一系列新的路径,以便每当新人进入时查看我会使用'void onNewUser(int userId){'或其他...?

+0

哎呀,我忘了,包括对HOTPOINT类,所以这是被编辑在现在! – basicallyright 2013-04-24 14:14:37

据我所知,你需要制作一个可调整大小的用户列表 - 这个列表已经由SimpleOpenNI提供给你 - 并且为你找到的每个新用户创建一个HotPoint类的新实例(或者将其删除即消失)就快,因为你是在已知用户的列表中正确遍历每个用户...

...

您需要添加根据调整HotPoints的ArrayList找到的用户数量...然后在找到新用户时创建点的新实例。你现在拥有的方式是,你只有一个实例,所以如果有两个用户,它会连接它们 - 这就是你所看到的。

确保根据需要从ArrayList中删除项目!

你真的很接近...... ;-)

所以......

// Global Variables 
ArrayList users; 

然后,在你的for循环在用户...

// Add a new HotPoint if we have more users than Points 
if(i > users.size()) { 
    users.add(new HotPoint(...)); 
} 
// Delete From ArrayList if we have too many... 
else if(users.size() > userList.size()) { 
    users.remove(...); 
} 

// Cast and call methods 
HotPoint piece = (HotPoint) users.get(i); 
piece.check(positionCenter); 
piece.draw(); 
etc... 
+0

基本正确 - 为您工作吗? – 2013-04-26 15:17:19

+0

非常感谢您的回答@ jesses.co.tt 这绝对是令人鼓舞的,尽管我仍然无法理解这一切。 我已经编辑了这个问题,希望能够将我的问题更多地分解下来...... 如果您可以进一步提供帮助,它会非常棒! – basicallyright 2013-05-04 15:54:33