DepthImage.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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
  11. {
  12. class DepthImage
  13. {
  14. private int width;
  15. private int height;
  16. private Image<Gray, Int16> image;
  17. private Image<Gray, Int16> edges;
  18. private bool[,] fingerPoints;
  19. private Int16 minDepth;
  20. private Int16 maxDepth;
  21. public DepthImage(int width, int height, Image<Gray, Int16> image) {
  22. this.width = width;
  23. this.height = height;
  24. this.image = image;
  25. this.image = this.image.SmoothMedian(3);
  26. //minDepth
  27. findMinDepth();
  28. //maxDepth
  29. maxDepth = (Int16)(minDepth + 200);
  30. thresholdDepth(minDepth, maxDepth);
  31. //edges
  32. calculateEdges();
  33. //findFingerPoints
  34. findFingerPoints();
  35. }
  36. public int getWidth() {
  37. return width;
  38. }
  39. public int getHeight() {
  40. return height;
  41. }
  42. public Int16 getMinDepth() {
  43. return minDepth;
  44. }
  45. public Int16 getMaxDepth() {
  46. return maxDepth;
  47. }
  48. public Int16 getDepthAt(int x, int y) {
  49. return image.Data[y, x, 0];
  50. }
  51. public Int16 getEdgeAt(int x, int y)
  52. {
  53. return edges.Data[y, x, 0];
  54. }
  55. public bool isFingerPoint(int x, int y) {
  56. return fingerPoints[x, y];
  57. }
  58. private void setDepthAt(int x, int y, Int16 value) {
  59. image.Data[y, x, 0] = value;
  60. }
  61. private void findMinDepth() {
  62. minDepth = Int16.MaxValue;
  63. for (int x = 0; x < width; ++x)
  64. {
  65. for (int y = 0; y < height; ++y)
  66. {
  67. Int16 depth = getDepthAt(x,y);
  68. if (depth < minDepth)
  69. minDepth = depth;
  70. }
  71. }
  72. }
  73. private void thresholdDepth(int min, int max)
  74. {
  75. image = image.ThresholdToZero(new Gray(min));
  76. image = image.ThresholdTrunc(new Gray(max));
  77. for (int x = 0; x < width; ++x)
  78. {
  79. for (int y = 0; y < height; ++y)
  80. {
  81. if (getDepthAt(x, y) == 0)
  82. setDepthAt(x, y, (short)max);
  83. }
  84. }
  85. }
  86. private void calculateEdges() {
  87. Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(Int16 depth) { return (byte)(((depth - minDepth) * Byte.MaxValue) / (maxDepth - minDepth)); });
  88. Image<Gray, byte> tmp2 = tmp.Canny(150, 75, 3);
  89. edges = tmp2.Convert<Int16>(delegate(byte depth) { if(depth > 0) {return Int16.MaxValue;} else {return 0;}});//((depth * Int16.MaxValue) / Byte.MaxValue); });
  90. }
  91. private void findFingerPoints()
  92. {
  93. int maxFingerSize = 30;
  94. int minFingerSize = 10;
  95. fingerPoints = new bool[width, height];
  96. for (int y = 0; y < height; y++) {
  97. for (int x = 0; x < width; x++) {
  98. if (getEdgeAt(x, y) > 0) {
  99. //search horizontal
  100. bool edgeRightFound = false;
  101. int edgeRightX = x + minFingerSize;
  102. while (!edgeRightFound && edgeRightX < width) {
  103. if (getEdgeAt(edgeRightX, y) > 0)
  104. edgeRightFound = true;
  105. else
  106. edgeRightX++;
  107. }
  108. if (edgeRightFound){
  109. int midX = (edgeRightX + x) / 2;
  110. Int16 depthLeft = getDepthAt(x, y);
  111. Int16 depthMid = getDepthAt(midX, y);
  112. Int16 depthRight = getDepthAt(edgeRightX, y);
  113. if ((edgeRightX - x) < maxFingerSize && depthLeft > depthMid && depthMid < depthRight) {
  114. fingerPoints[midX, y] = true;
  115. }
  116. }
  117. //search vertical
  118. bool edgeBottomFound = false;
  119. int edgeBottomY = y + minFingerSize;
  120. while (!edgeBottomFound && edgeBottomY < height) {
  121. if (getEdgeAt(x, edgeBottomY) > 0)
  122. edgeBottomFound = true;
  123. else
  124. edgeBottomY++;
  125. }
  126. if (edgeBottomFound) {
  127. int midY = (edgeBottomY + y) / 2;
  128. Int16 depthTop = getDepthAt(x, y);
  129. Int16 depthMid = getDepthAt(x, midY);
  130. Int16 depthBottom = getDepthAt(x, edgeBottomY);
  131. if ((edgeBottomY - y) < maxFingerSize && depthTop > depthMid && depthMid < depthBottom) {
  132. fingerPoints[x, midY] = true;
  133. }
  134. }
  135. x = edgeRightX - 1;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }