TouchDetector.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. foreach (Finger finger in fingers)
  33. {
  34. Vector2D tipPoint = finger.TipPoint;
  35. Vector2D direction = finger.Direction;
  36. Vector2D directionInv = direction.getInverse();
  37. Vector2D tipPointInside = (tipPoint + Parameters.TouchTipInsideFactor * directionInv).moveInBound(direction);
  38. Vector2D tipPointOutside = (tipPoint + Parameters.TouchTipOutsideFactor * direction).moveInBound(directionInv);
  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. finger.setTouchEvent(touchEvent);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. private Image<Gray, byte> getTouchMask(Vector2D touchPoint)
  58. {
  59. int minX = Math.Max(touchPoint.IntX - 2 * Parameters.TouchAreaSize / 3, 0);
  60. int maxX = Math.Min(touchPoint.IntX + Parameters.TouchAreaSize / 3, Parameters.ImageWidth - 1);
  61. int minY = Math.Max(touchPoint.IntY - 2 * Parameters.TouchAreaSize / 3, 0);
  62. int maxY = Math.Min(touchPoint.IntY + Parameters.TouchAreaSize / 3, Parameters.ImageHeight - 1);
  63. Vector2D relTouchPoint = new Vector2D(touchPoint.IntX - minX, touchPoint.IntY - minY);
  64. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  65. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  66. Image<Gray, byte> touchMask = new Image<Gray, byte>(rect.Width + 2, rect.Height + 2);
  67. MCvConnectedComp comp = new MCvConnectedComp();
  68. 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);
  69. Rectangle cropRect = new Rectangle(1, 1, rect.Width, rect.Height);
  70. return touchMask.Copy(cropRect);
  71. }
  72. }
  73. }