1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using bbiwarg.Utility;
- using Emgu.CV;
- using Emgu.CV.Structure;
- namespace bbiwarg.Detectors.Palm
- {
- class PalmRect
- {
- private Vector2D origin;
- private Matrix<float> transformationMatrix;
- bool valid = true;
- public PalmRect(Quad2D palmRect)
- {
- if (palmRect == null)
- {
- valid = false;
- return;
- }
- origin = palmRect.Origin;
- PointF v0 = palmRect.Origin + palmRect.DirWidth;
- PointF v2 = palmRect.Origin + palmRect.DirLength;
- Matrix<float> tmp = new Matrix<float>(new float[,] { { v0.X - origin.X, v2.X - origin.X }, { v0.Y - origin.Y, v2.Y - origin.Y } });
- transformationMatrix = new Matrix<float>(2, 2);
- CvInvoke.cvInvert(tmp.Ptr, transformationMatrix.Ptr, Emgu.CV.CvEnum.SOLVE_METHOD.CV_LU);
- }
- public bool isWithinMargin(Vector2D position)
- {
- if (!valid)
- return false;
-
- Vector2D relativePosition = getRelativePosition(position);
- return (relativePosition.X >= -0.1 && relativePosition.X <= 1.1 && relativePosition.Y >= -0.1 && relativePosition.Y <= 1.1);
- }
- public Vector2D getRelativePosition(Vector2D absolutePosition)
- {
- if (!valid)
- return null;
-
- Vector2D v = absolutePosition - origin;
- Matrix<float> coordsMatrix = transformationMatrix.Mul(new Matrix<float>(new float[,] { { v.X }, { v.Y } }));
- return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]);
- }
- }
- }
|