static bool FingerTracking(out int xFinger, out int yFinger, out int zFinger, out int NoseX, out int NoseY, out int NoseZ , out int SkullX, out int SkullY, out int SkullZ) { int SkullMin, SkullMax = -3; xFinger = -1; yFinger = -1; zFinger = -1; NoseX = -1; NoseY = -1; NoseZ = -1; SkullX = -1; SkullY = -1; SkullZ = -1; int xf = -1, yf = -1, zf = -1; int x, y, z = 10000; int xp1, xp3, yp1, yp3, l, dl; int NoseSearchArea = 150; // (int)(z12 * Graph.width / 3); int zBase; if (zNose > 0) { zBase = zNose; } else { zBase = zLeftEye; } bool FingerOrNose = true, Exit = false; if (Show) { for (int i = xLeftEye > 20 ? xLeftEye - 20 : 0; i < x1; i++) { for (int j = yLeftEye > 50 ? yLeftEye - 50 : 0; j < y3; j++) { Graph.putpixel(Graph.width - x1 + i, Graph.height - y3 + j, GKinect.GetColorPixel(i, j)); } } Graph.setcolor(Color.White); Graph.line(Graph.width - x1 + x1, Graph.height - y3 + y1, Graph.width - x1 + x3, Graph.height - y3 + y3); Graph.line(Graph.width - x1 + x1, Graph.height - y3 + y1, Graph.width - x1 + x2, Graph.height - y3 + y2); Graph.line(Graph.width - x1 + x3, Graph.height - y3 + y3, Graph.width - x1 + x4, Graph.height - y3 + y4); Graph.line(Graph.width - x1 + x2, Graph.height - y3 + y2, Graph.width - x1 + x4, Graph.height - y3 + y4); Graph.setcolor(Color.Yellow); Graph.line(Graph.width - x1 + x1, Graph.height - y3 + y1, Graph.width - x1 + xLeftEye, Graph.height - y3 + yLeftEye); Graph.line(Graph.width - x1 + x2, Graph.height - y3 + y2, Graph.width - x1 + xLeftEye, Graph.height - y3 + yLeftEye); Graph.line(Graph.width - x1 + x3, Graph.height - y3 + y3, Graph.width - x1 + xLeftEye, Graph.height - y3 + yLeftEye); Graph.line(Graph.width - x1 + x4, Graph.height - y3 + y4, Graph.width - x1 + xLeftEye, Graph.height - y3 + yLeftEye); Graph.setcolor(Color.Blue); //Graph.setfont("Times New Roman", FontStyle.Bold, 20); Graph.outtextxy(Graph.width - 200, Graph.height - 30, "Kinect Camera"); //Graph.outtextxy(xLeftEye, yLeftEye - 20, "EYE: X=" + xLeftEye + " Y=" + yLeftEye + " Z=" + zLeftEye); } l = (int)Math.Sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1)); int xLast = (int)(xLeftEye - 10 / PixelLength); if (xLast < 0) { xLast = 0; } for (xp1 = x1; (xp1 > xLast) && (!Exit); xp1--) { double d = 1.0 * (xp1 - xLeftEye) / (x1 - xLeftEye); //if (d < 0.2) break; // End of Refresh Area near to eye if (FingerOrNose) { if (d < 0.25) { FingerOrNose = false; SkullMax = -3; continue; } else { } // End of Refresh Area for fingers (near to eyes) } else if (d >= 0.25) { continue; } yp1 = (int)(yLeftEye + d * (y1 - yLeftEye)); if (FingerOrNose) { xp3 = (int)(xLeftEye + d * (x3 - xLeftEye)); yp3 = (int)(yLeftEye + d * (y3 - yLeftEye)); } else { xp3 = (int)(xp1 + x13 * 100 / PixelLength); yp3 = (int)(yp1 + y13 * 100 / PixelLength); } int TempX = (xp1 - xp3); TempX *= TempX; int TempY = (yp1 - yp3); TempY *= TempY; l = (int)Math.Sqrt(TempX + TempY); SkullMin = FingerOrNose ? 0 : -l; if (FingerOrNose) { SkullMax = l; } else if (SkullMax == -3) { SkullMax = l; } for (dl = SkullMin; dl <= SkullMax; dl++) { double dll = 1.0 * dl / l; x = (int)(xp1 + dll * (xp3 - xp1)); if (x > 640) { x = 639; } y = (int)(yp1 + dll * (yp3 - yp1)); if (y < 0) { y = 0; } z = GKinect.GetDepthPixel(x, y, MapToCamera.Color); if (FingerOrNose) { if (z != -1 && z >= z1 && z <= z2) { if (xf == -1 && yf == -1 && zf == -1) { if (Show) { Graph.setcolor(Color.Yellow); Graph.circle(Graph.width - x1 + x, Graph.height - y3 + y, 5); } xf = x; yf = y; zf = z; _KinectToMonitor(xf, yf, zf, out xFinger, out yFinger, out zFinger); FingerOrNose = false; SkullMax = -3; break; } } } else if (z != -1 && z >= zBase - NoseSearchArea * 2 && z <= zBase + NoseSearchArea) { if (NoseX == -1 && NoseY == -1 && NoseZ == -1) { NoseX = x; NoseY = y; NoseZ = z; //Exit = true; break; } SkullX = x; SkullY = y; SkullZ = z; SkullMax = dl - 1; break; } } } if (NoseX == -1 && NoseY == -1 && NoseZ == -1) { if (!KinectCalibration()) { Graph.closegraph(); } } if (Show) { Graph.setcolor(Color.Yellow); Graph.circle(Graph.width - x1 + SkullX, Graph.height - y3 + SkullY, 5); } if (xFinger >= 0 && xFinger < Graph.width && yFinger >= 0 && yFinger < Graph.height) { return(true); } return(false); }
static bool _ResetMonitorPoints() { int xLeft = (Graph.width - 640) / 2; int yTop = (Graph.height - 480) / 2; int xRight = Graph.width - xLeft; int yDown = Graph.height - yTop; int xm, ym, zm, click, turn = 1; string str = "Click on Top-Left point of screen!"; Graph.getmouse(out xm, out ym, out click); Graph.setfont("Times New Roman", FontStyle.Bold, 28); while (!Graph.keydown(Keys.Enter) && (!loaded)) { Graph.putimage(wall, 0, 0, Graph.width, Graph.height); for (int i = 0; i < 640; i++) { for (int j = 0; j < 480; j++) { Color col = GKinect.GetColorPixel(i, j); Graph.putpixel(i + xLeft, j + yTop, col); } } Graph.setcolor(Color.Black); Graph.outtextxy(0, 0, "Correct Kinect Vertical camera angle by UP and DOWN and then press Enter."); Graph.outtextxy(Graph.width - 200, Graph.height - 30, "Kinect Camera"); Graph.delay(0); if (Graph.keydown(Keys.Up)) { if (KinectAngle < 27) { KinectAngle += 2; } CameraVerticalAngle = KinectAngle; } if (Graph.keydown(Keys.Down)) { if (KinectAngle > -27) { KinectAngle -= 2; } CameraVerticalAngle = KinectAngle; } if (Graph.keydown(Keys.Escape)) { return(false); } } if (loaded) { str = "Put your 3D glass and then click on Your eye!"; turn = 5; } while (turn < 6) { Graph.putimage(wall, 0, 0, Graph.width, Graph.height); for (int i = 0; i < 640; i++) { for (int j = 0; j < 480; j++) { Graph.putpixel(i + xLeft, j + yTop, GKinect.GetColorPixel(i, j)); } } Graph.setcolor(Color.Black); Graph.setfont("Times New Roman", FontStyle.Bold, 28); Graph.outtextxy(Graph.width - 200, Graph.height - 30, "Kinect Camera"); Graph.setfont("Times New Roman", FontStyle.Bold, 28); Graph.outtextxy(0, 0, str); Graph.getmouse(out xm, out ym, out click); zm = -1; if (xm >= xLeft && xm < xRight && ym >= yTop && ym < yDown) { Graph.setcolor(Color.Yellow); Graph.line(xm, yTop, xm, ym - 6); Graph.line(xm - 1, yTop, xm - 1, ym - 26); Graph.line(xm + 1, yTop, xm + 1, ym - 26); Graph.line(xm, yDown, xm, ym + 6); Graph.line(xm + 1, yDown, xm + 1, ym + 26); Graph.line(xm - 1, yDown, xm - 1, ym + 26); Graph.line(xLeft, ym, xm - 6, ym); Graph.line(xLeft, ym + 1, xm - 26, ym + 1); Graph.line(xLeft, ym - 1, xm - 26, ym - 1); Graph.line(xm + 6, ym, xRight, ym); Graph.line(xm + 26, ym + 1, xRight, ym + 1); Graph.line(xm + 26, ym - 1, xRight, ym - 1); Graph.setcolor(Color.White); Graph.circle(xm, ym, 5); Graph.circle(xm, ym, 6); if (xm - 40 / 3 >= xLeft && xm + 40 / 3 < xRight && ym - 40 / 3 >= yTop && ym + 40 / 3 < yDown) { zm = GKinect.GetDepthPixel(xm - xLeft, ym - yTop, MapToCamera.Color); Graph.setfont("Times New Roman", FontStyle.Bold, 16); Graph.setcolor(Color.Blue); Graph.outtextxy(xm - 160, ym - 30, "Distance = " + zm / 10.0 + " cm"); for (int i = -40; i < 40; i++) { for (int j = -40; j < 40; j++) { if (i * i + j * j < 1600) { Graph.putpixel(xm - 50 + i, ym + 50 + j, GKinect.GetColorPixel(xm - xLeft + i / 3, ym - yTop + j / 3)); } } } if (zm == -1) { Graph.setcolor(Color.Red); } else { Graph.setcolor(Color.White); } Graph.circle(xm - 50, ym + 50, 40); Graph.circle(xm - 50, ym + 50, 41); if (zm == -1) { Graph.setcolor(Color.Red); } else { Graph.setcolor(Color.Yellow); } Graph.line(xm - 90, ym + 50, xm - 10, ym + 50); Graph.line(xm - 50, ym + 10, xm - 50, ym + 90); } } switch (turn) { case 1: if (click == 1 && zm != -1) { x1 = xm - xLeft; y1 = ym - yTop; z1 = zm; str = "Click on Down-Left point of screen!"; turn++; while (click != 0) { Graph.getmouse(out xm, out ym, out click); } } break; case 2: Graph.setcolor(Color.Blue); for (int r = 0; r <= 5; r++) { Graph.circle(x1 + xLeft, y1 + yTop, r); } if (click == 1 && zm != -1) { x3 = xm - xLeft; y3 = ym - yTop; z3 = zm; str = "Click on Top-Right point of screen!"; turn++; while (click != 0) { Graph.getmouse(out xm, out ym, out click); } } break; case 3: Graph.setcolor(Color.Blue); for (int r = 0; r <= 5; r++) { Graph.circle(x1 + xLeft, y1 + yTop, r); Graph.circle(x3 + xLeft, y3 + yTop, r); } if (click == 1 && zm != -1) { x2 = xm - xLeft; y2 = ym - yTop; z2 = zm; str = "Click on Down-Right point of screen!"; turn++; while (click != 0) { Graph.getmouse(out xm, out ym, out click); } } break; case 4: Graph.setcolor(Color.Blue); for (int r = 0; r <= 5; r++) { Graph.circle(x1 + xLeft, y1 + yTop, r); Graph.circle(x2 + xLeft, y2 + yTop, r); Graph.circle(x3 + xLeft, y3 + yTop, r); } if (click == 1 && zm != -1) { x4 = xm - xLeft; y4 = ym - yTop; z4 = zm; //dxLeftEye = xLeftEye - xHead; dyLeftEye = yLeftEye - yHead; dzLeftEye = zLeftEye - zHead; str = "Put your 3D glass and then click on Your eye!"; turn++; while (click != 0) { Graph.getmouse(out xm, out ym, out click); } } break; case 5: Graph.setcolor(Color.Blue); for (int r = 0; r <= 5; r++) { Graph.circle(x1 + xLeft, y1 + yTop, r); Graph.circle(x2 + xLeft, y2 + yTop, r); Graph.circle(x3 + xLeft, y3 + yTop, r); Graph.circle(x4 + xLeft, y4 + yTop, r); } if (click == 1 && zm != -1) { xLeftEye = xm - xLeft; yLeftEye = ym - yTop; zLeftEye = zm; SeenEyeX = xLeftEye; SeenEyeY = yLeftEye; SeenEyeZ = zLeftEye; //dxLeftEye = xLeftEye - xHead; dyLeftEye = yLeftEye - yHead; dzLeftEye = zLeftEye - zHead; turn++; while (click != 0) { Graph.getmouse(out xm, out ym, out click); } WriteMonitorIntoFile(); } break; } Graph.delay(0); if (Graph.keydown(Keys.Escape)) { while (click != 0) { Graph.getmouse(out xm, out ym, out click); } return(false); } } return(true); }