DepthImage.cs 4.6 KB

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