PalmRect.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. public PalmRect(MCvBox2D palmRect)
  17. {
  18. PointF[] vertices = palmRect.GetVertices();
  19. origin = new Vector2D(vertices[1]);
  20. PointF v0 = vertices[0];
  21. PointF v2 = vertices[2];
  22. Matrix<float> tmp = new Matrix<float>(new float[,] { { v0.X - origin.X, v2.X - origin.X }, { v0.Y - origin.Y, v2.Y - origin.Y } });
  23. transformationMatrix = new Matrix<float>(2, 2);
  24. CvInvoke.cvInvert(tmp.Ptr, transformationMatrix.Ptr, Emgu.CV.CvEnum.SOLVE_METHOD.CV_LU);
  25. }
  26. public bool isWithinMargin(Vector2D position)
  27. {
  28. Vector2D relativePosition = getRelativePosition(position);
  29. return (relativePosition.X >= -0.1 && relativePosition.X <= 1.1 && relativePosition.Y >= -0.1 && relativePosition.Y <= 1.1);
  30. }
  31. public Vector2D getRelativePosition(Vector2D absolutePosition)
  32. {
  33. Vector2D v = absolutePosition - origin;
  34. Matrix<float> coordsMatrix = transformationMatrix.Mul(new Matrix<float>(new float[,] { { v.X }, { v.Y } }));
  35. return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]);
  36. }
  37. }
  38. }