close

private void button14_Click(object sender, EventArgs e)
        {
            // ConvexHull()
            /*
             * public static void ConvexHull(IInputArray points,
                IOutputArray hull, bool clockwise = false, bool returnPoints =
                true);//取得外接多邊
            */
            Random rd = new Random();
            int length = rd.Next(3, 100);
            VectorOfPoint vector_Point = new VectorOfPoint();
            Point[] points = new Point[length];
            Image<Bgr, byte> dst = new Image<Bgr, byte>(imageBox1.Size);

            for (int i = 0 ; i< length ;i++)
            {
                Point point = new Point(rd.Next(imageBox1.Width * 1 / 4, imageBox1.Width * 3 / 4),
                    rd.Next(imageBox1.Height * 1 / 4, imageBox1.Height * 3 / 4));

                // 在imagebox control的1/4 到3/4隨機創建點
                points[i] = point;
            }
            foreach(PointF f in points)
            {
                dst.Draw(new CircleF(f,1),new Bgr(rd.Next(0,255),
                    rd.Next(0, 255), rd.Next(0, 255)), 3);
            }
            vector_Point.Push(points);
            // 取得最外部的矩形
            Rectangle rect = CvInvoke.BoundingRectangle(vector_Point);
            // 提取最小面積矩形
            CvInvoke.MinAreaRect(vector_Point);
            // 提取最小面積圓
            CvInvoke.MinEnclosingCircle(vector_Point);
            
            // 創建VectorOfPoint 類型,存儲三角形
            VectorOfPoint tri = new VectorOfPoint();
            // 創建 vectorOfPoint存儲凸包點
            VectorOfPoint hull = new VectorOfPoint();

            // 取得外接三角型
            double area = CvInvoke.MinEnclosingTriangle(vector_Point, tri);
            // 凸包檢測
            CvInvoke.ConvexHull(vector_Point, hull);

            //採用blue  畫筆繪製外接矩形
            dst.Draw(rect, new Bgr(255, 0, 0), 2);
            //採用green 畫筆繪製外接矩形
            dst.Draw(rect, new Bgr(0, 255, 0), 2);
            //採用red 畫筆繪製外接矩形
            dst.Draw(rect, new Bgr(0, 0, 255), 2);
            //採用purple畫筆來繪製凸包
            dst.Draw(hull.ToArray(), new Bgr(255, 0, 255), 2);

            imageBox1.Image = dst;
        }

 

 

 

 

arrow
arrow
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()