private List <Cv.Rect> MergeRects(List <Cv.Rect> rects, int width, int height)
        {
            List <int> ignoreIndices = new List <int>();

            rects.Sort((l, r) => l.X.CompareTo(r.X));

            for (int i = 0; i < rects.Count; i++)
            {
                if (ignoreIndices.Contains(i))
                {
                    continue;
                }

                for (int j = i + 1; j < rects.Count; j++)
                {
                    if (rects[j].Height < 5 || rects[j].Width < 5)
                    {
                        ignoreIndices.Add(j);
                        continue;
                    }

                    var hExpand = (int)Math.Round(width * 0.01, 0);
                    var r       = new Cv.Rect(rects[j].X, rects[j].Y, rects[j].Width, rects[j].Height);
                    r.Inflate(hExpand / 2, 0);

                    if (rects[i].IntersectsWith(r))
                    {
                        var union = rects[i].Union(rects[j]);

                        if (union.Width < width * 0.30 && union.Height < height * 0.05)
                        {
                            rects[i] = union;
                            ignoreIndices.Add(j);
                        }
                    }
                }
            }

            var merged = new List <Cv.Rect>();

            for (int i = 0; i < rects.Count; i++)
            {
                rects[i] = FixInvalidRects(rects[i], width, height);

                if (!ignoreIndices.Contains(i))
                {
                    merged.Add(rects[i]);
                }
            }
            return(merged);
        }
Example #2
0
        /// <summary>
        /// このCvRectを指定の量だけ膨らませる
        /// </summary>
        /// <param name="rect">対象の矩形</param>
        /// <param name="x">水平方向の膨張量</param>
        /// <param name="y">垂直方向の膨張量</param>
        /// <returns></returns>
#else
        /// <summary>
        /// Creates and returns an inflated copy of the specified CvRect structure.
        /// </summary>
        /// <param name="rect">The Rectangle with which to start. This rectangle is not modified. </param>
        /// <param name="x">The amount to inflate this Rectangle horizontally. </param>
        /// <param name="y">The amount to inflate this Rectangle vertically. </param>
        /// <returns></returns>
#endif
        public static Rect Inflate(Rect rect, int x, int y)
        {
            rect.Inflate(x, y);
            return(rect);
        }