DepthImage.cs 2.6 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.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 ImageSize Size { get; private set; }
  19. public Image<Gray, byte> Image { get; private set; }
  20. public Image<Gray, byte> BackgroundMask { get; private set; }
  21. public Int16 MinDepth { get; private set; }
  22. public Int16 MaxDepth { get; private set; }
  23. public DepthImage(IntPtr rawDepthData, ImageSize size, ConfidenceImage confidenceImage)
  24. {
  25. Size = size;
  26. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(Size.Width, Size.Height, Size.Width * 2, rawDepthData);
  27. // filter with confidenceImage mask
  28. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  29. // smooth with median filter
  30. rawDepthImage = rawDepthImage.SmoothMedian(Parameters.DepthImageMedianSize);
  31. // threshold min&maxDepth
  32. MinDepth = findMinDepth(rawDepthImage);
  33. MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange);
  34. // threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  35. Image = (rawDepthImage - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  36. // smooth with median filter
  37. Image = Image.SmoothMedian(Parameters.DepthImageMedianSize);
  38. }
  39. public Int16 getDepthAt(Point point)
  40. {
  41. return getDepthAt(point.X, point.Y);
  42. }
  43. public Int16 getDepthAt(int x, int y)
  44. {
  45. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  46. }
  47. public void setDepthAt(Point point, Int16 depth)
  48. {
  49. setDepthAt(point.X, point.Y, depth);
  50. }
  51. public void setDepthAt(int x, int y, Int16 depth)
  52. {
  53. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  54. }
  55. private Int16 findMinDepth(Image<Gray, Int16> image)
  56. {
  57. // min and max values
  58. double[] min, max;
  59. // min and max locations
  60. Point[] minLoc, maxLoc;
  61. image.MinMax(out min, out max, out minLoc, out maxLoc);
  62. return (Int16)min[0];
  63. }
  64. }
  65. }