TouchDetector.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. public List<TouchEvent> TouchEvents { get; private set; }
  21. public TouchDetector(DepthImage depthImage, List<Finger> fingers, Quadrangle palmQuad)
  22. {
  23. this.depthImage = depthImage;
  24. this.fingers = fingers;
  25. this.TouchEvents = new List<TouchEvent>();
  26. foreach (Finger finger in fingers)
  27. {
  28. Vector2D tipPoint = finger.TipPoint;
  29. Vector2D direction = finger.TipDirection;
  30. Vector2D directionInv = direction.getInverse();
  31. Vector2D tipPointInside = (tipPoint + Constants.TouchEventTipInsideFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  32. Vector2D tipPointOutside = (tipPoint + Constants.TouchEventTipOutsideFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  33. Image<Gray, byte> touchMask = getTouchMask(tipPointInside);
  34. int touchPixels = touchMask.CountNonzero()[0];
  35. int numPixels = touchMask.Width * touchMask.Height;
  36. float touchValue = touchPixels / (float)numPixels;
  37. if (touchValue > Constants.TouchEventMinTouchValue)
  38. {
  39. if (palmQuad != null && palmQuad.isInside(tipPointOutside))
  40. {
  41. TouchEvent touchEvent = new PalmTouchEvent(tipPointOutside, touchMask, finger, palmQuad);
  42. TouchEvents.Add(touchEvent);
  43. finger.setTouchEvent(touchEvent);
  44. }
  45. }
  46. }
  47. }
  48. private Image<Gray, byte> getTouchMask(Vector2D touchPoint)
  49. {
  50. int minX = Math.Max(touchPoint.IntX - 2 * Constants.TouchEventAreaSize / 3, 0);
  51. int maxX = Math.Min(touchPoint.IntX + Constants.TouchEventAreaSize / 3, depthImage.Width - 1);
  52. int minY = Math.Max(touchPoint.IntY - 2 * Constants.TouchEventAreaSize / 3, 0);
  53. int maxY = Math.Min(touchPoint.IntY + Constants.TouchEventAreaSize / 3, depthImage.Height - 1);
  54. Vector2D relTouchPoint = new Vector2D(touchPoint.IntX - minX, touchPoint.IntY - minY);
  55. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  56. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  57. Image<Gray, byte> touchMask = new Image<Gray, byte>(rect.Width + 2, rect.Height + 2);
  58. MCvConnectedComp comp = new MCvConnectedComp();
  59. CvInvoke.cvFloodFill(touchArea, relTouchPoint, new MCvScalar(255), new MCvScalar(Constants.TouchEventFloodfillLowDiff), new MCvScalar(Constants.TouchEventFloodfillHighDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, touchMask);
  60. Rectangle cropRect = new Rectangle(1, 1, rect.Width, rect.Height);
  61. return touchMask.Copy(cropRect);
  62. }
  63. }
  64. }