FingerImage.cs 3.2 KB

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