DepthImage.cs 2.4 KB

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