PalmImage.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Emgu.CV;
  7. using Emgu.CV.Structure;
  8. using System.Drawing;
  9. using bbiwarg.Detectors.Fingers;
  10. using bbiwarg.Utility;
  11. namespace bbiwarg.Images
  12. {
  13. class PalmImage
  14. {
  15. private Image<Gray, Byte> image;
  16. private int width, height;
  17. bool[,] inContour;
  18. public PalmImage(EdgeImage edgeImage, FingerTracker fingerTracker)
  19. {
  20. image = edgeImage.getImage(); //.Clone();
  21. width = image.Width;
  22. height = image.Height;
  23. foreach (Finger f in fingerTracker.getFingers())
  24. {
  25. Vector<float> tip = new Vector<float>(new float[] { f.Tip.X, f.Tip.Y });
  26. Vector<float> hand = new Vector<float>(new float[] { f.Hand.X, f.Hand.Y });
  27. for (int x = 0; x < width; ++x)
  28. {
  29. for (int y = 0; y < height; ++y)
  30. {
  31. if (image.Data[y, x, 0] != 0)
  32. {
  33. Vector<float> p = new Vector<float>(new float[] { x, y });
  34. float l2 = (tip - hand) * (tip - hand);
  35. float t = (p - tip) * (hand - tip) / l2;
  36. if (t >= 0.0 && t <= 1.0)
  37. {
  38. Vector<float> tmp = hand - tip;
  39. tmp.multiply(t);
  40. Vector<float> projection = tip + tmp;
  41. if (p.distance(projection) <= 15)
  42. image.Data[y, x, 0] = 0;
  43. }
  44. }
  45. }
  46. }
  47. }
  48. findContour();
  49. }
  50. private void findContour()
  51. {
  52. //image = image.Dilate(1);
  53. Contour<Point> contour = image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,
  54. Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);
  55. Contour<Point> maxContour = contour;
  56. double maxPerimeter = 0;
  57. while (contour != null)
  58. {
  59. if (contour.Perimeter > maxPerimeter)
  60. {
  61. maxPerimeter = contour.Perimeter;
  62. maxContour = contour;
  63. }
  64. contour = contour.HNext;
  65. }
  66. inContour = new bool[width, height];
  67. if (maxContour != null)
  68. {
  69. foreach (Point p in maxContour)
  70. inContour[p.X, p.Y] = true;
  71. }
  72. }
  73. public bool belongsToPalm(int x, int y)
  74. {
  75. return inContour[x, y];
  76. }
  77. }
  78. }