using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using bbiwarg.Recognition.FingerRecognition; using Emgu.CV.Structure; namespace bbiwarg.Utility { /// /// Stores data about and provides functions to work with convexity defects. /// public class ConvexityDefect { /// /// the point of start and end point which is nearer to the depth point /// public Vector2D OuterShort { get; private set; } /// /// the point of start and end point which is farther away from the depth point /// public Vector2D OuterLong { get; private set; } /// /// the depth point /// public Vector2D Inner { get; private set; } /// /// vector from Inner to OuterShort /// public Vector2D VectorShort { get; private set; } /// /// vector from Inner to OuterLong /// public Vector2D VectorLong { get; private set; } /// /// line segment from start to end point /// public LineSegment2D OuterLineSegment { get; private set; } /// /// distance from OuterLineSegment to Inner /// public float Depth { get; private set; } /// /// Constructs a ConvexityDefect. /// /// the emgu convexity defect which has start, end and depth point public ConvexityDefect(MCvConvexityDefect mcvConvexityDefect) { Inner = new Vector2D(mcvConvexityDefect.DepthPoint); Vector2D p1 = new Vector2D(mcvConvexityDefect.StartPoint); Vector2D p2 = new Vector2D(mcvConvexityDefect.EndPoint); if ((p1 - Inner).Length > (p2 - Inner).Length) { OuterLong = p1; OuterShort = p2; } else { OuterLong = p2; OuterShort = p1; } VectorLong = OuterLong - Inner; VectorShort = OuterShort - Inner; OuterLineSegment = new LineSegment2D(OuterLong, OuterShort); Depth = OuterLineSegment.getDistanceTo(Inner); } /// /// Tests if this defect could be the defect near the thumb. /// /// finger representing the thumb /// true iff this defect is a possible thumb defect public bool isPossibleThumbDefect(Finger thumb) { float tipDistance = thumb.TipPoint.getDistanceTo(OuterShort); float handDistance = thumb.HandPoint.getDistanceTo(Inner); float thumbShortLengthRatio = thumb.LineSegment.Length / VectorShort.Length; float shortLongLengthRatio = VectorShort.Length / VectorLong.Length; return (tipDistance <= Parameters.HandThumbDefectMaxDistanceToThumb && handDistance <= Parameters.HandThumbDefectMaxDistanceToThumb && thumbShortLengthRatio <= Parameters.HandThumbDefectMaxThumbShortLengthRatio && thumbShortLengthRatio >= Parameters.HandThumbDefectMinThumbShortLengthRatio && shortLongLengthRatio <= Parameters.HandThumbDefectMaxShortLongLengthRatio && shortLongLengthRatio >= Parameters.HandThumbDefectMinShortLongLengthRatio); } /// /// Tests if this defect is caused by a finger. /// /// the finger maybe causing the defect /// true iff this defect is caused by finger public bool isCausedByFinger(Finger finger) { return (OuterLineSegment.intersectsWith(finger.LineSegment)); } } }