DepthImage.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using BBIWARG.Utility;
  2. using Emgu.CV;
  3. using Emgu.CV.Structure;
  4. using System;
  5. using System.Drawing;
  6. namespace BBIWARG.Images
  7. {
  8. /// <summary>
  9. /// DepthImage stores a processed version of the depth image read by the camera as an <see cref="Image"/>
  10. /// </summary>
  11. public class DepthImage
  12. {
  13. /// <summary>
  14. /// the processed depth image
  15. /// </summary>
  16. public Image<Gray, byte> Image { get; private set; }
  17. /// <summary>
  18. /// the maximum depth which is considered important
  19. /// </summary>
  20. public Int16 MaxDepth { get; private set; }
  21. /// <summary>
  22. /// the minimum depth in the raw depth image
  23. /// </summary>
  24. public Int16 MinDepth { get; private set; }
  25. /// <summary>
  26. /// image size of the depth image
  27. /// </summary>
  28. public ImageSize Size { get; private set; }
  29. /// <summary>
  30. /// Construct a DepthImage from the raw depth data, the image size and a confidenceImage.
  31. /// Filters the image using confidenceImage and also applies thresholding and smoothing.
  32. /// </summary>
  33. /// <param name="rawDepthData">the raw depth data</param>
  34. /// <param name="size">the image size</param>
  35. /// <param name="confidenceImage">the confidence image</param>
  36. public DepthImage(IntPtr rawDepthData, ImageSize size, ConfidenceImage confidenceImage)
  37. {
  38. Size = size;
  39. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(Size.Width, Size.Height, Size.Width * 2, rawDepthData);
  40. // filter with confidenceImage mask
  41. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  42. // smooth with median filter
  43. rawDepthImage = rawDepthImage.SmoothMedian(Parameters.DepthImageMedianSize);
  44. // threshold min&maxDepth
  45. MinDepth = findMinDepth(rawDepthImage);
  46. MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange);
  47. // threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  48. Image = (rawDepthImage - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  49. // smooth with median filter
  50. Image = Image.SmoothMedian(Parameters.DepthImageMedianSize);
  51. }
  52. /// <summary>
  53. /// Returns the depth in the processed image at a given point.
  54. /// </summary>
  55. /// <param name="point">the point</param>
  56. /// <returns>depth at the point</returns>
  57. public Int16 getDepthAt(Point point)
  58. {
  59. return getDepthAt(point.X, point.Y);
  60. }
  61. /// <summary>
  62. /// Returns the depth in the processed image at a given position.
  63. /// </summary>
  64. /// <param name="x">x coordinate of the position</param>
  65. /// <param name="y">y coordinate of the position</param>
  66. /// <returns>the depth at the position</returns>
  67. public Int16 getDepthAt(int x, int y)
  68. {
  69. try
  70. {
  71. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  72. }
  73. catch (IndexOutOfRangeException e)
  74. {
  75. return (Int16)(MinDepth + Image.Data[0, 0, 0]);
  76. }
  77. }
  78. /// <summary>
  79. /// Returns the depth in the processed image at a given position.
  80. /// It will move the x,y more to the center of the palm!
  81. /// </summary>
  82. /// <param name="x">x coordinate of the position</param>
  83. /// <param name="y">y coordinate of the position</param>
  84. /// <returns>the depth at the position</returns>
  85. public Int16 getDepthAtFixed(int x0, int y0, int x1, int y1)
  86. {
  87. try
  88. {
  89. int rx = (int) ((x1 - x0) * 0.15f + x0);
  90. int ry = (int) ((y1 - y0) * 0.15f + y0);
  91. return (Int16)(MinDepth + Image.Data[ry, rx, 0]);
  92. }
  93. catch (IndexOutOfRangeException e)
  94. {
  95. return (Int16)(MinDepth + Image.Data[0, 0, 0]);
  96. }
  97. }
  98. /// <summary>
  99. /// Sets the depth in the processed image.
  100. /// </summary>
  101. /// <param name="point">point where the depth is set</param>
  102. /// <param name="depth">new depth value</param>
  103. public void setDepthAt(Point point, Int16 depth)
  104. {
  105. setDepthAt(point.X, point.Y, depth);
  106. }
  107. /// <summary>
  108. /// Sets the depth in the processed image.
  109. /// </summary>
  110. /// <param name="x">x coordinate of position to set depth</param>
  111. /// <param name="y">y coordinate of position to set depth</param>
  112. /// <param name="depth">new depth value</param>
  113. public void setDepthAt(int x, int y, Int16 depth)
  114. {
  115. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  116. }
  117. /// <summary>
  118. /// Returns the minimum depth in an <see cref="Image"/>
  119. /// </summary>
  120. /// <param name="image">the image</param>
  121. /// <returns>the minimum depth</returns>
  122. private Int16 findMinDepth(Image<Gray, Int16> image)
  123. {
  124. // min and max values
  125. double[] min, max;
  126. // min and max locations
  127. Point[] minLoc, maxLoc;
  128. image.MinMax(out min, out max, out minLoc, out maxLoc);
  129. return (Int16)min[0];
  130. }
  131. }
  132. }