DepthImage.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Emgu.CV;
  7. using Emgu.CV.Structure;
  8. namespace bbiwarg.Images
  9. {
  10. class DepthImage
  11. {
  12. private int width;
  13. private int height;
  14. private Image<Gray, Int16> image;
  15. private Int16 minDepth;
  16. private Int16 maxDepth;
  17. public DepthImage(int width, int height, Image<Gray, Int16> image) {
  18. this.width = width;
  19. this.height = height;
  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(int x, int y) {
  29. return image.Data[y, x, 0];
  30. }
  31. public void setDepthAt(int x, int y, Int16 depth) {
  32. image.Data[y, x, 0] = depth;
  33. }
  34. public float getRelativeDepth(int x, int y) {
  35. float minMaxInterval = Math.Max(maxDepth - minDepth, 1);
  36. return (getDepthAt(x, y)-minDepth) / minMaxInterval;
  37. }
  38. public Int16 getMinDepth() {
  39. return minDepth;
  40. }
  41. public Int16 getMaxDepth() {
  42. return maxDepth;
  43. }
  44. public int getWidth() {
  45. return width;
  46. }
  47. public int getHeight() {
  48. return height;
  49. }
  50. public Image<Gray, Int16> getImage() {
  51. return image;
  52. }
  53. private Int16 findMinDepth() {
  54. minDepth = Int16.MaxValue;
  55. for (int x = 0; x < width; ++x)
  56. {
  57. for (int y = 0; y < height; ++y)
  58. {
  59. Int16 depth = getDepthAt(x, y);
  60. if (depth < minDepth)
  61. minDepth = depth;
  62. }
  63. }
  64. return minDepth;
  65. }
  66. private void thresholdDepth(Int16 min, Int16 max) {
  67. for (int x = 0; x < width; ++x)
  68. {
  69. for (int y = 0; y < height; ++y)
  70. {
  71. Int16 depth = getDepthAt(x, y);
  72. if (depth > max || depth < min)
  73. image.Data[y, x, 0] = max;
  74. }
  75. }
  76. }
  77. }
  78. }