KalmanDemo.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using Emgu.CV;
  8. using Emgu.CV.Structure;
  9. using bbiwarg.Utility;
  10. using bbiwarg.Detectors.Touch;
  11. namespace bbiwarg.Graphics
  12. {
  13. class KalmanDemo
  14. {
  15. private List<Point> points, kalmanPoints;
  16. private Kalman2DPositionFilter kalman;
  17. public KalmanDemo()
  18. {
  19. points = new List<Point>();
  20. kalmanPoints = new List<Point>();
  21. }
  22. public void addPoint(Point p)
  23. {
  24. if (points.Count == 0)
  25. kalman = new Kalman2DPositionFilter(new Vector2D(p));
  26. Vector2D v = kalman.getCorrectedPosition(new Vector2D(p));
  27. points.Add(p);
  28. kalmanPoints.Add(new Point(v.IntX, v.IntY));
  29. }
  30. public void reset()
  31. {
  32. points.Clear();
  33. kalmanPoints.Clear();
  34. }
  35. public Int16[] getTextureData(int width, int height)
  36. {
  37. Image<Bgr, short> image = new Image<Bgr, short>(width, height);
  38. image.DrawPolyline(points.ToArray(), false, new Bgr(0, Int16.MaxValue, 0), 1);
  39. image.DrawPolyline(kalmanPoints.ToArray(), false, new Bgr(0, 0, Int16.MaxValue), 1);
  40. Int16[] textureData = new Int16[3 * width * height];
  41. int index = 0;
  42. for (int y = 0; y < height; ++y)
  43. {
  44. for (int x = 0; x < width; ++x)
  45. {
  46. textureData[index + 0] = image.Data[y, x, 2];
  47. textureData[index + 1] = image.Data[y, x, 1];
  48. textureData[index + 2] = image.Data[y, x, 0];
  49. index += 3;
  50. }
  51. }
  52. return textureData;
  53. }
  54. }
  55. }