TouchEvent.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Recognition.FingerRecognition;
  7. using bbiwarg.Recognition.Tracking;
  8. using bbiwarg.Utility;
  9. using Emgu.CV;
  10. using Emgu.CV.Structure;
  11. namespace bbiwarg.Recognition.TouchRecognition
  12. {
  13. class TouchEvent : TrackableObject
  14. {
  15. public Vector2D Position { get; private set; }
  16. public Image<Gray, byte> TouchMask { get; private set; }
  17. public Finger Finger { get; private set; }
  18. public TouchEvent(Vector2D position, Image<Gray, byte> touchMask, Finger finger)
  19. {
  20. Position = position;
  21. TouchMask = touchMask;
  22. Finger = finger;
  23. }
  24. public float getTouchValue()
  25. {
  26. int touchPixels = TouchMask.CountNonzero()[0];
  27. int numPixels = TouchMask.Width * TouchMask.Height;
  28. float touchValue = touchPixels / (float)numPixels;
  29. return touchValue;
  30. }
  31. public override float getSimilarity(TrackableObject compareObject)
  32. {
  33. TouchEvent compareTouchEvent = (TouchEvent)compareObject;
  34. //finger similarity
  35. float fingerSimilarity = (Finger.TrackID == compareTouchEvent.Finger.TrackID) ? 1 : 0;
  36. //position similarity
  37. float distance = Position.getDistanceTo(compareTouchEvent.Position);
  38. float distanceSimilarity = Math.Max(0, 1 - distance / Constants.ImageDiagonalLength);
  39. float similarity = fingerSimilarity * distanceSimilarity;
  40. return similarity;
  41. }
  42. }
  43. }