DepthImage.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /// <summary>
  17. /// DepthImage stores a processed version of the depth image read by the camera as an <see cref="Image"/>
  18. /// </summary>
  19. public class DepthImage
  20. {
  21. /// <summary>
  22. /// image size of the deph image
  23. /// </summary>
  24. public ImageSize Size { get; private set; }
  25. /// <summary>
  26. /// the processed depth image
  27. /// </summary>
  28. public Image<Gray, byte> Image { get; private set; }
  29. /// <summary>
  30. /// the minimum depth in the raw depth image
  31. /// </summary>
  32. public Int16 MinDepth { get; private set; }
  33. /// <summary>
  34. /// the maximum depth which is considered important
  35. /// </summary>
  36. public Int16 MaxDepth { get; private set; }
  37. /// <summary>
  38. /// Construct a DepthImage from the raw depth data, the image size and a confidenceImage.
  39. /// Filters the image using confidenceImage and also thresholds and smoothes it.
  40. /// </summary>
  41. /// <param name="rawDepthData">the raw depth data</param>
  42. /// <param name="size">the image size</param>
  43. /// <param name="confidenceImage">the confidence image</param>
  44. public DepthImage(IntPtr rawDepthData, ImageSize size, ConfidenceImage confidenceImage)
  45. {
  46. Size = size;
  47. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(Size.Width, Size.Height, Size.Width * 2, rawDepthData);
  48. // filter with confidenceImage mask
  49. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  50. // smooth with median filter
  51. rawDepthImage = rawDepthImage.SmoothMedian(Parameters.DepthImageMedianSize);
  52. // threshold min&maxDepth
  53. MinDepth = findMinDepth(rawDepthImage);
  54. MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange);
  55. // threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  56. Image = (rawDepthImage - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  57. // smooth with median filter
  58. Image = Image.SmoothMedian(Parameters.DepthImageMedianSize);
  59. }
  60. /// <summary>
  61. /// Returns the depth in the processed image at a given point.
  62. /// </summary>
  63. /// <param name="point">the point</param>
  64. /// <returns>depth at the point</returns>
  65. public Int16 getDepthAt(Point point)
  66. {
  67. return getDepthAt(point.X, point.Y);
  68. }
  69. /// <summary>
  70. /// Returns the depth in the processed image at a given position.
  71. /// </summary>
  72. /// <param name="x">x coordinate of the position</param>
  73. /// <param name="y">y coordinate of the position</param>
  74. /// <returns>the depth at the position</returns>
  75. public Int16 getDepthAt(int x, int y)
  76. {
  77. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  78. }
  79. /// <summary>
  80. /// Sets the depth in the processed image.
  81. /// </summary>
  82. /// <param name="point">point where the depth is set</param>
  83. /// <param name="depth">new depth value</param>
  84. public void setDepthAt(Point point, Int16 depth)
  85. {
  86. setDepthAt(point.X, point.Y, depth);
  87. }
  88. /// <summary>
  89. /// Sets the depth in the processed image.
  90. /// </summary>
  91. /// <param name="x">x coordinate of position to set depth</param>
  92. /// <param name="y">y coordinate of position to set depth</param>
  93. /// <param name="depth">new depth value</param>
  94. public void setDepthAt(int x, int y, Int16 depth)
  95. {
  96. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  97. }
  98. /// <summary>
  99. /// Returns the minimum depth in an <see cref="Image"/>
  100. /// </summary>
  101. /// <param name="image">the image</param>
  102. /// <returns>the minimum depth</returns>
  103. private Int16 findMinDepth(Image<Gray, Int16> image)
  104. {
  105. // min and max values
  106. double[] min, max;
  107. // min and max locations
  108. Point[] minLoc, maxLoc;
  109. image.MinMax(out min, out max, out minLoc, out maxLoc);
  110. return (Int16)min[0];
  111. }
  112. }
  113. }