試著寫出揮手的手勢辨識
import SimpleOpenNI.*;
SimpleOpenNI openni;
void setup() {
size(640, 480);
openni = new SimpleOpenNI(this);
openni.enableRGB();
openni.enableDepth();
openni.enableUser();
}
void draw(){
openni.update();
background(255);
image(openni.userImage(), 2,2.4,640,480);
int [] userList = openni.getUsers();
PVector pos = new PVector(0,0,0);
PVector pos2 = new PVector(0,0,0);
for(int userID : userList){
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_RIGHT_HAND, pos);
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_HEAD, pos2);
}
ellipse(pos.x, pos.y, 50,50);
ellipse(pos2.x, pos2.y, 50,50);
println(pos);
Wave();
}
void Wave(){
int [] userList = openni.getUsers();
PVector pos = new PVector(0,0,0);
PVector pos2 = new PVector(0,0,0);
for(int userID : userList){
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_RIGHT_HAND, pos);
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_HEAD, pos2);
}
if(pos.x>pos2.x+0.5)
{
println("WAVE");
}
}
void onNewUser(SimpleOpenNI openni,int userId)
{
openni.startTrackingSkeleton(userId);
}
加上王冠甲的程式碼
import SimpleOpenNI.*;
SimpleOpenNI openni;
color[] userClr = new color[]{ color(255,0,0),
color(0,255,0),
color(0,0,255),
color(255,255,0),
color(255,0,255),
color(0,255,255)
};
PVector com = new PVector();
PVector com2d = new PVector();
void setup()
{
size(640,480);
openni = new SimpleOpenNI(this);
if(openni.isInit() == false)
{
println("Can't init SimpleOpenNI, maybe the camera is not connected!");
exit();
return;
}
openni.enableDepth();
openni.enableUser();
background(200,0,0);
stroke(0,0,255);
strokeWeight(3);
smooth();
}
void draw()
{
openni.update();
PVector pos = new PVector(0,0,0), hand1=new PVector(0,0,0), hand2=new PVector(0,0,0);
int[] userList = openni.getUsers();
image(openni.userImage(),0,0);
for(int userID : userList){
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_LEFT_HAND, pos);
openni.convertRealWorldToProjective(pos, pos);
fill(0,255,0);ellipse(pos.x,pos.y,50,50); hand1.set(pos); hand1.z=0;
fill(0,255,0);ellipse(pos.x/10, pos.y/10, 5,5);
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_RIGHT_HAND, pos);
openni.convertRealWorldToProjective(pos, pos);
fill(0,255,0);ellipse(pos.x/10,pos.y/10, 5,5);
fill(0,255,0);ellipse(pos.x, pos.y,50,50); hand2.set(pos); hand2.z=0;
}
for(int i=0;i<userList.length;i++)
{
if(openni.isTrackingSkeleton(userList[i]))
{
stroke(userClr[ (userList[i] - 1) % userClr.length ] );
drawSkeleton(userList[i]);
}
if(openni.getCoM(userList[i],com))
{
openni.convertRealWorldToProjective(com,com2d);
stroke(100,255,0);
strokeWeight(1);
beginShape(LINES);
vertex(com2d.x,com2d.y - 5);
vertex(com2d.x,com2d.y + 5);
vertex(com2d.x - 5,com2d.y);
vertex(com2d.x + 5,com2d.y);
endShape();
fill(0,255,100);
text(Integer.toString(userList[i]),com2d.x,com2d.y);
}
}
Wave();
println(pos.x);
}
void drawSkeleton(int userId)
{
openni.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
openni.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
openni.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
openni.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
openni.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
openni.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
openni.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
openni.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
openni.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
openni.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
openni.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
openni.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
openni.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
openni.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
openni.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
}
void onNewUser(SimpleOpenNI curContext, int userId)
{
println("onNewUser - userId: " + userId);
println("\tstart tracking skeleton");
curContext.startTrackingSkeleton(userId);
}
void Wave(){
int [] userList = openni.getUsers();
PVector pos = new PVector(0,0,0);
PVector pos2 = new PVector(0,0,0);
for(int userID : userList){
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_RIGHT_HAND, pos);
openni.getJointPositionSkeleton(userID,SimpleOpenNI.SKEL_HEAD, pos2);
}
if(pos.x>pos2.x+0.5)
{
println("WAVE");
}
}