TouchDetector.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 tipPointInside = tipPoint.moveWithinBound(direction.getInverse(), Parameters.TouchTipInsideFactor);
  37. Vector2D tipPointOutside = tipPoint.moveWithinBound(direction, Parameters.TouchTipOutsideFactor);
  38. foreach (Palm palm in palms)
  39. {
  40. if (palm.isInside(tipPointOutside))
  41. {
  42. Image<Gray, byte> touchMask = getTouchMask(tipPointInside);
  43. int touchPixels = touchMask.CountNonzero()[0];
  44. int numPixels = touchMask.Width * touchMask.Height;
  45. float touchValue = touchPixels / (float)numPixels;
  46. if (touchValue > Parameters.TouchMinTouchValue)
  47. {
  48. TouchEvent touchEvent = new TouchEvent(tipPointOutside, touchMask, finger, palm);
  49. TouchEvents.Add(touchEvent);
  50. finger.setTouchEvent(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. }