DepthImage.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 System.Runtime.InteropServices;
  8. using Emgu.CV;
  9. using Emgu.CV.Structure;
  10. using bbiwarg.Graphics;
  11. using bbiwarg.Recognition.FingerRecognition;
  12. using bbiwarg.Recognition.HandRecognition;
  13. using bbiwarg.Utility;
  14. namespace bbiwarg.Images
  15. {
  16. class DepthImage
  17. {
  18. public Image<Gray, byte> Image { get; private set; }
  19. public Image<Gray, byte> BackgroundMask { get; private set; }
  20. public Int16 MinDepth { get; private set; }
  21. public Int16 MaxDepth { get; private set; }
  22. public DepthImage(Image<Gray, Int16> image)
  23. {
  24. //image = image.SmoothBlur(3,3,true);
  25. image = image.SmoothMedian(Parameters.DepthImageMedianSize);
  26. //threshold min&maxDepth
  27. MinDepth = findMinDepth(image);
  28. MaxDepth = (Int16)(MinDepth + Parameters.DepthImageDepthRange);
  29. //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  30. Image = (image - MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  31. //Image = Image.SmoothBlur(3,3, true);
  32. Image = Image.SmoothMedian(Parameters.DepthImageMedianSize);
  33. }
  34. public Int16 getDepthAt(Point point)
  35. {
  36. return getDepthAt(point.X, point.Y);
  37. }
  38. public Int16 getDepthAt(int x, int y)
  39. {
  40. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  41. }
  42. public void setDepthAt(Point point, Int16 depth)
  43. {
  44. setDepthAt(point.X, point.Y, depth);
  45. }
  46. public void setDepthAt(int x, int y, Int16 depth)
  47. {
  48. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  49. }
  50. private Int16 findMinDepth(Image<Gray, Int16> image)
  51. {
  52. // min and max values
  53. double[] min, max;
  54. // min and max locations
  55. Point[] minLoc, maxLoc;
  56. image.MinMax(out min, out max, out minLoc, out maxLoc);
  57. return (Int16)min[0];
  58. }
  59. }
  60. }