Cv2_objdetect.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using OpenCvSharp.Internal;
  2. using OpenCvSharp.Internal.Vectors;
  3. namespace OpenCvSharp;
  4. static partial class Cv2
  5. {
  6. /// <summary>
  7. /// Groups the object candidate rectangles.
  8. /// </summary>
  9. /// <param name="rectList"> Input/output vector of rectangles. Output vector includes retained and grouped rectangles.</param>
  10. /// <param name="groupThreshold">Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.</param>
  11. /// <param name="eps"></param>
  12. public static void GroupRectangles(IList<Rect> rectList, int groupThreshold, double eps = 0.2)
  13. {
  14. if (rectList is null)
  15. throw new ArgumentNullException(nameof(rectList));
  16. using var rectListVec = new VectorOfRect(rectList);
  17. NativeMethods.HandleException(
  18. NativeMethods.objdetect_groupRectangles1(rectListVec.CvPtr, groupThreshold, eps));
  19. ClearAndAddRange(rectList, rectListVec.ToArray());
  20. }
  21. /// <summary>
  22. /// Groups the object candidate rectangles.
  23. /// </summary>
  24. /// <param name="rectList"> Input/output vector of rectangles. Output vector includes retained and grouped rectangles.</param>
  25. /// <param name="weights"></param>
  26. /// <param name="groupThreshold">Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.</param>
  27. /// <param name="eps">Relative difference between sides of the rectangles to merge them into a group.</param>
  28. public static void GroupRectangles(IList<Rect> rectList, out int[] weights, int groupThreshold, double eps = 0.2)
  29. {
  30. if (rectList is null)
  31. throw new ArgumentNullException(nameof(rectList));
  32. using var rectListVec = new VectorOfRect(rectList);
  33. using var weightsVec = new VectorOfInt32();
  34. NativeMethods.HandleException(
  35. NativeMethods.objdetect_groupRectangles2(rectListVec.CvPtr, weightsVec.CvPtr, groupThreshold, eps));
  36. ClearAndAddRange(rectList, rectListVec.ToArray());
  37. weights = weightsVec.ToArray();
  38. }
  39. /// <summary>
  40. /// Groups the object candidate rectangles.
  41. /// </summary>
  42. /// <param name="rectList"></param>
  43. /// <param name="groupThreshold"></param>
  44. /// <param name="eps"></param>
  45. /// <param name="weights"></param>
  46. /// <param name="levelWeights"></param>
  47. public static void GroupRectangles(IList<Rect> rectList, int groupThreshold, double eps, out int[] weights, out double[] levelWeights)
  48. {
  49. if (rectList is null)
  50. throw new ArgumentNullException(nameof(rectList));
  51. using var rectListVec = new VectorOfRect(rectList);
  52. using var weightsVec = new VectorOfInt32();
  53. using var levelWeightsVec = new VectorOfDouble();
  54. NativeMethods.HandleException(
  55. NativeMethods.objdetect_groupRectangles3(
  56. rectListVec.CvPtr, groupThreshold, eps, weightsVec.CvPtr, levelWeightsVec.CvPtr));
  57. ClearAndAddRange(rectList, rectListVec.ToArray());
  58. weights = weightsVec.ToArray();
  59. levelWeights = levelWeightsVec.ToArray();
  60. }
  61. /// <summary>
  62. /// Groups the object candidate rectangles.
  63. /// </summary>
  64. /// <param name="rectList"></param>
  65. /// <param name="rejectLevels"></param>
  66. /// <param name="levelWeights"></param>
  67. /// <param name="groupThreshold"></param>
  68. /// <param name="eps"></param>
  69. public static void GroupRectangles(IList<Rect> rectList, out int[] rejectLevels, out double[] levelWeights, int groupThreshold, double eps = 0.2)
  70. {
  71. if (rectList is null)
  72. throw new ArgumentNullException(nameof(rectList));
  73. using var rectListVec = new VectorOfRect(rectList);
  74. using var rejectLevelsVec = new VectorOfInt32();
  75. using var levelWeightsVec = new VectorOfDouble();
  76. NativeMethods.HandleException(
  77. NativeMethods.objdetect_groupRectangles4(
  78. rectListVec.CvPtr, rejectLevelsVec.CvPtr, levelWeightsVec.CvPtr, groupThreshold, eps));
  79. ClearAndAddRange(rectList, rectListVec.ToArray());
  80. rejectLevels = rejectLevelsVec.ToArray();
  81. levelWeights = levelWeightsVec.ToArray();
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. /// <param name="rectList"></param>
  87. /// <param name="foundWeights"></param>
  88. /// <param name="foundScales"></param>
  89. /// <param name="detectThreshold"></param>
  90. /// <param name="winDetSize"></param>
  91. public static void GroupRectanglesMeanshift(IList<Rect> rectList, out double[] foundWeights,
  92. out double[] foundScales, double detectThreshold = 0.0, Size? winDetSize = null)
  93. {
  94. if (rectList is null)
  95. throw new ArgumentNullException(nameof(rectList));
  96. var winDetSize0 = winDetSize.GetValueOrDefault(new Size(64, 128));
  97. using var rectListVec = new VectorOfRect(rectList);
  98. using var foundWeightsVec = new VectorOfDouble();
  99. using var foundScalesVec = new VectorOfDouble();
  100. NativeMethods.HandleException(
  101. NativeMethods.objdetect_groupRectangles_meanshift(
  102. rectListVec.CvPtr, foundWeightsVec.CvPtr, foundScalesVec.CvPtr, detectThreshold, winDetSize0));
  103. ClearAndAddRange(rectList, rectListVec.ToArray());
  104. foundWeights = foundWeightsVec.ToArray();
  105. foundScales = foundScalesVec.ToArray();
  106. }
  107. private static void ClearAndAddRange<T>(ICollection<T> list, IEnumerable<T> values)
  108. {
  109. list.Clear();
  110. foreach (var t in values)
  111. {
  112. list.Add(t);
  113. }
  114. }
  115. }