PalmRect.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 bbiwarg.Utility;
  8. using Emgu.CV;
  9. using Emgu.CV.Structure;
  10. namespace bbiwarg.Detectors.Palm
  11. {
  12. class PalmRect
  13. {
  14. private Vector2D origin;
  15. private Matrix<float> transformationMatrix;
  16. bool valid = true;
  17. public PalmRect(Quad2D palmRect)
  18. {
  19. if (palmRect == null)
  20. {
  21. valid = false;
  22. return;
  23. }
  24. origin = palmRect.Origin;
  25. PointF v0 = palmRect.Origin + palmRect.DirWidth;
  26. PointF v2 = palmRect.Origin + palmRect.DirLength;
  27. Matrix<float> tmp = new Matrix<float>(new float[,] { { v0.X - origin.X, v2.X - origin.X }, { v0.Y - origin.Y, v2.Y - origin.Y } });
  28. transformationMatrix = new Matrix<float>(2, 2);
  29. CvInvoke.cvInvert(tmp.Ptr, transformationMatrix.Ptr, Emgu.CV.CvEnum.SOLVE_METHOD.CV_LU);
  30. }
  31. public bool isWithinMargin(Vector2D position)
  32. {
  33. if (!valid)
  34. return false;
  35. Vector2D relativePosition = getRelativePosition(position);
  36. return (relativePosition.X >= -0.1 && relativePosition.X <= 1.1 && relativePosition.Y >= -0.1 && relativePosition.Y <= 1.1);
  37. }
  38. public Vector2D getRelativePosition(Vector2D absolutePosition)
  39. {
  40. if (!valid)
  41. return null;
  42. Vector2D v = absolutePosition - origin;
  43. Matrix<float> coordsMatrix = transformationMatrix.Mul(new Matrix<float>(new float[,] { { v.X }, { v.Y } }));
  44. return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]);
  45. }
  46. }
  47. }