DepthImage.cs 2.6 KB

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