TouchDetector.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Emgu.CV;
  8. using Emgu.CV.Structure;
  9. using bbiwarg.Images;
  10. using bbiwarg.Recognition.FingerRecognition;
  11. using bbiwarg.Recognition.PalmRecognition;
  12. using bbiwarg.Utility;
  13. using bbiwarg.Graphics;
  14. namespace bbiwarg.Recognition.TouchRecognition
  15. {
  16. class TouchDetector
  17. {
  18. private DepthImage depthImage;
  19. private List<Finger> fingers;
  20. private List<Palm> palms;
  21. public List<TouchEvent> TouchEvents { get; private set; }
  22. public TouchDetector(DepthImage depthImage, List<Finger> fingers, List<Palm> palms)
  23. {
  24. this.depthImage = depthImage;
  25. this.fingers = fingers;
  26. this.palms = palms;
  27. TouchEvents = new List<TouchEvent>();
  28. if (palms.Count > 0)
  29. detectTouch();
  30. }
  31. private void detectTouch()
  32. {
  33. foreach (Finger finger in fingers)
  34. {
  35. Vector2D tipPoint = finger.TipPoint;
  36. Vector2D direction = finger.Direction;
  37. Vector2D tipPointInside = tipPoint.moveWithinBound(direction.getInverse(), Parameters.TouchTipInsideFactor);
  38. Vector2D tipPointOutside = tipPoint.moveWithinBound(direction, Parameters.TouchTipOutsideFactor);
  39. foreach (Palm palm in palms)
  40. {
  41. if (palm.isInside(tipPointOutside))
  42. {
  43. Image<Gray, byte> touchMask = getTouchMask(tipPointInside);
  44. int touchPixels = touchMask.CountNonzero()[0];
  45. int numPixels = touchMask.Width * touchMask.Height;
  46. float touchValue = touchPixels / (float)numPixels;
  47. if (touchValue > Parameters.TouchMinTouchValue)
  48. {
  49. TouchEvent touchEvent = new TouchEvent(tipPointOutside, touchMask, finger, palm);
  50. TouchEvents.Add(touchEvent);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. private Image<Gray, byte> getTouchMask(Vector2D touchPoint)
  57. {
  58. int minX = Math.Max(touchPoint.IntX - 2 * Parameters.TouchAreaSize / 3, 0);
  59. int maxX = Math.Min(touchPoint.IntX + Parameters.TouchAreaSize / 3, Parameters.ImageWidth - 1);
  60. int minY = Math.Max(touchPoint.IntY - 2 * Parameters.TouchAreaSize / 3, 0);
  61. int maxY = Math.Min(touchPoint.IntY + Parameters.TouchAreaSize / 3, Parameters.ImageHeight - 1);
  62. Vector2D relTouchPoint = new Vector2D(touchPoint.IntX - minX, touchPoint.IntY - minY);
  63. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  64. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  65. Image<Gray, byte> touchMask = new Image<Gray, byte>(rect.Width + 2, rect.Height + 2);
  66. MCvConnectedComp comp = new MCvConnectedComp();
  67. CvInvoke.cvFloodFill(touchArea, relTouchPoint, new MCvScalar(255), new MCvScalar(Parameters.TouchFloodfillDownDiff), new MCvScalar(Parameters.TouchFloodfillUpDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, touchMask);
  68. Rectangle cropRect = new Rectangle(1, 1, rect.Width, rect.Height);
  69. return touchMask.Copy(cropRect);
  70. }
  71. }
  72. }