DepthImage.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Output;
  11. using bbiwarg.Recognition.FingerRecognition;
  12. using bbiwarg.Recognition.HandRecognition;
  13. using bbiwarg.Utility;
  14. namespace bbiwarg.Images
  15. {
  16. public 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(IntPtr rawDepthData, int width, int height, ConfidenceImage confidenceImage)
  23. {
  24. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(width, height, width * 2, rawDepthData);
  25. // filter with confidenceImage mask
  26. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  27. // smooth with median filter
  28. rawDepthImage = rawDepthImage.SmoothMedian(Parameters.DepthImageMedianSize);
  29. // threshold min&maxDepth
  30. MinDepth = findMinDepth(rawDepthImage);
  31. MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange);
  32. // threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  33. Image = (rawDepthImage - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  34. // smooth with median filter
  35. Image = Image.SmoothMedian(Parameters.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. }