您的位置:首页 > 编程语言 > C#

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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息