TouchDetector.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 OutputImage outputImage;
  20. private List<Finger> fingers;
  21. public List<TouchEvent> TouchEvents { get; private set; }
  22. public TouchDetector(List<Finger> fingers, DepthImage depthImage, OutputImage outputImage)
  23. {
  24. this.depthImage = depthImage;
  25. this.outputImage = outputImage;
  26. this.fingers = fingers;
  27. this.TouchEvents = new List<TouchEvent>();
  28. foreach (Finger finger in fingers)
  29. {
  30. Vector2D tipPoint = finger.TipPoint;
  31. Vector2D direction = finger.TipDirection;
  32. Vector2D directionInv = direction.getInverse();
  33. Vector2D tipPointInside = (tipPoint + Constants.TouchEventTipInsideFactor * directionInv).moveInBound(Vector2D.Zero, depthImage.BottomRight, direction);
  34. Vector2D tipPointOutside = (tipPoint + Constants.TouchEventTipOutsideFactor * direction).moveInBound(Vector2D.Zero, depthImage.BottomRight, directionInv);
  35. outputImage.fillCircle(tipPoint.IntX, tipPoint.IntY, 3, Constants.TouchEventTipColor);
  36. float floodValue = getTouchValue(tipPointInside);
  37. if (floodValue > Constants.TouchEventMinTouchValue)
  38. {
  39. outputImage.fillCircle(tipPointOutside.IntX, tipPointOutside.IntY, 5, Constants.TouchEventDetectedColor);
  40. TouchEvent touchEvent = new TouchEvent(tipPointOutside, floodValue, finger);
  41. TouchEvents.Add(touchEvent);
  42. }
  43. }
  44. }
  45. private float getTouchValue(Point touchPoint)
  46. {
  47. int floodValue = 255;
  48. int minX = Math.Max(touchPoint.X - 2*Constants.TouchEventAreaSize / 3, 0);
  49. int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 3, depthImage.Width-1);
  50. int minY = Math.Max(touchPoint.Y - 2*Constants.TouchEventAreaSize / 3, 0);
  51. int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 3, depthImage.Height-1);
  52. Vector2D relTouch = new Vector2D(touchPoint.X - minX, touchPoint.Y - minY);
  53. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  54. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  55. MCvConnectedComp comp = new MCvConnectedComp();
  56. CvInvoke.cvFloodFill(touchArea, relTouch, new MCvScalar(floodValue), new MCvScalar(Constants.TouchEventFloodfillLowDiff), new MCvScalar(Constants.TouchEventFloodfillHighDiff), out comp, Emgu.CV.CvEnum.CONNECTIVITY.EIGHT_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, IntPtr.Zero);
  57. int matchedPixels = 0;
  58. int countedPixels = 0;
  59. for (int x = minX; x <= maxX; x++)
  60. {
  61. for (int y = minY; y <= maxY; y++)
  62. {
  63. Color color = outputImage.getColotAt(x, y);
  64. byte depth = touchArea.Data[y - minY, x - minX, 0];
  65. Color subtractColor;
  66. if(depth == floodValue)
  67. {
  68. matchedPixels++;
  69. subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
  70. }
  71. else
  72. {
  73. subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
  74. }
  75. Color newColor = Color.FromArgb(Math.Max(color.R - subtractColor.R, 0), Math.Max(color.G - subtractColor.G, 0), Math.Max(color.B - subtractColor.B, 0));
  76. outputImage.drawPixel(x, y, newColor);
  77. countedPixels++;
  78. }
  79. }
  80. float rel = (float)matchedPixels / (float)countedPixels;
  81. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor, 2);
  82. return rel;
  83. }
  84. }
  85. }