예제 #1
0
        /// <summary>
        /// Applies the non-maximum suppression algorithm to the specified <see cref="Observation"/>s using the
        /// specified IOU (intersection over union) threshold and confidence threshold.
        /// </summary>
        /// <param name="observations">The specified <see cref="Observation"/>s.</param>
        /// <param name="iouThreshold">The specified threshold for IOU (intersection over union).</param>
        /// <param name="confidenceThreshold">The specified threshold for low-confidence pruning.</param>
        /// <returns><see cref="Observation"/>s selected according to the specified thresholds.</returns>
        public static IEnumerable <Observation> NonMaximumSuppression(IEnumerable <Observation> observations, double
                                                                      iouThreshold, double confidenceThreshold)
        {
            var observationList = observations.ToList();
            var sorted          = SortObservationsByConfidence(observationList);
            var selected        = new List <Observation>();

            foreach (var observationA in sorted)
            {
                if (observationA.Confidence < confidenceThreshold)
                {
                    break;
                }

                var shouldSelect = true;

                foreach (var observationB in selected)
                {
                    if (!(SKRectExtensions.IntersectionOverUnion(observationA.BoundingBox, observationB.BoundingBox) >
                          iouThreshold))
                    {
                        continue;
                    }
                    shouldSelect = false;
                    break;
                }

                if (shouldSelect)
                {
                    selected.Add(observationA);
                }
            }

            return(selected);
        }
        public void TestIntersectionOverUnion()
        {
            var rect1  = new SKRect(1f, 2f, 3f, 4f);
            var rect2  = new SKRect(2f, 3f, 5f, 7f);
            var result = SKRectExtensions.IntersectionOverUnion(rect1, rect2);

            Assert.AreEqual(1.0f / 15.0f, result);
        }
        public void TestUnion_Intersect()
        {
            var rect1  = new SKRect(1f, 2f, 3f, 4f);
            var rect2  = new SKRect(2f, 3f, 5f, 7f);
            var result = SKRectExtensions.Union(rect1, rect2);

            Assert.AreEqual(15.0f, result, Tolerance);
        }