DepthImage.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Emgu.CV;
  9. using Emgu.CV.Structure;
  10. namespace bbiwarg.Images
  11. {
  12. class DepthImage
  13. {
  14. public Image<Gray, byte> 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, Int16 minDepth)
  20. {
  21. Width = image.Width;
  22. Height = image.Height;
  23. //threshold min&maxDepth
  24. MinDepth = minDepth;
  25. MaxDepth = (Int16)(MinDepth + 200); // max = minDepth+255 (else it can't fit whole range in byte image)
  26. //smooth+threshold
  27. Image = image.SmoothMedian(3).Convert<byte>(delegate(Int16 depth) { if (depth > MaxDepth || depth < MinDepth) return (byte)(MaxDepth - MinDepth); else return (byte)(depth - MinDepth); });
  28. }
  29. public Int16 getDepthAt(Point point)
  30. {
  31. return getDepthAt(point.X, point.Y);
  32. }
  33. public Int16 getDepthAt(int x, int y)
  34. {
  35. return (Int16) (MinDepth + Image.Data[y, x, 0]);
  36. }
  37. public void setDepthAt(Point point, Int16 depth)
  38. {
  39. setDepthAt(point.X, point.Y, depth);
  40. }
  41. public void setDepthAt(int x, int y, Int16 depth)
  42. {
  43. Image.Data[y, x, 0] = (byte) (depth - MinDepth);
  44. }
  45. public float getRelativeDepthAt(Point point)
  46. {
  47. return getRelativeDepthAt(point.X, point.Y);
  48. }
  49. public float getRelativeDepthAt(int x, int y)
  50. {
  51. float minMaxInterval = Math.Max(MaxDepth - MinDepth, 1);
  52. return (getDepthAt(x, y) - MinDepth) / minMaxInterval;
  53. }
  54. }
  55. }