DepthImage.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 Int16 MinDepth { get; private set; }
  21. public Int16 MaxDepth { get; private set; }
  22. public DepthImage(Image<Gray, Int16> image)
  23. {
  24. image = image.SmoothMedian(Parameters.DepthImageMedianSize);
  25. //threshold min&maxDepth
  26. MinDepth = findMinDepth(image);
  27. MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange);
  28. //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  29. Image = (image - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  30. Image = Image.SmoothMedian(Parameters.DepthImageMedianSize);
  31. }
  32. public Int16 getDepthAt(Point point)
  33. {
  34. return getDepthAt(point.X, point.Y);
  35. }
  36. public Int16 getDepthAt(int x, int y)
  37. {
  38. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  39. }
  40. public void setDepthAt(Point point, Int16 depth)
  41. {
  42. setDepthAt(point.X, point.Y, depth);
  43. }
  44. public void setDepthAt(int x, int y, Int16 depth)
  45. {
  46. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  47. }
  48. private Int16 findMinDepth(Image<Gray, Int16> image)
  49. {
  50. // min and max values
  51. double[] min, max;
  52. // min and max locations
  53. Point[] minLoc, maxLoc;
  54. image.MinMax(out min, out max, out minLoc, out maxLoc);
  55. return (Int16)min[0];
  56. }
  57. }
  58. }