DepthImage.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.Utility;
  12. namespace bbiwarg.Images
  13. {
  14. class DepthImage
  15. {
  16. private OutputImage outputImage;
  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, OutputImage outputImage)
  24. {
  25. this.outputImage = outputImage;
  26. Width = image.Width;
  27. Height = image.Height;
  28. image = image.SmoothMedian(Constants.DepthImageMedianSize);
  29. //threshold min&maxDepth
  30. MinDepth = findMinDepth(image);
  31. MaxDepth = (Int16)(MinDepth + Constants.DepthImageDepthRange);
  32. //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  33. Image = (image- MinDepth).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  34. Image = Image.SmoothMedian(Constants.DepthImageMedianSize);
  35. }
  36. public Int16 getDepthAt(Point point)
  37. {
  38. return getDepthAt(point.X, point.Y);
  39. }
  40. public Int16 getDepthAt(int x, int y)
  41. {
  42. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  43. }
  44. public void setDepthAt(Point point, Int16 depth)
  45. {
  46. setDepthAt(point.X, point.Y, depth);
  47. }
  48. public void setDepthAt(int x, int y, Int16 depth)
  49. {
  50. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  51. }
  52. private Int16 findMinDepth(Image<Gray, Int16> image)
  53. {
  54. // min and max values
  55. double[] min, max;
  56. // min and max locations
  57. Point[] minLoc, maxLoc;
  58. image.MinMax(out min, out max, out minLoc, out maxLoc);
  59. return (Int16)min[0];
  60. }
  61. public void removeBackground(List<Finger> fingers) {
  62. BackgroundMask = Image.Copy();
  63. MCvConnectedComp comp = new MCvConnectedComp();
  64. foreach (Finger finger in fingers) {
  65. Vector2D mid = finger.SliceTrail.Slices[finger.SliceTrail.NumSlices / 2].Mid;
  66. outputImage.fillCircle(mid.IntX, mid.IntY, 3, Color.Blue);
  67. if (BackgroundMask.Data[mid.IntY, mid.IntX,0] != 0)
  68. CvInvoke.cvFloodFill(BackgroundMask, mid, new MCvScalar(0), new MCvScalar(3), new MCvScalar(3), out comp, Emgu.CV.CvEnum.CONNECTIVITY.FOUR_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, IntPtr.Zero);
  69. }
  70. BackgroundMask = BackgroundMask.Erode(3).Dilate(3).ThresholdBinary(new Gray(0), new Gray(255));
  71. Image = Image.Or(BackgroundMask);
  72. }
  73. }
  74. }