DepthImage.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  70. }
  71. /// <summary>
  72. /// Sets the depth in the processed image.
  73. /// </summary>
  74. /// <param name="point">point where the depth is set</param>
  75. /// <param name="depth">new depth value</param>
  76. public void setDepthAt(Point point, Int16 depth)
  77. {
  78. setDepthAt(point.X, point.Y, depth);
  79. }
  80. /// <summary>
  81. /// Sets the depth in the processed image.
  82. /// </summary>
  83. /// <param name="x">x coordinate of position to set depth</param>
  84. /// <param name="y">y coordinate of position to set depth</param>
  85. /// <param name="depth">new depth value</param>
  86. public void setDepthAt(int x, int y, Int16 depth)
  87. {
  88. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  89. }
  90. /// <summary>
  91. /// Returns the minimum depth in an <see cref="Image"/>
  92. /// </summary>
  93. /// <param name="image">the image</param>
  94. /// <returns>the minimum depth</returns>
  95. private Int16 findMinDepth(Image<Gray, Int16> image)
  96. {
  97. // min and max values
  98. double[] min, max;
  99. // min and max locations
  100. Point[] minLoc, maxLoc;
  101. image.MinMax(out min, out max, out minLoc, out maxLoc);
  102. return (Int16)min[0];
  103. }
  104. }
  105. }