TouchDetector.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, Quadrangle palmQuad, 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. Image<Gray, byte> touchMask = getTouchMask(tipPointInside);
  36. int touchPixels = touchMask.CountNonzero()[0];
  37. int numPixels = touchMask.Width * touchMask.Height;
  38. float touchValue = touchPixels / (float)numPixels;
  39. if (touchValue > Constants.TouchEventMinTouchValue)
  40. {
  41. outputImage.fillCircle(tipPointOutside.IntX, tipPointOutside.IntY, 5, Constants.TouchEventDetectedColor);
  42. TouchEvent touchEvent;
  43. if (palmQuad != null && palmQuad.isInside(tipPointOutside))
  44. touchEvent = new PalmTouchEvent(tipPointOutside, touchMask, finger, palmQuad);
  45. else
  46. touchEvent = new TouchEvent(tipPointOutside, touchMask, finger);
  47. TouchEvents.Add(touchEvent);
  48. finger.setTouchEvent(touchEvent);
  49. }
  50. outputImage.fillCircle(tipPoint.IntX, tipPoint.IntY, 3, Constants.TouchEventTipColor);
  51. outputImage.drawText(tipPoint.IntX, tipPoint.IntY,String.Format("{0:0.00}", touchValue), Color.Red);
  52. }
  53. }
  54. private Image<Gray, byte> getTouchMask(Vector2D touchPoint)
  55. {
  56. int minX = Math.Max(touchPoint.IntX - 2 * Constants.TouchEventAreaSize / 3, 0);
  57. int maxX = Math.Min(touchPoint.IntX + Constants.TouchEventAreaSize / 3, depthImage.Width - 1);
  58. int minY = Math.Max(touchPoint.IntY - 2 * Constants.TouchEventAreaSize / 3, 0);
  59. int maxY = Math.Min(touchPoint.IntY + Constants.TouchEventAreaSize / 3, depthImage.Height - 1);
  60. Vector2D relTouchPoint = new Vector2D(touchPoint.IntX - minX, touchPoint.IntY - minY);
  61. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  62. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  63. Image<Gray, byte> touchMask = new Image<Gray, byte>(rect.Width + 2, rect.Height + 2);
  64. MCvConnectedComp comp = new MCvConnectedComp();
  65. 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);
  66. Rectangle cropRect = new Rectangle(1, 1, rect.Width, rect.Height);
  67. return touchMask.Copy(cropRect);
  68. }
  69. /*
  70. private float getTouchValue(Point touchPoint)
  71. {
  72. int floodValue = 255;
  73. int minX = Math.Max(touchPoint.X - 2*Constants.TouchEventAreaSize / 3, 0);
  74. int maxX = Math.Min(touchPoint.X + Constants.TouchEventAreaSize / 3, depthImage.Width-1);
  75. int minY = Math.Max(touchPoint.Y - 2*Constants.TouchEventAreaSize / 3, 0);
  76. int maxY = Math.Min(touchPoint.Y + Constants.TouchEventAreaSize / 3, depthImage.Height-1);
  77. Vector2D relTouch = new Vector2D(touchPoint.X - minX, touchPoint.Y - minY);
  78. Rectangle rect = new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
  79. Image<Gray, byte> touchArea = depthImage.Image.Copy(rect);
  80. MCvConnectedComp comp = new MCvConnectedComp();
  81. 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);
  82. int matchedPixels = 0;
  83. int countedPixels = 0;
  84. for (int x = minX; x <= maxX; x++)
  85. {
  86. for (int y = minY; y <= maxY; y++)
  87. {
  88. Color color = outputImage.getColotAt(x, y);
  89. byte depth = touchArea.Data[y - minY, x - minX, 0];
  90. Color subtractColor;
  91. if(depth == floodValue)
  92. {
  93. matchedPixels++;
  94. subtractColor = Constants.TouchEventAreaMatchedSubtractColor;
  95. }
  96. else
  97. {
  98. subtractColor = Constants.TouchEventAreaNonMatchedSubtractColor;
  99. }
  100. 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));
  101. outputImage.drawPixel(x, y, newColor);
  102. countedPixels++;
  103. }
  104. }
  105. float rel = (float)matchedPixels / (float)countedPixels;
  106. outputImage.drawLineSegment(new Utility.LineSegment2D(new Vector2D(minX, maxY), new Vector2D(minX + rel * Constants.TouchEventAreaSize, maxY)), Constants.TouchEventStatusBarColor, 2);
  107. return rel;
  108. }*/
  109. }
  110. }