DepthImage.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. using bbiwarg.Graphics;
  10. using bbiwarg.Detectors.FingerDetection;
  11. using bbiwarg.Detectors.HandDetection;
  12. using bbiwarg.Utility;
  13. namespace bbiwarg.Images
  14. {
  15. class DepthImage
  16. {
  17. public Image<Gray, byte> Image { get; private set; }
  18. public Image<Gray, byte> BackgroundMask { get; private set; }
  19. public int Width { get; private set; }
  20. public int Height { get; private set; }
  21. public Int16 MinDepth { get; private set; }
  22. public Int16 MaxDepth { get; private set; }
  23. public DepthImage(Image<Gray, Int16> image)
  24. {
  25. Width = image.Width;
  26. Height = image.Height;
  27. image = image.SmoothMedian(Constants.DepthImageMedianSize);
  28. //threshold min&maxDepth
  29. MinDepth = findMinDepth(image);
  30. MaxDepth = (Int16)(MinDepth + Constants.DepthImageDepthRange);
  31. //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  32. Image = (image- MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  33. Image = Image.SmoothMedian(Constants.DepthImageMedianSize);
  34. }
  35. public Int16 getDepthAt(Point point)
  36. {
  37. return getDepthAt(point.X, point.Y);
  38. }
  39. public Int16 getDepthAt(int x, int y)
  40. {
  41. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  42. }
  43. public void setDepthAt(Point point, Int16 depth)
  44. {
  45. setDepthAt(point.X, point.Y, depth);
  46. }
  47. public void setDepthAt(int x, int y, Int16 depth)
  48. {
  49. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  50. }
  51. private Int16 findMinDepth(Image<Gray, Int16> image)
  52. {
  53. // min and max values
  54. double[] min, max;
  55. // min and max locations
  56. Point[] minLoc, maxLoc;
  57. image.MinMax(out min, out max, out minLoc, out maxLoc);
  58. return (Int16)min[0];
  59. }
  60. public void removeBackground(List<Hand> hands) {
  61. Image<Gray, byte> mask = Image.CopyBlank();
  62. foreach (Hand hand in hands) {
  63. mask = mask.Or(hand.Mask);
  64. }
  65. Image = Image.Or(255-mask);
  66. }
  67. }
  68. }