Emgu 边缘检测,Triangle2DF[]画三角形,MCvBox2D[]画矩形

程序配图

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV.Util;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

//Emgu image<,>将pictureBox1控件中的图像读入
//Memstorage对OpenCV的cvMemStorage的封装
namespace ContourFinding
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strFileName = string.Empty;
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Image<Bgr, Byte> img1 = new Image<Bgr, Byte>(ofd.FileName);
                pictureBox1.Image = img1.ToBitmap();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //将pictureBox1控件中的图像读入
            Image<Bgr, Byte> img1 = new Image<Bgr, byte>(new Bitmap(pictureBox1.Image));
            //转换为灰度图像,然后对img1滤除噪声
            Image<Gray, Byte> gray1 = img1.Convert<Gray, Byte>().PyrDown().PyrUp();
            //Canny算子边缘检测
            Image<Gray, Byte> cannyGray = gray1.Canny(120, 180);

            //存储三角形和矩形的列表
            //三角形
            List<Triangle2DF> triangleList = new List<Triangle2DF>();
            List<MCvBox2D> boxList = new List<MCvBox2D>();
            //实例化Memstorage,对OpenCV的cvMemStorage的封装
            //Contour轮廓
            //
            using (MemStorage storage1 = new MemStorage())
                for (Contour<Point> contours1 = cannyGray.FindContours(); contours1 != null; contours1 = contours1.HNext)
                {
                    //多边形逼近
                    Contour<Point> contoursAP = contours1.ApproxPoly(contours1.Perimeter * 0.05, storage1);
                    //利用区域面积判断
                    if (contours1.Area >= 200)
                    {
                        //用顶点数量来决定形状
                        if (contoursAP.Total == 3)
                        {
                            //三角形
                            Point[] points = contoursAP.ToArray();
                            triangleList.Add(new Triangle2DF(
                                points[0],
                                points[1],
                                points[2]
                                ));
                        }
                        else if (contoursAP.Total == 4)
                        { 
                            //四边形
                            bool isRectangle = true;
                            Point[] points = contoursAP.ToArray();
                            LineSegment2D[] edges = PointCollection.PolyLine(points, true);
                            //在[ 75,105 ]的范围内将被检测
                            for (int i = 0; i < edges.Length; i++)
                            {
                                double angle = Math.Abs(
                                   edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i]));
                                if (angle < 75 || angle > 105)
                                {
                                    isRectangle = false;
                                    break;
                                }
                            }
                            if (isRectangle)
                            {
                                boxList.Add(contoursAP.GetMinAreaRect());
                            }
                        }
                    }
                }
            //Draw result
            Image<Bgr, Byte> imageResult = img1.CopyBlank();
            foreach (Triangle2DF triangle in triangleList)
                imageResult.Draw(triangle, new Bgr(Color.LightSteelBlue), 5);
            foreach (MCvBox2D box in boxList)
                imageResult.Draw(box, new Bgr(Color.LimeGreen), 5);
            //Show result
            pictureBox2.Image = imageResult.ToBitmap();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值