DebugImageCreator.cs 7.9 KB

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