DepthImage.cs 6.2 KB

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