DepthImage.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.Fingers;
  11. using bbiwarg.Utility;
  12. namespace bbiwarg.Images
  13. {
  14. class DepthImage
  15. {
  16. public Image<Gray, byte> Image { get; private set; }
  17. public Image<Gray, byte> BackgroundMask { get; private set; }
  18. public int Width { get; private set; }
  19. public int Height { 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. Width = image.Width;
  25. Height = image.Height;
  26. image = image.SmoothMedian(3);
  27. //threshold min&maxDepth
  28. MinDepth = findMinDepth(image);
  29. MaxDepth = (Int16)(MinDepth + 200); // max = minDepth+255 (else it can't fit whole range in byte image)
  30. //smooth+threshold (dst = (src > (MaxDepth - MinDepth)) ? MaxDepth - MinDepth : src)
  31. Image = image.Sub(new Gray(MinDepth)).ThresholdTrunc(new Gray(MaxDepth - MinDepth)).Convert<Gray, byte>();
  32. }
  33. public Int16 getDepthAt(Point point)
  34. {
  35. return getDepthAt(point.X, point.Y);
  36. }
  37. public Int16 getDepthAt(int x, int y)
  38. {
  39. return (Int16)(MinDepth + Image.Data[y, x, 0]);
  40. }
  41. public void setDepthAt(Point point, Int16 depth)
  42. {
  43. setDepthAt(point.X, point.Y, depth);
  44. }
  45. public void setDepthAt(int x, int y, Int16 depth)
  46. {
  47. Image.Data[y, x, 0] = (byte)(depth - MinDepth);
  48. }
  49. public float getRelativeDepthAt(Point point)
  50. {
  51. return getRelativeDepthAt(point.X, point.Y);
  52. }
  53. public float getRelativeDepthAt(int x, int y)
  54. {
  55. float minMaxInterval = Math.Max(MaxDepth - MinDepth, 1);
  56. return (getDepthAt(x, y) - MinDepth) / minMaxInterval;
  57. }
  58. private Int16 findMinDepth(Image<Gray, Int16> image)
  59. {
  60. // min and max values
  61. double[] min, max;
  62. // min and max locations
  63. Point[] minLoc, maxLoc;
  64. image.MinMax(out min, out max, out minLoc, out maxLoc);
  65. return (Int16)min[0];
  66. }
  67. public void removeBackground(List<Finger> fingers) {
  68. BackgroundMask = Image.Copy();
  69. MCvConnectedComp comp = new MCvConnectedComp();
  70. foreach (Finger finger in fingers) {
  71. Vector2D mid = (finger.Tip + finger.Hand) / 2;
  72. if(BackgroundMask.Data[mid.IntY, mid.IntX,0] != 0)
  73. CvInvoke.cvFloodFill(BackgroundMask, mid, new MCvScalar(0), new MCvScalar(5), new MCvScalar(5), out comp, Emgu.CV.CvEnum.CONNECTIVITY.FOUR_CONNECTED, Emgu.CV.CvEnum.FLOODFILL_FLAG.DEFAULT, IntPtr.Zero);
  74. }
  75. BackgroundMask = BackgroundMask.ThresholdBinary(new Gray(0), new Gray(255));
  76. Image = Image.Or(BackgroundMask);
  77. }
  78. }
  79. }