DepthImage.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 float getRelativeDepth(int x, int y) {
  32. float minMaxInterval = Math.Max(maxDepth - minDepth, 1);
  33. return (getDepthAt(x, y)-minDepth) / minMaxInterval;
  34. }
  35. public Int16 getMinDepth() {
  36. return minDepth;
  37. }
  38. public Int16 getMaxDepth() {
  39. return maxDepth;
  40. }
  41. public int getWidth() {
  42. return width;
  43. }
  44. public int getHeight() {
  45. return height;
  46. }
  47. public Image<Gray, Int16> getImage() {
  48. return image;
  49. }
  50. private Int16 findMinDepth() {
  51. minDepth = Int16.MaxValue;
  52. for (int x = 0; x < width; ++x)
  53. {
  54. for (int y = 0; y < height; ++y)
  55. {
  56. Int16 depth = getDepthAt(x, y);
  57. if (depth < minDepth)
  58. minDepth = depth;
  59. }
  60. }
  61. return minDepth;
  62. }
  63. private void thresholdDepth(Int16 min, Int16 max) {
  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 > max || depth < min)
  70. image.Data[y, x, 0] = max;
  71. }
  72. }
  73. }
  74. }
  75. }