FingerImage.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Emgu.CV;
  8. using Emgu.CV.Structure;
  9. namespace bbiwarg.Images
  10. {
  11. public enum FingerImageState {
  12. none = 0,
  13. possibleFinger = 1,
  14. fingerDetected = 2,
  15. fingerTracked = 3
  16. }
  17. class FingerImage
  18. {
  19. private Image<Gray, Int16> image;
  20. public FingerImage(DepthImage depthImage) {
  21. this.image = depthImage.getImage().CopyBlank();
  22. }
  23. public void setFingerAt(int x, int y, FingerImageState fis) {
  24. image.Data[y, x, 0] = (Int16) fis;
  25. }
  26. public FingerImageState getStateAt(int x, int y) {
  27. return (FingerImageState)image.Data[y, x, 0];
  28. }
  29. public void drawLine(PointF start, PointF end, FingerImageState state)
  30. {
  31. int width = image.Width;
  32. int height = image.Height;
  33. // bresenham from wikipedia
  34. int xstart = (int)start.X, xend = (int)end.X, ystart = (int)start.Y, yend = (int)end.Y;
  35. int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
  36. /* Entfernung in beiden Dimensionen berechnen */
  37. dx = xend - xstart;
  38. dy = yend - ystart;
  39. /* Vorzeichen des Inkrements bestimmen */
  40. incx = dx < 0 ? -1 : 1;
  41. incy = dy < 0 ? -1 : 1;
  42. if (dx < 0) dx = -dx;
  43. if (dy < 0) dy = -dy;
  44. /* feststellen, welche Entfernung größer ist */
  45. if (dx > dy)
  46. {
  47. /* x ist schnelle Richtung */
  48. pdx = incx; pdy = 0; /* pd. ist Parallelschritt */
  49. ddx = incx; ddy = incy; /* dd. ist Diagonalschritt */
  50. es = dy; el = dx; /* Fehlerschritte schnell, langsam */
  51. }
  52. else
  53. {
  54. /* y ist schnelle Richtung */
  55. pdx = 0; pdy = incy; /* pd. ist Parallelschritt */
  56. ddx = incx; ddy = incy; /* dd. ist Diagonalschritt */
  57. es = dx; el = dy; /* Fehlerschritte schnell, langsam */
  58. }
  59. /* Initialisierungen vor Schleifenbeginn */
  60. x = xstart;
  61. y = ystart;
  62. err = el / 2;
  63. setFingerAt(Math.Min(width - 1, Math.Max(0, x)), Math.Min(height - 1, Math.Max(0, y)), state);
  64. /* Pixel berechnen */
  65. for (t = 0; t < el; ++t) /* t zaehlt die Pixel, el ist auch Anzahl */
  66. {
  67. /* Aktualisierung Fehlerterm */
  68. err -= es;
  69. if (err < 0)
  70. {
  71. /* Fehlerterm wieder positiv (>=0) machen */
  72. err += el;
  73. /* Schritt in langsame Richtung, Diagonalschritt */
  74. x += ddx;
  75. y += ddy;
  76. }
  77. else
  78. {
  79. /* Schritt in schnelle Richtung, Parallelschritt */
  80. x += pdx;
  81. y += pdy;
  82. }
  83. setFingerAt(Math.Min(width - 1, Math.Max(0, x)), Math.Min(height - 1, Math.Max(0, y)), state);
  84. }
  85. }
  86. }
  87. }