DepthImage.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. private int width;
  14. private int height;
  15. private Image<Gray, Int16> image;
  16. private Int16 minDepth;
  17. private Int16 maxDepth;
  18. public DepthImage(int width, int height, Image<Gray, Int16> image) {
  19. this.width = width;
  20. this.height = height;
  21. this.image = image;
  22. //smooth
  23. this.image = image.SmoothMedian(3);
  24. //threshold min&maxDepth
  25. minDepth = findMinDepth();
  26. maxDepth = (Int16) (minDepth + 200);
  27. thresholdDepth(minDepth, maxDepth);
  28. }
  29. public Int16 getDepthAt(Point point) {
  30. return getDepthAt(point.X, point.Y);
  31. }
  32. public Int16 getDepthAt(int x, int y) {
  33. return image.Data[y, x, 0];
  34. }
  35. public void setDepthAt(Point point, Int16 depth) {
  36. setDepthAt(point.X, point.Y, depth);
  37. }
  38. public void setDepthAt(int x, int y, Int16 depth) {
  39. image.Data[y, x, 0] = depth;
  40. }
  41. public float getRelativeDepthAt(Point point) {
  42. return getRelativeDepthAt(point.X, point.Y);
  43. }
  44. public float getRelativeDepthAt(int x, int y) {
  45. float minMaxInterval = Math.Max(maxDepth - minDepth, 1);
  46. return (getDepthAt(x, y)-minDepth) / minMaxInterval;
  47. }
  48. public Int16 getMinDepth() {
  49. return minDepth;
  50. }
  51. public Int16 getMaxDepth() {
  52. return maxDepth;
  53. }
  54. public int getWidth() {
  55. return width;
  56. }
  57. public int getHeight() {
  58. return height;
  59. }
  60. public Image<Gray, Int16> getImage() {
  61. return image;
  62. }
  63. private Int16 findMinDepth() {
  64. minDepth = Int16.MaxValue;
  65. for (int x = 0; x < width; ++x)
  66. {
  67. for (int y = 0; y < height; ++y)
  68. {
  69. Int16 depth = getDepthAt(x, y);
  70. if (depth < minDepth)
  71. minDepth = depth;
  72. }
  73. }
  74. return minDepth;
  75. }
  76. private void thresholdDepth(Int16 min, Int16 max) {
  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. }