Palm.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 Palm
  13. {
  14. private Vector2D origin;
  15. private Matrix<float> transformationMatrix;
  16. public Palm(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 Vector2D getRelativePosition(Vector2D absolutePosition)
  27. {
  28. Vector2D v = absolutePosition - origin;
  29. Matrix<float> coordsMatrix = transformationMatrix.Mul(new Matrix<float>(new float[,] { { v.X }, { v.Y } }));
  30. return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]);
  31. }
  32. }
  33. }