DepthImage.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. Stopwatch sw = new Stopwatch();
  27. sw.Start();
  28. this.image = this.image.SmoothMedian(3);
  29. //minDepth
  30. findMinDepth();
  31. //maxDepth
  32. maxDepth = (Int16)(minDepth + 200);
  33. thresholdDepth(minDepth, maxDepth);
  34. //edges
  35. calculateEdges();
  36. //findFingerPoints
  37. findFingerPoints();
  38. //findFingers
  39. findFingers();
  40. sw.Stop();
  41. Console.WriteLine(sw.ElapsedMilliseconds);
  42. }
  43. public int getWidth() {
  44. return width;
  45. }
  46. public int getHeight() {
  47. return height;
  48. }
  49. public Int16 getMinDepth() {
  50. return minDepth;
  51. }
  52. public Int16 getMaxDepth() {
  53. return maxDepth;
  54. }
  55. public Int16 getDepthAt(int x, int y) {
  56. return image.Data[y, x, 0];
  57. }
  58. public Int16 getEdgeAt(int x, int y)
  59. {
  60. return edges.Data[y, x, 0];
  61. }
  62. public bool isFingerPoint(int x, int y) {
  63. return fingerPoints[x, y];
  64. }
  65. private void setDepthAt(int x, int y, Int16 value) {
  66. image.Data[y, x, 0] = value;
  67. }
  68. private void findMinDepth() {
  69. minDepth = Int16.MaxValue;
  70. for (int x = 0; x < width; ++x)
  71. {
  72. for (int y = 0; y < height; ++y)
  73. {
  74. Int16 depth = getDepthAt(x,y);
  75. if (depth < minDepth)
  76. minDepth = depth;
  77. }
  78. }
  79. }
  80. private void thresholdDepth(int min, int max)
  81. {
  82. image = image.ThresholdToZero(new Gray(min));
  83. image = image.ThresholdTrunc(new Gray(max));
  84. for (int x = 0; x < width; ++x)
  85. {
  86. for (int y = 0; y < height; ++y)
  87. {
  88. if (getDepthAt(x, y) == 0)
  89. setDepthAt(x, y, (short)max);
  90. }
  91. }
  92. }
  93. private void calculateEdges() {
  94. Image<Gray, Byte> tmp = image.Convert<Byte>(delegate(Int16 depth) { return (byte)(((depth - minDepth) * Byte.MaxValue) / (maxDepth - minDepth)); });
  95. Image<Gray, byte> tmp2 = tmp.Canny(100, 75, 3);
  96. edges = tmp2.Convert<Int16>(delegate(byte depth) { if(depth > 0) {return Int16.MaxValue;} else {return 0;}});//((depth * Int16.MaxValue) / Byte.MaxValue); });
  97. }
  98. private void findFingerPoints()
  99. {
  100. int maxFingerSize = 30;
  101. int minFingerSize = 10;
  102. fingerPoints = new bool[width, height];
  103. for (int y = 0; y < height; y++) {
  104. for (int x = 0; x < width; x++) {
  105. if (getEdgeAt(x, y) > 0) {
  106. //search horizontal
  107. bool edgeRightFound = false;
  108. int edgeRightX = x + minFingerSize;
  109. while (!edgeRightFound && edgeRightX < width) {
  110. if (getEdgeAt(edgeRightX, y) > 0)
  111. edgeRightFound = true;
  112. else
  113. edgeRightX++;
  114. }
  115. if (edgeRightFound){
  116. int midX = (edgeRightX + x) / 2;
  117. Int16 depthLeft = getDepthAt(x, y);
  118. Int16 depthMid = getDepthAt(midX, y);
  119. Int16 depthRight = getDepthAt(edgeRightX, y);
  120. if ((edgeRightX - x) < maxFingerSize && depthLeft > depthMid && depthMid < depthRight) {
  121. fingerPoints[midX, y] = true;
  122. }
  123. }
  124. //search vertical
  125. bool edgeBottomFound = false;
  126. int edgeBottomY = y + minFingerSize;
  127. while (!edgeBottomFound && edgeBottomY < height) {
  128. if (getEdgeAt(x, edgeBottomY) > 0)
  129. edgeBottomFound = true;
  130. else
  131. edgeBottomY++;
  132. }
  133. if (edgeBottomFound) {
  134. int midY = (edgeBottomY + y) / 2;
  135. Int16 depthTop = getDepthAt(x, y);
  136. Int16 depthMid = getDepthAt(x, midY);
  137. Int16 depthBottom = getDepthAt(x, edgeBottomY);
  138. if ((edgeBottomY - y) < maxFingerSize && depthTop > depthMid && depthMid < depthBottom) {
  139. fingerPoints[x, midY] = true;
  140. }
  141. }
  142. //x = edgeRightX - 1; // better performance but looses some points (skips x-values where it has already found a horizontal-fingerPoint, but it will miss any vertical-fingerPoints in this area)
  143. }
  144. }
  145. }
  146. }
  147. private void findFingers() {
  148. float maxDistanceTogether = 5.0f;
  149. float minFingerLength = 20.0f;
  150. List<Finger> possibleFingers = new List<Finger>();
  151. for (int y = 0; y < height; y++) {
  152. for (int x = 0; x < width; x++) {
  153. if (fingerPoints[x, y]) {
  154. Point fingerPoint = new Point(x,y);
  155. float minDistanceValue = float.MaxValue;
  156. int minDistanceIndex = 0;
  157. for (int i = 0; i < possibleFingers.Count; i++)
  158. {
  159. float distance = possibleFingers[i].getMinDistance(fingerPoint);
  160. if (distance < minDistanceValue) {
  161. minDistanceValue = distance;
  162. minDistanceIndex = i;
  163. }
  164. }
  165. if (minDistanceValue < maxDistanceTogether)
  166. {
  167. possibleFingers[minDistanceIndex].add(fingerPoint);
  168. }
  169. else
  170. {
  171. possibleFingers.Add(new Finger(fingerPoint));
  172. }
  173. }
  174. }
  175. }
  176. fingers = new List<Finger>();
  177. fingerPoints = new bool[width, height];
  178. foreach (Finger finger in possibleFingers) {
  179. float length = finger.getLength();
  180. if (length > minFingerLength)
  181. {
  182. fingers.Add(finger);
  183. /*foreach (Point fingerPoint in finger.getFingerPoints()) {
  184. fingerPoints[fingerPoint.X, fingerPoint.Y] = true;
  185. }*/
  186. List<Point> points = finger.getFingerPoints();
  187. PointF[] pointArray = new PointF[points.Count];
  188. int i = 0;
  189. foreach (Point p in points) {
  190. pointArray[i] = new PointF(p.X, p.Y);
  191. ++i;
  192. }
  193. PointF direction, pointOnLine;
  194. PointCollection.Line2DFitting(pointArray, Emgu.CV.CvEnum.DIST_TYPE.CV_DIST_L2, out direction, out pointOnLine);
  195. PointF start = projectToLine(finger.getFarthest(this), direction, pointOnLine);
  196. PointF end = projectToLine(finger.getNearest(this), direction, pointOnLine);
  197. setFingerPoints(start, end);
  198. }
  199. }
  200. }
  201. private PointF projectToLine(PointF p, PointF direction, PointF pointOnLine)
  202. {
  203. float px = p.X, py = p.Y, dx = direction.X, dy = direction.Y, ox = pointOnLine.X, oy = pointOnLine.Y;
  204. float diffx = px - ox;
  205. float diffy = py - oy;
  206. float diff_d = (diffx * dx + diffy * dy);
  207. float d_d = (dx * dx + dy * dy);
  208. float q = diff_d / d_d;
  209. return new PointF(ox + q * dx, oy + q * dy);
  210. }
  211. private void setFingerPoints(PointF start, PointF end)
  212. {
  213. // bresenham from wikipedia
  214. int xstart = (int)start.X, xend = (int)end.X, ystart = (int)start.Y, yend = (int)end.Y;
  215. int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
  216. /* Entfernung in beiden Dimensionen berechnen */
  217. dx = xend - xstart;
  218. dy = yend - ystart;
  219. /* Vorzeichen des Inkrements bestimmen */
  220. incx = dx < 0 ? -1 : 1;
  221. incy = dy < 0 ? -1 : 1;
  222. if (dx < 0) dx = -dx;
  223. if (dy < 0) dy = -dy;
  224. /* feststellen, welche Entfernung größer ist */
  225. if (dx > dy)
  226. {
  227. /* x ist schnelle Richtung */
  228. pdx = incx; pdy = 0; /* pd. ist Parallelschritt */
  229. ddx = incx; ddy = incy; /* dd. ist Diagonalschritt */
  230. es = dy; el = dx; /* Fehlerschritte schnell, langsam */
  231. }
  232. else
  233. {
  234. /* y ist schnelle Richtung */
  235. pdx = 0; pdy = incy; /* pd. ist Parallelschritt */
  236. ddx = incx; ddy = incy; /* dd. ist Diagonalschritt */
  237. es = dx; el = dy; /* Fehlerschritte schnell, langsam */
  238. }
  239. /* Initialisierungen vor Schleifenbeginn */
  240. x = xstart;
  241. y = ystart;
  242. err = el / 2;
  243. fingerPoints[Math.Min(getWidth() - 1, Math.Max(0, x)), Math.Min(getHeight() - 1, Math.Max(0, y))] = true;
  244. /* Pixel berechnen */
  245. for (t = 0; t < el; ++t) /* t zaehlt die Pixel, el ist auch Anzahl */
  246. {
  247. /* Aktualisierung Fehlerterm */
  248. err -= es;
  249. if (err < 0)
  250. {
  251. /* Fehlerterm wieder positiv (>=0) machen */
  252. err += el;
  253. /* Schritt in langsame Richtung, Diagonalschritt */
  254. x += ddx;
  255. y += ddy;
  256. }
  257. else
  258. {
  259. /* Schritt in schnelle Richtung, Parallelschritt */
  260. x += pdx;
  261. y += pdy;
  262. }
  263. fingerPoints[Math.Min(getWidth() - 1, Math.Max(0, x)), Math.Min(getHeight() - 1, Math.Max(0, y))] = true;
  264. }
  265. }
  266. }
  267. }