DepthImage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 List<Finger> fingers;
  20. private Int16 minDepth;
  21. private Int16 maxDepth;
  22. public DepthImage(int width, int height, Image<Gray, Int16> image) {
  23. this.width = width;
  24. this.height = height;
  25. this.image = image;
  26. this.image = this.image.SmoothMedian(3);
  27. //minDepth
  28. findMinDepth();
  29. //maxDepth
  30. maxDepth = (Int16)(minDepth + 200);
  31. thresholdDepth(minDepth, maxDepth);
  32. //edges
  33. calculateEdges();
  34. //findFingerPoints
  35. findFingerPoints();
  36. //findFingers
  37. findFingers();
  38. }
  39. public int getWidth() {
  40. return width;
  41. }
  42. public int getHeight() {
  43. return height;
  44. }
  45. public Int16 getMinDepth() {
  46. return minDepth;
  47. }
  48. public Int16 getMaxDepth() {
  49. return maxDepth;
  50. }
  51. public Int16 getDepthAt(int x, int y) {
  52. return image.Data[y, x, 0];
  53. }
  54. public Int16 getEdgeAt(int x, int y)
  55. {
  56. return edges.Data[y, x, 0];
  57. }
  58. public bool isFingerPoint(int x, int y) {
  59. return fingerPoints[x, y];
  60. }
  61. private void setDepthAt(int x, int y, Int16 value) {
  62. image.Data[y, x, 0] = value;
  63. }
  64. private void findMinDepth() {
  65. minDepth = Int16.MaxValue;
  66. for (int x = 0; x < width; ++x)
  67. {
  68. for (int y = 0; y < height; ++y)
  69. {
  70. Int16 depth = getDepthAt(x,y);
  71. if (depth < minDepth)
  72. minDepth = depth;
  73. }
  74. }
  75. }
  76. private void thresholdDepth(int min, int max)
  77. {
  78. image = image.ThresholdToZero(new Gray(min));
  79. image = image.ThresholdTrunc(new Gray(max));
  80. for (int x = 0; x < width; ++x)
  81. {
  82. for (int y = 0; y < height; ++y)
  83. {
  84. if (getDepthAt(x, y) == 0)
  85. setDepthAt(x, y, (short)max);
  86. }
  87. }
  88. }
  89. private void calculateEdges() {
  90. Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(Int16 depth) { return (byte)(((depth - minDepth) * Byte.MaxValue) / (maxDepth - minDepth)); });
  91. Image<Gray, byte> tmp2 = tmp.Canny(150, 75, 3);
  92. edges = tmp2.Convert<Int16>(delegate(byte depth) { if(depth > 0) {return Int16.MaxValue;} else {return 0;}});//((depth * Int16.MaxValue) / Byte.MaxValue); });
  93. }
  94. private void findFingerPoints()
  95. {
  96. int maxFingerSize = 30;
  97. int minFingerSize = 10;
  98. fingerPoints = new bool[width, height];
  99. for (int y = 0; y < height; y++) {
  100. for (int x = 0; x < width; x++) {
  101. if (getEdgeAt(x, y) > 0) {
  102. //search horizontal
  103. bool edgeRightFound = false;
  104. int edgeRightX = x + minFingerSize;
  105. while (!edgeRightFound && edgeRightX < width) {
  106. if (getEdgeAt(edgeRightX, y) > 0)
  107. edgeRightFound = true;
  108. else
  109. edgeRightX++;
  110. }
  111. if (edgeRightFound){
  112. int midX = (edgeRightX + x) / 2;
  113. Int16 depthLeft = getDepthAt(x, y);
  114. Int16 depthMid = getDepthAt(midX, y);
  115. Int16 depthRight = getDepthAt(edgeRightX, y);
  116. if ((edgeRightX - x) < maxFingerSize && depthLeft > depthMid && depthMid < depthRight) {
  117. fingerPoints[midX, y] = true;
  118. }
  119. }
  120. //search vertical
  121. bool edgeBottomFound = false;
  122. int edgeBottomY = y + minFingerSize;
  123. while (!edgeBottomFound && edgeBottomY < height) {
  124. if (getEdgeAt(x, edgeBottomY) > 0)
  125. edgeBottomFound = true;
  126. else
  127. edgeBottomY++;
  128. }
  129. if (edgeBottomFound) {
  130. int midY = (edgeBottomY + y) / 2;
  131. Int16 depthTop = getDepthAt(x, y);
  132. Int16 depthMid = getDepthAt(x, midY);
  133. Int16 depthBottom = getDepthAt(x, edgeBottomY);
  134. if ((edgeBottomY - y) < maxFingerSize && depthTop > depthMid && depthMid < depthBottom) {
  135. fingerPoints[x, midY] = true;
  136. }
  137. }
  138. x = edgeRightX - 1;
  139. }
  140. }
  141. }
  142. }
  143. private void findFingers() {
  144. float maxDistanceTogether = 5.0f;
  145. float minFingerLength = 20.0f;
  146. List<Finger> possibleFingers = new List<Finger>();
  147. for (int y = 0; y < height; y++) {
  148. for (int x = 0; x < width; x++) {
  149. if (fingerPoints[x, y]) {
  150. Point fingerPoint = new Point(x,y);
  151. float minDistanceValue = float.MaxValue;
  152. int minDistanceIndex = 0;
  153. for (int i = 0; i < possibleFingers.Count; i++)
  154. {
  155. float distance = possibleFingers[i].getMinDistance(fingerPoint);
  156. if (distance < minDistanceValue) {
  157. minDistanceValue = distance;
  158. minDistanceIndex = i;
  159. }
  160. }
  161. if (minDistanceValue < maxDistanceTogether)
  162. {
  163. possibleFingers[minDistanceIndex].add(fingerPoint);
  164. }
  165. else
  166. {
  167. possibleFingers.Add(new Finger(fingerPoint));
  168. }
  169. }
  170. }
  171. }
  172. fingers = new List<Finger>();
  173. fingerPoints = new bool[width, height];
  174. foreach (Finger finger in possibleFingers) {
  175. float length = finger.getLength();
  176. if (length > minFingerLength)
  177. {
  178. fingers.Add(finger);
  179. foreach (Point fingerPoint in finger.getFingerPoints()) {
  180. fingerPoints[fingerPoint.X, fingerPoint.Y] = true;
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }