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 transformationMatrix; bool valid = true; public PalmRect(Quad2D palmRect) { if (palmRect == null) { valid = false; return; } //TODO origin = palmRect.Origin; PointF v0 = palmRect.Origin + palmRect.DirWidth; PointF v2 = palmRect.Origin + palmRect.DirLength; Matrix tmp = new Matrix(new float[,] { { v0.X - origin.X, v2.X - origin.X }, { v0.Y - origin.Y, v2.Y - origin.Y } }); transformationMatrix = new Matrix(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 coordsMatrix = transformationMatrix.Mul(new Matrix(new float[,] { { v.X }, { v.Y } })); return new Vector2D(coordsMatrix.Data[0, 0], coordsMatrix.Data[1, 0]); } } }