FingerImage.cs 3.3 KB

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