DebugImageCreator.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using bbiwarg.Input.InputHandling;
  8. using bbiwarg.Recognition.FingerRecognition;
  9. using bbiwarg.Recognition.HandRecognition;
  10. using bbiwarg.Recognition.PalmRecognition;
  11. using bbiwarg.Recognition.TouchRecognition;
  12. using Emgu.CV.Structure;
  13. using Emgu.CV;
  14. namespace bbiwarg.Output.DebugOutput
  15. {
  16. /// <summary>
  17. /// DebugImageCreator creates all debug images as <see cref="OutputImage"/> objects using the frame data.
  18. /// </summary>
  19. class DebugImageCreator
  20. {
  21. /// <summary>
  22. /// visualizes touch events
  23. /// </summary>
  24. private TouchEventVisualizer touchEventVisualizer;
  25. /// <summary>
  26. /// the depth image with additional interesting points
  27. /// </summary>
  28. public OutputImage DepthImage { get; private set; }
  29. /// <summary>
  30. /// the edge image with all detected fingers drawn into it
  31. /// </summary>
  32. public OutputImage FingerImage { get; private set; }
  33. /// <summary>
  34. /// image showing detected hands in diffrent colors
  35. /// </summary>
  36. public OutputImage HandImage { get; private set; }
  37. /// <summary>
  38. /// image showing the palm grid
  39. /// </summary>
  40. public OutputImage PalmImage { get; private set; }
  41. /// <summary>
  42. /// image showing the touch events in a grid
  43. /// </summary>
  44. public OutputImage TouchImage { get; private set; }
  45. /// <summary>
  46. /// Creates a DebugImageCreator.
  47. /// </summary>
  48. /// <param name="touchEventVisualizer">the touchEventVisualizer used to generate the TouchImage</param>
  49. public DebugImageCreator(TouchEventVisualizer touchEventVisualizer)
  50. {
  51. this.touchEventVisualizer = touchEventVisualizer;
  52. }
  53. /// <summary>
  54. /// Updates all debug images.
  55. /// </summary>
  56. /// <param name="frameData">data for the new frame</param>
  57. /// <param name="palmGridNumRows">number of rows in the palm grid</param>
  58. /// <param name="palmGridNumColumns">number of columns in the palm grid</param>
  59. public void updateImages(FrameData frameData, int palmGridNumRows, int palmGridNumColumns)
  60. {
  61. updateDepthImage(frameData);
  62. updateFingerImage(frameData);
  63. updateHandImage(frameData);
  64. updatePalmImage(frameData, palmGridNumRows, palmGridNumColumns);
  65. updateTouchImage(frameData, palmGridNumRows, palmGridNumColumns);
  66. }
  67. /// <summary>
  68. /// Updates the depth image.
  69. /// </summary>
  70. /// <param name="frameData">data for the new frame</param>
  71. private void updateDepthImage(FrameData frameData)
  72. {
  73. if (DepthImage != null)
  74. DepthImage.Dispose();
  75. DepthImage = new OutputImage(frameData.ImageSize);
  76. // background (depth image)
  77. DepthImage.drawImage((frameData.DepthImage.MaxDepth - frameData.DepthImage.MinDepth) - frameData.DepthImage.Image, Parameters.DepthImageColor);
  78. // finger tips
  79. foreach (Finger f in frameData.TrackedFingers)
  80. {
  81. Color tipColor = (f.Touch != null) ? Parameters.TouchEventDetectedColor : Parameters.FingerTipColor;
  82. Color handColor = Parameters.FingerHandColor;
  83. DepthImage.fillCircle(f.TipPoint, 5, tipColor);
  84. DepthImage.fillCircle(f.HandPoint, 5, handColor);
  85. }
  86. // border
  87. DepthImage.drawBorder(Parameters.OutputImageBorderColor);
  88. }
  89. /// <summary>
  90. /// Updates the finger image.
  91. /// </summary>
  92. /// <param name="frameData">data for the new frame</param>
  93. private void updateFingerImage(FrameData frameData)
  94. {
  95. if (FingerImage != null)
  96. FingerImage.Dispose();
  97. FingerImage = new OutputImage(frameData.ImageSize);
  98. // background (edge image)
  99. FingerImage.drawImage(frameData.EdgeImage.Image.ThresholdBinary(new Gray(0), new Gray(255)), Parameters.EdgeImageColor);
  100. // draw fingers
  101. foreach (Finger f in frameData.TrackedFingers)
  102. {
  103. foreach (FingerSlice slice in f.SliceTrail.Slices)
  104. FingerImage.drawLineSegment(slice.LineSegment, Parameters.FingerSliceColor);
  105. FingerImage.drawContour(f.getContour(Parameters.FingerContourMargin), Parameters.FingerContourColor);
  106. FingerImage.drawLineSegment(f.LineSegment, Parameters.FingerTrackedColor);
  107. FingerImage.drawText(f.MidPoint, f.TrackID.ToString(), Parameters.FingerIDColor);
  108. }
  109. //border
  110. FingerImage.drawBorder(Parameters.OutputImageBorderColor);
  111. }
  112. /// <summary>
  113. /// Updates the hand image.
  114. /// </summary>
  115. /// <param name="frameData">data for the new frame</param>
  116. private void updateHandImage(FrameData frameData)
  117. {
  118. if (HandImage != null)
  119. HandImage.Dispose();
  120. HandImage = new OutputImage(frameData.ImageSize);
  121. foreach (Hand h in frameData.TrackedHands)
  122. {
  123. HandImage.drawImage(h.Mask.ThresholdBinary(new Gray(0), new Gray(255)), Parameters.HandColors[h.TrackID % Parameters.HandNumColors]);
  124. HandImage.fillCircle(h.Centroid, 5, Parameters.HandCentroidColor);
  125. HandImage.drawText(h.Centroid, h.TrackID.ToString(), Parameters.HandIDColor);
  126. if (h.Palm != null)
  127. HandImage.drawDefect(h.Palm.ThumbDefect, Parameters.HandThumbDefectPointColor, Parameters.HandThumbDefectLineColor);
  128. }
  129. // border
  130. HandImage.drawBorder(Parameters.OutputImageBorderColor);
  131. }
  132. /// <summary>
  133. /// Updates the palm image.
  134. /// </summary>
  135. /// <param name="frameData">data for the new frame</param>
  136. /// <param name="numRows">number of rows in the palm grid</param>
  137. /// <param name="numColumns">number of columns in the palm grid</param>
  138. private void updatePalmImage(FrameData frameData, int numRows, int numColumns)
  139. {
  140. if (PalmImage != null)
  141. PalmImage.Dispose();
  142. PalmImage = new OutputImage(frameData.ImageSize);
  143. // handMask
  144. Image<Gray, byte> handMask = new Image<Gray, byte>(frameData.ImageSize.Width, frameData.ImageSize.Height);
  145. foreach (Hand h in frameData.TrackedHands)
  146. handMask = handMask.Or(h.Mask);
  147. // background
  148. PalmImage.drawImage((frameData.DepthImage.MaxDepth - frameData.DepthImage.MinDepth) - frameData.DepthImage.Image.Or(255 - handMask.ThresholdBinary(new Gray(0), new Gray(255))), Parameters.DepthImageColor);
  149. foreach (Touch t in frameData.TrackedTouches)
  150. PalmImage.fillCircle(t.AbsolutePosition, 5, Parameters.TouchEventTrackedColor);
  151. foreach (Palm p in frameData.TrackedPalms)
  152. PalmImage.drawQuadrangleGrid(p.Quad, Parameters.PalmQuadColor, Parameters.PalmGridColor, numRows, numColumns);
  153. // border
  154. PalmImage.drawBorder(Parameters.OutputImageBorderColor);
  155. }
  156. /// <summary>
  157. /// Updates the touch image.
  158. /// </summary>
  159. /// <param name="frameData">data for the new frame</param>
  160. /// <param name="numRows">number of rows in the palm grid</param>
  161. /// <param name="numColumns">number of columns in the palm grid</param>
  162. private void updateTouchImage(FrameData frameData, int numRows, int numColumns) {
  163. if (TouchImage != null)
  164. TouchImage.Dispose();
  165. TouchImage = touchEventVisualizer.getOutputImage(frameData.ImageSize, numRows, numColumns);
  166. }
  167. }
  168. }