Kalman2DPositionFilter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Emgu.CV;
  7. using Emgu.CV.Structure;
  8. namespace bbiwarg.Utility
  9. {
  10. class Kalman2DPositionFilter
  11. {
  12. private Kalman kalman;
  13. private float measurementNoiseFactor, processNoiseFactor;
  14. public bool Initialized { get; private set; }
  15. // state: x, y, v_x, v_y
  16. // x: current x position
  17. // y: current y position
  18. // v_x: velocity in x direction
  19. // v_y: velocity in y direction
  20. public Kalman2DPositionFilter(float measurementNoiseFactor = 1.0e-1f, float processNoiseFactor = 1.0e-4f)
  21. {
  22. this.measurementNoiseFactor = measurementNoiseFactor;
  23. this.processNoiseFactor = processNoiseFactor;
  24. reset();
  25. }
  26. public void reset()
  27. {
  28. // 4 state variables and 2 measurements (0 controls)
  29. kalman = new Kalman(4, 2, 0);
  30. // transition matrix
  31. Matrix<float> transitionMatrix = new Matrix<float>(new float[,]
  32. { {1.0f, 0.0f, 1.0f, 0.0f}, // x = x + v_x
  33. {0.0f, 1.0f, 0.0f, 1.0f}, // y = y + v_y
  34. {0.0f, 0.0f, 1.0f, 0.0f}, // v_x = v_x
  35. {0.0f, 0.0f, 0.0f, 1.0f}}); // v_y = v_y
  36. kalman.TransitionMatrix = transitionMatrix;
  37. // measurement matrix
  38. Matrix<float> measurementMatrix = new Matrix<float>(new float[,]
  39. { {1.0f, 0.0f, 0.0f, 0.0f}, // first measurement = x
  40. {0.0f, 1.0f, 0.0f, 0.0f} // second measurement = y
  41. });
  42. kalman.MeasurementMatrix = measurementMatrix;
  43. // measurement noise covariance matrix
  44. Matrix<float> measurementNoiseCovarianceMatrix = new Matrix<float>(2, 2);
  45. measurementNoiseCovarianceMatrix.SetIdentity(new MCvScalar(measurementNoiseFactor));
  46. kalman.MeasurementNoiseCovariance = measurementNoiseCovarianceMatrix;
  47. // process noise covariance matrix
  48. Matrix<float> processNoiseCovarianceMatrix = new Matrix<float>(4, 4);
  49. processNoiseCovarianceMatrix.SetIdentity(new MCvScalar(processNoiseFactor));
  50. kalman.ProcessNoiseCovariance = processNoiseCovarianceMatrix;
  51. // error covariance post matrix (initial value)
  52. Matrix<float> errorCovariancePostMatrix = new Matrix<float>(4, 4);
  53. errorCovariancePostMatrix.SetIdentity();
  54. kalman.ErrorCovariancePost = errorCovariancePostMatrix;
  55. Initialized = false;
  56. }
  57. public void setInitialPosition(Vector2D initialPosition)
  58. {
  59. // initial state (x, y, v_x, v_y)
  60. Matrix<float> initialState = new Matrix<float>(new float[] { initialPosition.X, initialPosition.Y, 0.0f, 0.0f });
  61. kalman.CorrectedState = initialState;
  62. Initialized = true;
  63. }
  64. public Vector2D getPrediction()
  65. {
  66. Matrix<float> predicton = kalman.Predict();
  67. return new Vector2D(predicton[0, 0], predicton[1, 0]);
  68. }
  69. // updates the filter and returns the corrected position
  70. public Vector2D getCorrectedPosition(Vector2D rawPosition)
  71. {
  72. Matrix<float> rawPositionMatrix = new Matrix<float>(new float[,]
  73. { {rawPosition.X},
  74. {rawPosition.Y}});
  75. // prediction according to model
  76. Matrix<float> prediction = kalman.Predict();
  77. // corrected point
  78. Matrix<float> estimate = kalman.Correct(rawPositionMatrix);
  79. return new Vector2D(estimate[0, 0], estimate[1, 0]);
  80. }
  81. }
  82. }