Emgu 边缘检测,LineSegment2D[]画直线,CircleF[]画圆

程序配图

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;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;

namespace CannyEdgeDetector
{
    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();
                //转成灰度图
                Image<Gray, Byte> gray1 = img1.Convert<Gray, Byte>();
                //Canny 边缘检测
                Image<Gray, Byte> cannyGray = gray1.Canny(120, 180);
                pictureBox2.Image = cannyGray.ToBitmap();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //从pictureBox2中加载图像
            Image<Gray, Byte> cannyGray = new Image<Gray, byte>(new Bitmap(pictureBox2.Image));
            //调用 HoughLinesBinary检测直线 ,返回一个 LineSegment2D[][]数组
            LineSegment2D[] lines = cannyGray.HoughLinesBinary(1, Math.PI / 45.0, 20, 30, 10)[0];
            //画线
            Image<Bgr, Byte> imageLines = new Image<Bgr, byte>(cannyGray.Width, cannyGray.Height);
            //遍历二维直线数组
            foreach (LineSegment2D line in lines)
            {
                ///在imageLines上将直线画出
                imageLines.Draw(line, new Bgr(Color.DeepSkyBlue), 5);
            }
            //显示结果
            pictureBox2.Image = imageLines.ToBitmap();
        }

        private void button3_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();
                //转灰度图
                Image<Gray, Byte> gray1 = img1.Convert<Gray, Byte>();
                //调用HoughCircles (Canny included)检测圆形
                CircleF[] circles = gray1.HoughCircles(
                    new Gray(180),  //cannyThreshold
                    new Gray(120),  //accumulatorThreshold
                    2.0,            //dp
                    15.0,           //minDist
                    5,              //minRadius
                    0               //maxRadius
                    )[0];
                //画圆
                //img1.CopyBlank()直接复制上一步创建的空白图像
                Image<Bgr, Byte> imageCircles = img1.CopyBlank();
                foreach (CircleF circle in circles)
                {
                    imageCircles.Draw(circle, new Bgr(Color.Yellow), 5);
                }
                //显示结果
                pictureBox2.Image = imageCircles.ToBitmap();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值