DepthImage.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 UInt16 MaxDepth { get; private set; }
  21. /// <summary>
  22. /// the minimum depth in the raw depth image
  23. /// </summary>
  24. public UInt16 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(Image<Gray, UInt16> rawDepthImage)
  37. {
  38. Size = new ImageSize(rawDepthImage.Width, rawDepthImage.Height);
  39. // smooth with median filter
  40. rawDepthImage = rawDepthImage.SmoothMedian(Parameters.DepthImageMedianSize);
  41. // threshold min&maxDepth
  42. MinDepth = findMinDepth(rawDepthImage);
  43. MaxDepth = (UInt16)(MinDepth + Parameters.DepthImageDepthRange);
  44. // threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  45. Image = (rawDepthImage - MinDepth).Convert<Gray, Int16>().ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  46. // smooth with median filter
  47. Image = Image.SmoothMedian(Parameters.DepthImageMedianSize);
  48. }
  49. /// <summary>
  50. /// Returns the depth in the processed image at a given point.
  51. /// </summary>
  52. /// <param name="point">the point</param>
  53. /// <returns>depth at the point</returns>
  54. public UInt16 getDepthAt(Point point)
  55. {
  56. return getDepthAt(point.X, point.Y);
  57. }
  58. /// <summary>
  59. /// Returns the depth in the processed image at a given position.
  60. /// </summary>
  61. /// <param name="x">x coordinate of the position</param>
  62. /// <param name="y">y coordinate of the position</param>
  63. /// <returns>the depth at the position</returns>
  64. public UInt16 getDepthAt(int x, int y)
  65. {
  66. try
  67. {
  68. return (UInt16)(MinDepth + Image.Data[y, x, 0]);
  69. }
  70. catch (IndexOutOfRangeException e)
  71. {
  72. return (UInt16)(MinDepth + Image.Data[0, 0, 0]);
  73. }
  74. }
  75. /// <summary>
  76. /// Returns the depth in the processed image at a given position.
  77. /// It will move the x,y more to the center of the palm!
  78. /// </summary>
  79. /// <param name="x">x coordinate of the position</param>
  80. /// <param name="y">y coordinate of the position</param>
  81. /// <returns>the depth at the position</returns>
  82. public UInt16 getDepthAtFixed(int x0, int y0, int x1, int y1)
  83. {
  84. try
  85. {
  86. int rx = (int) ((x1 - x0) * 0.15f + x0);
  87. int ry = (int) ((y1 - y0) * 0.15f + y0);
  88. return (UInt16)(MinDepth + Image.Data[ry, rx, 0]);
  89. }
  90. catch (IndexOutOfRangeException e)
  91. {
  92. return (UInt16)(MinDepth + Image.Data[0, 0, 0]);
  93. }
  94. }
  95. /// <summary>
  96. /// Sets the depth in the processed image.
  97. /// </summary>
  98. /// <param name="point">point where the depth is set</param>
  99. /// <param name="depth">new depth value</param>
  100. public void setDepthAt(Point point, UInt16 depth)
  101. {
  102. setDepthAt(point.X, point.Y, depth);
  103. }
  104. /// <summary>
  105. /// Sets the depth in the processed image.
  106. /// </summary>
  107. /// <param name="x">x coordinate of position to set depth</param>
  108. /// <param name="y">y coordinate of position to set depth</param>
  109. /// <param name="depth">new depth value</param>
  110. public void setDepthAt(int x, int y, UInt16 depth)
  111. {
  112. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  113. }
  114. /// <summary>
  115. /// Returns the minimum depth in an <see cref="Image"/>
  116. /// </summary>
  117. /// <param name="image">the image</param>
  118. /// <returns>the minimum depth</returns>
  119. private UInt16 findMinDepth(Image<Gray, UInt16> image)
  120. {
  121. // min and max values
  122. double[] min, max;
  123. // min and max locations
  124. Point[] minLoc, maxLoc;
  125. image.MinMax(out min, out max, out minLoc, out maxLoc);
  126. return (UInt16)min[0];
  127. }
  128. }
  129. }