DepthImage.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Graphics;
  10. using bbiwarg.Recognition.FingerRecognition;
  11. using bbiwarg.Recognition.HandRecognition;
  12. using bbiwarg.Utility;
  13. namespace bbiwarg.Images
  14. {
  15. class DepthImage
  16. {
  17. public Image<Gray, byte> Image { get; private set; }
  18. public Image<Gray, byte> BackgroundMask { get; private set; }
  19. public int Width { get; private set; }
  20. public int Height { get; private set; }
  21. public Vector2D BottomRight { get; private set; }
  22. public Int16 MinDepth { get; private set; }
  23. public Int16 MaxDepth { get; private set; }
  24. public DepthImage(Image<Gray, Int16> image)
  25. {
  26. Width = image.Width;
  27. Height = image.Height;
  28. BottomRight = new Vector2D(Width - 1, Height - 1);
  29. image = image.SmoothMedian(Constants.DepthImageMedianSize);
  30. //threshold min&maxDepth
  31. MinDepth = findMinDepth(image);
  32. MaxDepth = (Int16)(MinDepth + Constants.DepthImageDepthRange);
  33. //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  34. Image = (image- MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  35. Image = Image.SmoothMedian(Constants.DepthImageMedianSize);
  36. }
  37. public Int16 getDepthAt(Point point)
  38. {
  39. return getDepthAt(point.X, point.Y);
  40. }
  41. public Int16 getDepthAt(int x, int y)
  42. {
  43. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  44. }
  45. public void setDepthAt(Point point, Int16 depth)
  46. {
  47. setDepthAt(point.X, point.Y, depth);
  48. }
  49. public void setDepthAt(int x, int y, Int16 depth)
  50. {
  51. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  52. }
  53. private Int16 findMinDepth(Image<Gray, Int16> image)
  54. {
  55. // min and max values
  56. double[] min, max;
  57. // min and max locations
  58. Point[] minLoc, maxLoc;
  59. image.MinMax(out min, out max, out minLoc, out maxLoc);
  60. return (Int16)min[0];
  61. }
  62. }
  63. }