Emgu 边缘检测,LineSegment2D[]画直线,CircleF[]画圆
2016-12-17 14:25
633 查看
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(); } } } }
相关文章推荐
- c#调用COM组件
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#使用第三方组件生成二维码汇总
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现给图片加水印的方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#中的delegate委托类型基本学习教程
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- 举例讲解C#中自动实现的属性
- C#中this的用法集锦
- C#数据结构之顺序表(SeqList)实例详解
- C#.NET获取拨号连接的宽带连接方法
- C#网络适配器简单操作
- C#实现导出List数据到xml文件的方法【附demo源码下载】
- C#异步绑定数据实现方法