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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cvMat/article/details/53707037

程序配图

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();
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页