I am doing the project where I am using the Hands example using SimpleOpenNI example to draw point in realtime on screen. I want to get the position of hand (point) in previous frame so that i can find the difference ho far the hand moved based on that the image will move. Here is my code. Please help
- import fullscreen.*;
- import japplemenubar.*;
- import processing.opengl.*;
- import SimpleOpenNI.*;
- SimpleOpenNI context;
- FullScreen fs;
- XnVSessionManager sessionManager;
- PushDetectorLab pushDetectorLab;
- SwipeDetectorLab swipeDetectorLab;
- XnVFlowRouter flowRouter;
- PointDrawer pointDrawer;
- PVector s = new PVector();
- PVector screenPos = new PVector();
- PVector v = new PVector();
- boolean activeLeft = false;
- boolean activeRight = false;
- int counter = 0;
- PFont fontA32;
- PImage shapeOfUser;
- boolean foundSkeleton = false;
- double offsetX ;
- int timeDelay = 10000; //milliseconds in one minute
- int time = 0;
- boolean blinkText = false;
- boolean bWaitText = true;
- boolean on;
- int blinkSpeed = 12;
- final float MAX_ANGLE = radians(70);
- final float interval = 500;
- final int offsetZ = 500;
- final int bgColor = color(255, 255, 255);
- Photo[] p = new Photo[8];
- PImage logo, newLogo, sky;
- PImage waveTextImage;
- PImage logo0, logo1, logo2, logo3, logo4, logo5, logo6, logo7;
- int val = 255, val1= 255, val2 = 0;
- void setup() {
- size(1024, 768, OPENGL);
- context = new SimpleOpenNI(this, SimpleOpenNI.RUN_MODE_MULTI_THREADED);
- String ONI_FILE = "";
- if (context.openFileRecording(ONI_FILE)) println("Playing 3d data from "+ONI_FILE);
- context.setMirror(true);
- if (context.enableDepth() == false)
- {
- println("Can't open the depthMap, maybe the camera is not connected!");
- exit();
- return;
- }
- context.enableHands();
- context.enableGesture();
- sessionManager = context.createSessionManager("Click,Wave", "RaiseHand");
- pushDetectorLab = new PushDetectorLab();
- swipeDetectorLab = new SwipeDetectorLab();
- pointDrawer = new PointDrawer();
- flowRouter = new XnVFlowRouter();
- flowRouter.SetActive(pointDrawer);
- sessionManager.AddListener(swipeDetectorLab);
- sessionManager.AddListener(pointDrawer);
- sessionManager.AddListener( flowRouter);
- for (int i = 0; i < p.length; i++) {
- p[i] = new Photo("img"+i+".jpg");
- p[i].nX = (i - p.length / 2) * interval;
- }
- logo = loadImage("dheeraj.png");
- newLogo = loadImage("New_dheeraj.png");
- waveTextImage = loadImage("wave_hand_icon.jpg");
- sky = loadImage("bg.jpg");
- fontA32 = createFont("Arial", 32);
- textFont(fontA32, 32);
- smooth();
- }
- void draw() {
- beginCamera();
- camera();
- background(0);
- context.update();
- context.update(sessionManager);
- pointDrawer.draw();
- endCamera();
- camera(0, 0, 1200, 0, 0, 0, 0, 1, 0);
- double minX = 9999;
- noTint();
- for (int i = 0; i < p.length; i++) {
- p[i].update();
- if (abs((float) p[i].nX) < abs((float) minX)) minX = p[i].nX;
- }
- // here i want to save value the value of point in previous frame
- v.x = screenPos.x // for each frame the value of hand position will be saved
- /* if(frameCount % 30 == 0){
- v.x = screenPos.x;
- } */
- if (activeLeft == true) {
- offsetX = 6 * (screenPos.x - v.x); // value of current point - value of point in previous frame
- }
- else if (activeRight == true) {
- offsetX = 6 * (screenPos.x + v.x); // value of current point + value of point in previous frame
- }
- else {
- offsetX = - minX * 0.1;
- }
- for (int i = 0; i < p.length; i++) {
- p[i].nX += offsetX;
- break;
- }
- }
- // -----------------------------------------------------------------
- // SimpleOpenNI events
- void onNewUser(int userId)
- {
- println("onNewUser - userId: " + userId);
- println(" start pose detection");
- context.startPoseDetection("Psi", userId);
- }
- void onLostUser(int userId)
- {
- println("onLostUser - userId: " + userId);
- }
- void onStartCalibration(int userId)
- {
- println("onStartCalibration - userId: " + userId);
- }
- void onEndCalibration(int userId, boolean successfull)
- {
- println("onEndCalibration - userId: " + userId + ", successfull: " + successfull);
- if (successfull)
- {
- println(" User calibrated !!!");
- context.startTrackingSkeleton(userId);
- }
- else
- {
- println(" Failed to calibrate user !!!");
- println(" Start pose detection ");
- context.startPoseDetection("Psi", userId);
- }
- }
- void onStartPose(String pose, int userId)
- {
- println("onStartPose - userId: " + userId + ", pose: " + pose);
- println(" stop pose detection");
- context.stopPoseDetection(userId);
- context.requestCalibrationSkeleton(userId, true);
- }
- void onEndPose(String pose, int userId)
- {
- println("onEndPose - userId: " + userId + ", pose: " + pose);
- }
- //////////////////////////////////////////////////
- ///////////////////////////////////////////////////
- // session callbacks
- void onStartSession(PVector pos)
- {
- println("onStartSession: " + pos);
- context.removeGesture("Wave,Click");
- foundSkeleton = true;
- }
- void onEndSession()
- {
- println("onEndSession: ");
- context.addGesture("Wave, Click");
- }
- void onFocusSession(String strFocus, PVector pos, float progress)
- {
- println("onFocusSession: focus=" + strFocus + ",pos=" + pos + ",progress=" + progress);
- }
- /////////////////
- // HANDS
- // -----------------------------------------------------------------
- // hand events
- void onCreateHands(int handId, PVector pos, float time)
- {
- println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
- foundSkeleton = true;
- }
- void onUpdateHands(int handId, PVector pos, float time)
- {
- println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
- foundSkeleton = true;
- }
- void onDestroyHands(int handId, float time)
- {
- println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
- foundSkeleton = false;
- }
here is the pointDrawer class.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// PointDrawer keeps track of the handpoints
-
class PointDrawer extends XnVPushDetector{HashMap _pointLists;int _maxPoints;color[] _colorList = {color(255, 0, 0), color(0, 255, 0), color(0, 0, 255), color(255, 255, 0)};
int pushed = 0;
public PointDrawer(){_maxPoints = 30;_pointLists = new HashMap();
RegisterPush(this);}
public void OnPointCreate(XnVHandPointContext cxt){// create a new listaddPoint(cxt.getNID(), new PVector(cxt.getPtPosition().getX(), cxt.getPtPosition().getY(), cxt.getPtPosition().getZ()));
println("onPointCreate " + cxt.getFTime() +":"+cxt.getNUserID()+"("+cxt.getNID() +") "+cxt.getPtPosition().getX() + " " +cxt.getPtPosition().getY()+" " +cxt.getPtPosition().getZ());}
public void OnPointUpdate(XnVHandPointContext cxt){/*println("onPointUpdate " + cxt.getFTime() +":"+cxt.getNUserID()+"("+cxt.getNID() +") "+cxt.getPtPosition().getX() + " " +cxt.getPtPosition().getY()+" " +cxt.getPtPosition().getZ());*///println("OnPointUpdate " + cxt.getPtPosition());addPoint(cxt.getNID(), new PVector(cxt.getPtPosition().getX(), cxt.getPtPosition().getY(), cxt.getPtPosition().getZ()));}
public void OnPointDestroy(long nID){println("OnPointDestroy, handId: " + nID);
// remove listif (_pointLists.containsKey(nID))_pointLists.remove(nID);}
void onPush(float vel, float angle) {println(">>>>>>>>> PUSH v:" + vel + "a: " + angle);pushed = 10;// println("ID = " + GetPrimaryID()+" immduration: " + GetPushImmediateDuration()+" immoffset: "+GetPushImmediateOffset() );}
public ArrayList getPointList(long handId){ArrayList curList;if (_pointLists.containsKey(handId))curList = (ArrayList)_pointLists.get(handId);else{curList = new ArrayList(_maxPoints);_pointLists.put(handId, curList);}return curList;}
public void addPoint(long handId, PVector handPoint){ArrayList curList = getPointList(handId);
curList.add(0, handPoint);if (curList.size() > _maxPoints)curList.remove(curList.size() - 1);}
public void draw(){
if (_pointLists.size() <= 0)return;
pushStyle();noFill();
PVector vec;PVector firstVec;// PVector screenPos = new PVector();int colorIndex=0;
// draw the hand listsIterator<Map.Entry> itrList = _pointLists.entrySet().iterator();while (itrList.hasNext ()){strokeWeight(2);stroke(_colorList[colorIndex % (_colorList.length - 1)]);
ArrayList curList = (ArrayList)itrList.next().getValue();
// draw linefirstVec = null;Iterator<PVector> itr = curList.iterator();beginShape();while (itr.hasNext ()){vec = itr.next();if (firstVec == null)firstVec = vec;// calc the screen poscontext.convertRealWorldToProjective(vec, screenPos);//vertex(screenPos.x, screenPos.y);}endShape();
// draw current pos of the handif (firstVec != null){//active = true;strokeWeight(8);context.convertRealWorldToProjective(firstVec, screenPos);point(screenPos.x, screenPos.y);s = screenPos;if (pushed > 0) {// rect(screenPos.x, screenPos.y, 10, 10);pushed --;}}colorIndex++;}
popStyle();}}
The Photo class which does places the images in the order
class Photo {
// int w = 640;// int h = 480;int width = 640;int height = 480;
double x;double z;private double angle;double nX;private double rate = 4.0;
PImage img;PImage shadowImg;
Photo(String fileName) {
img = loadImage(fileName);if (img == null) {img = createImage(300, 300, RGB);for (int i = 0; i < img.pixels.length; i++) {img.pixels[i] = 0xFFFFFFFF;}}shadowImg = createImage(img.width, img.height, RGB);
float a = 100;float dA = - (float)0xFF / img.height;
for (int iy = 0; iy < shadowImg.height; iy++) {for (int ix = 0; ix < shadowImg.width; ix++) {if ( a < 0 ) a = 0;int i = iy * shadowImg.width + ix;int c = img.pixels[(img.height - iy - 1)*img.width + ix];
int fg = c;int fR = (0x00FF0000 & fg) >>> 16;int fG = (0x0000FF00 & fg) >>> 8;int fB = 0x000000FF & fg;
int bR = (0x00FF0000 & bgColor) >>> 16;int bG = (0x0000FF00 & bgColor) >>> 8;int bB = 0x000000FF & bgColor;
int rR = (fR * (int)a + bR * (int)(0xFF - a)) >>> 8;int rG = (fG * (int)a + bG * (int)(0xFF - a)) >>> 8;int rB = (fB * (int)a + bB * (int)(0xFF - a)) >>> 8;
shadowImg.pixels[i] = 0xFF000000 | (rR << 16) | (rG << 8) | rB;}a += dA;}}
void update() {z = 0;angle = MAX_ANGLE;if (nX < 0) angle = -angle;noStroke();fill(255);if (abs((float)x) <= interval + 0.1) {x = nX;angle = MAX_ANGLE * x / interval;z = offsetZ * (interval - abs((float)nX))/interval;}else {x = nX;double offset = interval;if (nX < 0) offset = -offset;x -= offset;x /= rate;x += offset;}
pushMatrix();translate((float)x, 0, (float)z);rotateY(-(float)angle);
int n = 20;float dW = (float)this.width / n;float dTexW = (float)img.width / n;
/* lights();beginShape(QUAD_STRIP);texture(img);for (int i = 0; i <= n; i++) {
// vertex(-this.width / 2.0 + i*dW, -this.height / 2.0, i*dTexW, 0);// vertex(-this.width / 2.0 + i*dW, this.height / 2.0, i*dTexW, img.height);
vertex(-250 + i*dW, 0, i*dTexW, 0);vertex(-250 + i*dW, 568, i*dTexW, img.height);}endShape();
noLights();beginShape(QUAD_STRIP);texture(shadowImg);for (int i = 0; i <= n; i++) {//vertex(-this.width / 2.0 + i*dW, this.height / 2.0, i*dTexW, 0);// vertex(-this.width / 2.0 + i*dW, this.height * 3.0 / 2.0, i*dTexW, shadowImg.height);vertex(-250 + i*dW, 568, i*dTexW, 0);vertex(-250 + i*dW, 1168, i*dTexW, shadowImg.height);}endShape(); */lights();beginShape(QUAD_STRIP);texture(img);for(int i = 0; i <= n; i++) {vertex(-this.width / 2.0 + i*dW, -this.height / 2.0, i*dTexW, 0);vertex(-this.width / 2.0 + i*dW, this.height / 2.0, i*dTexW, img.height);}endShape();noLights();beginShape(QUAD_STRIP);texture(shadowImg);for(int i = 0; i <= n; i++) {vertex(-this.width / 2.0 + i*dW, this.height / 2.0, i*dTexW, 0);vertex(-this.width / 2.0 + i*dW, this.height * 3.0 / 2.0, i*dTexW, shadowImg.height);}endShape();
popMatrix();}}
1