用opencvSharp实现在任意多边形内寻找最大的内接正交矩形

用opencvSharp实现在任意多边形内寻找最大的内接正交矩形

之前写过一篇在任意多边形内寻找近似最大的内接正交矩形,但不怎么符合工作要求,于是再认真看了看之前那篇文章,最后总算是搞出来了。
原图:

用opencvSharp实现在任意多边形内寻找最大的内接正交矩形
结果:
用opencvSharp实现在任意多边形内寻找最大的内接正交矩形

1.第一步还是先求出多边形的近似轮廓,减少轮廓数量,方便后面计算。

2.根据轮廓让点与下一个点之间形成一个矩形,然后让每个矩形都与当前所有矩形相交,求出相交的矩形,再把这些矩形所有的角放到一个集合里。

用opencvSharp实现在任意多边形内寻找最大的内接正交矩形
3.最后去除重复的点,再让这些点两两组合成一个矩形,判断是否为内部矩形,如果是就算出面积,找出最大内接矩形。
用opencvSharp实现在任意多边形内寻找最大的内接正交矩形
比如一共4个点,第1个与第2个形成矩形(矩形1),第1与第3(矩形2),第1与第4(矩形3),第2与第3(矩形4),第2与第4(矩形5),第3与第4(矩形6);
由于矩形1为第一个元素,没有相交矩形,所以直接放入allPoint中;
接着把矩形2的四个角,以及矩形2和矩形1相交矩形的四个角,放入allPoint中;
矩形3以此类推,其本身四个角,以及和矩形1相交矩形的四个角,以及和矩形2相交矩形的四个角,放入allPoint中。

以上就是所有步骤了,代码实现起来还是比较简单的,但是这个方法的原理理解起来就比较困难了,看了半天也看不到原理。
完整代码:
        public Form1()
        {
            InitializeComponent();
            Test();
        }

        private static void Test()
        {
            var src = Cv2.ImRead("C:\\Users\\Administrator\\Desktop\\test.png", ImreadModes.Color);
            var dst = new Mat();
            Cv2.CvtColor(src, dst, ColorConversionCodes.RGB2GRAY);
            Cv2.FindContours(dst, out var contours, out var hierarchy, RetrievalModes.External,
                ContourApproximationModes.ApproxSimple);
            List<List<Point>> approxContours = new List<List<Point>>();
            for (int i = 0; i < contours.Length; i++)
            {

                var approxContour = Cv2.ApproxPolyDP(contours[i], 20, true);
                approxContours.Add(approxContour.ToList());
                DrawContour(src, approxContour, Scalar.White, 1);
            }

            foreach (var contour in approxContours)
            {
                GetMaxInscribedRect(src, contour);
            }

            Cv2.ImShow("src", src);
        }

        private static Rect GetMaxInscribedRect(Mat src, List<Point> contour)
        {

            Rect maxInscribedRect = new Rect();
            List<Rect> allRect = new List<Rect>();
            List<Point> allPoint = new List<Point>(contour);

            for (int i = 0; i < contour.Count; i++)
            {
                for (int j = i + 1; j < contour.Count; j++)
                {
                    var p1 = contour[i];
                    var p2 = contour[j];
                    if (p1.Y == p2.Y || p1.X == p2.X)
                        continue;
                    var tempRect = FromTowPoint(p1, p2);
                    allPoint.AddRange(GetAllCorner(tempRect));

                    foreach (var rect in allRect)
                    {
                        var intersectR = tempRect.Intersect(rect);
                        if (intersectR != Rect.Empty)
                            allPoint.AddRange(GetAllCorner(intersectR));
                    }

                    allRect.Add(tempRect);
                }
            }

            List<Point> distinctPoints = allPoint.Distinct().ToList();
            for (int i = 0; i < distinctPoints.Count; i++)
            {
                for (int j = i + 1; j < distinctPoints.Count; j++)
                {
                    var tempRect = FromTowPoint(distinctPoints[i], distinctPoints[j]);

                    if (!ContainPoints(contour, GetAllCorner(tempRect)) || ContainsAnyPt(tempRect, contour))
                        continue;
                    src.Rectangle(tempRect, Scalar.RandomColor(), 2);
                    if (tempRect.Width * tempRect.Height > maxInscribedRect.Width * maxInscribedRect.Height)
                        maxInscribedRect = tempRect;
                }
            }

            src.Rectangle(maxInscribedRect, Scalar.Yellow, 2);
            return maxInscribedRect == Rect.Empty ? Cv2.BoundingRect(contour) : maxInscribedRect;
        }

        public static Point[] GetAllCorner(Rect rect)
        {
            Point[] result = new Point[4];
            result[0] = rect.Location;
            result[1] = new Point(rect.X + rect.Width, rect.Y);
            result[2] = rect.BottomRight;
            result[3] = new Point(rect.X, rect.Y + rect.Height);
            return result;
        }

        public static bool ContainPoint(List<Point> contour, Point p1)
        {
            return Cv2.PointPolygonTest(contour, p1, false) > 0;
        }

        public static bool ContainPoints(List<Point> contour, IEnumerable<Point> points)
        {
            foreach (var point in points)
            {
                if (Cv2.PointPolygonTest(contour, point, false) < 0)
                    return false;
            }
            return true;
        }

        private static void DrawContour(Mat mat, Point[] contour, Scalar color, int thickness)
        {
            for (int i = 0; i < contour.Length; i++)
            {
                if (i + 1 < contour.Length)
                    Cv2.Line(mat, contour[i], contour[i + 1], color, thickness);
            }
        }

        private static bool ContainsAnyPt(Rect rect, IEnumerable<Point> points)
        {
            foreach (var point in points)
            {
                if (point.X > rect.X && point.X < rect.X + rect.Width && point.Y < rect.BottomRight.Y && point.Y > rect.Y)
                    return true;
            }
            return false;
        }

        public static Rect FromTowPoint(Point p1, Point p2)
        {
            if (p1.X == p2.X || p1.Y == p2.Y)
                return Rect.Empty;

            if (p1.X > p2.X && p1.Y < p2.Y)
            {
                (p1, p2) = (p2, p1);
            }
            else if (p1.X > p2.X && p1.Y > p2.Y)
            {
                (p1.X, p2.X) = (p2.X, p1.X);
            }
            else if (p1.X < p2.X && p1.Y < p2.Y)
            {
                (p1.Y, p2.Y) = (p2.Y, p1.Y);
            }
            return Rect.FromLTRB(p1.X, p2.Y, p2.X, p1.Y);
        }

Original: https://blog.csdn.net/weixin_43950082/article/details/124452565
Author: 云季云
Title: 用opencvSharp实现在任意多边形内寻找最大的内接正交矩形

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/641108/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球