PalmRect.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. //TODO
  25. origin = palmRect.Origin;
  26. PointF v0 = palmRect.Origin + palmRect.DirWidth;
  27. PointF v2 = palmRect.Origin + palmRect.DirLength;
  28. Matrix<float> tmp = new Matrix<float>(new float[,] { { v0.X - origin.X, v2.X - origin.X }, { v0.Y - origin.Y, v2.Y - origin.Y } });
  29. transformationMatrix = new Matrix<float>(2, 2);
  30. CvInvoke.cvInvert(tmp.Ptr, transformationMatrix.Ptr, Emgu.CV.CvEnum.SOLVE_METHOD.CV_LU);
  31. }
  32. public bool isWithinMargin(Vector2D position)
  33. {
  34. if (!valid)
  35. return false;
  36. Vector2D relativePosition = getRelativePosition(position);
  37. return (relativePosition.X >= -0.1 && relativePosition.X <= 1.1 && relativePosition.Y >= -0.1 && relativePosition.Y <= 1.1);
  38. }
  39. public Vector2D getRelativePosition(Vector2D absolutePosition)
  40. {
  41. if (!valid)
  42. return null;
  43. Vector2D v = absolutePosition - origin;
  44. Matrix<float> coordsMatrix = transformationMatrix.Mul(new Matrix<float>(new float[,] { { v.X }, { v.Y } }));
  45. return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]);
  46. }
  47. }
  48. }