DepthImage.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Emgu.CV;
  8. using Emgu.CV.Structure;
  9. using System.Diagnostics;
  10. namespace bbiwarg.Images
  11. {
  12. class DepthImage
  13. {
  14. public Image<Gray, Int16> Image { get; private set; }
  15. public int Width { get; private set; }
  16. public int Height { get; private set; }
  17. public Int16 MinDepth { get; private set; }
  18. public Int16 MaxDepth { get; private set; }
  19. public DepthImage(Image<Gray, Int16> image)
  20. {
  21. this.Image = image;
  22. Width = image.Width;
  23. Height = image.Height;
  24. //smooth
  25. this.Image = image.SmoothMedian(3);
  26. //threshold min&maxDepth
  27. MinDepth = (Int16)findMinDepth();
  28. MaxDepth = (Int16)(MinDepth + 200);
  29. thresholdDepth(MaxDepth);
  30. }
  31. public Int16 getDepthAt(Point point)
  32. {
  33. return getDepthAt(point.X, point.Y);
  34. }
  35. public Int16 getDepthAt(int x, int y)
  36. {
  37. return Image.Data[y, x, 0];
  38. }
  39. public void setDepthAt(Point point, Int16 depth)
  40. {
  41. setDepthAt(point.X, point.Y, depth);
  42. }
  43. public void setDepthAt(int x, int y, Int16 depth)
  44. {
  45. Image.Data[y, x, 0] = depth;
  46. }
  47. public float getRelativeDepthAt(Point point)
  48. {
  49. return getRelativeDepthAt(point.X, point.Y);
  50. }
  51. public float getRelativeDepthAt(int x, int y)
  52. {
  53. float minMaxInterval = Math.Max(MaxDepth - MinDepth, 1);
  54. return (getDepthAt(x, y) - MinDepth) / minMaxInterval;
  55. }
  56. public Int16 getMinDepth()
  57. {
  58. return MinDepth;
  59. }
  60. public Int16 getMaxDepth()
  61. {
  62. return MaxDepth;
  63. }
  64. private Int16 findMinDepth()
  65. {
  66. // min and max values
  67. double[] min, max;
  68. // min and max locations
  69. Point[] minLoc, maxLoc;
  70. Image.MinMax(out min, out max, out minLoc, out maxLoc);
  71. return (Int16) min[0];
  72. }
  73. private void thresholdDepth(Int16 max)
  74. {
  75. // newDepth = (depth > max) ? max : depth;
  76. Image = Image.ThresholdTrunc(new Gray(max));
  77. }
  78. }
  79. }