用Emgu CV模板匹配等一些简单应用
来源:互联网 发布:高中数学网络教学 编辑:程序博客网 时间:2024/06/05 19:39
此程序是学习王先荣朋友的文章,里面很详细,有兴趣的朋友可以看看:http://www.cnblogs.com/xrwang/archive/2010/02/09/HowToUseContour.html
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.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using System.Diagnostics;
namespace Chuanlin2012._01
{
public partial class MainFrm : Form
{
public Image<Bgr, byte> src;
public Image<Bgr, byte> subsrc;
public MainFrm()
{
InitializeComponent();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog opflg = new OpenFileDialog();
opflg.Filter = "所有文件|*.*";
if (opflg.ShowDialog() == DialogResult.OK)
{
Image<Bgr, byte> rgbimg1 = new Image<Bgr, byte>(opflg.FileName);
imageBox1.Image = rgbimg1;
src = rgbimg1;
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageBox1.Image != null)
{
SaveFileDialog sfdlg = new SaveFileDialog();
sfdlg.Filter = "bmp文件|*.bmp";
sfdlg.FilterIndex = 2;
sfdlg.RestoreDirectory = true;
if (sfdlg.ShowDialog() == DialogResult.OK)
{
string fname = sfdlg.FileName;
Bitmap bmp = imageBox1.Image.Bitmap;
bmp.Save(fname, System.Drawing.Imaging.ImageFormat.Bmp);
}
}
else
{
System.Windows.Forms.MessageBox.Show("请先打开图像!");
return;
}
}
private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void sobel算子ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageBox1.Image != null)
{
Image<Bgr, float> imgsoble = src.Sobel(0, 1, 3);
imageBox1.Image = imgsoble;
}
else
{
System.Windows.Forms.MessageBox.Show("请先打开图像!");
return;
}
}
private void laplace算子ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageBox1.Image != null)
{
Image<Bgr, float> imglaplace = src.Laplace(3);
imageBox1.Image = imglaplace;
}
else
{
System.Windows.Forms.MessageBox.Show("请先打开图像!");
return;
}
}
private void canny算子ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageBox1.Image != null)
{
//CannyFrm cfrm = new CannyFrm();
//cfrm.Show();
double m = 90;
double n = 255;
Gray threshold1 = new Gray(m);
Gray threshold2 = new Gray(n);
Image<Gray, byte> imggray = src.Convert<Gray, byte>();
Image<Gray, byte> imgcanny = imggray.Canny(threshold1, threshold2);
imageBox1.Image = imgcanny;
}
else
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
private void 二值化后绘制图像轮廓ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageBox1.Image != null)
{
Image<Gray, byte> imggray = src.Convert<Gray, byte>();
Image<Gray, byte> img2b = imggray.ThresholdBinary(new Gray(60), new Gray(255));
Contour<Point> contour = img2b.FindContours(); //找轮廓
Image<Bgr, byte> imgbgr = img2b.Convert<Bgr, byte>();
int maxLevel = 0; //绘制的轮廓深度
imgbgr.Draw(contour, new Bgr(0, 0, 255), new Bgr(0, 0, 255), maxLevel, 1);//绘制图像轮廓
imageBox1.Image = imgbgr;
}
else
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
private void SmoothBlur算子ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (imageBox1.Image != null)
{
Image<Bgr, byte> imgsmoot = src.SmoothBlur(9, 9);
imageBox1.Image = imgsmoot;
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog opdlg = new OpenFileDialog();
opdlg.Filter = "所有文件|*.*";
if (opdlg.ShowDialog() == DialogResult.OK)
{
Image<Bgr, byte> imgrgb = new Image<Bgr, byte>(opdlg.FileName);
imageBox2.Image = imgrgb;
subsrc = imgrgb;
}
}
//六种模板匹配方法
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (imageBox1.Image != null && imageBox2.Image != null)
{
double time;
double totalTime = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
//输入图像
Image<Bgr, Byte> imageInput =src ;
//模板图像
Image<Bgr, Byte> imageTemplate = subsrc;
//匹配方式数组
TM_TYPE tmType = TM_TYPE.CV_TM_CCOEFF;
//输出图像(匹配结果)
Image<Gray, Single> imageResult = imageInput.MatchTemplate(imageTemplate,tmType);
//归一化结果
//CvInvoke.cvNormalize(imageResult.Ptr, imageResult.Ptr, 1d, 0d, NORM_TYPE.CV_MINMAX, IntPtr.Zero);
//imageBox1.Image = imageResult;
//找到最匹配的点,以及该点的值
double bestValue;
Point bestPoint;
FindBestMatchPointAndValue(imageResult, tmType, out bestValue, out bestPoint);
//在最匹配的点附近画一个跟模板一样大的矩形
Rectangle rect = new Rectangle(new Point(bestPoint.X - imageTemplate.Size.Width / 2, bestPoint.Y - imageTemplate.Size.Height / 2), imageTemplate.Size);
imageResult.Draw(rect, new Gray(bestValue), 2);
imageBox1.Image = imageResult;
time = sw.Elapsed.TotalMilliseconds;
totalTime += time;
sw.Stop();
textBox1.Text = totalTime.ToString();
sw.Reset();
}
else
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (imageBox1.Image != null && imageBox2.Image != null)
{
//输入图像
Image<Bgr, Byte> imageInput = src;
//模板图像
Image<Bgr, Byte> imageTemplate = subsrc;
TM_TYPE tmtype = TM_TYPE.CV_TM_CCORR;
Image<Gray, Single> imgResult = imageInput.MatchTemplate(imageTemplate, tmtype);
//找到最好的点和最好点的值
double bestValue;
Point bestPoint;
FindBestMatchPointAndValue(imgResult,tmtype,out bestValue,out bestPoint);
Rectangle rect = new Rectangle(new Point(bestPoint.X - imageTemplate.Width / 2, bestPoint.Y - imageTemplate.Height / 2), imageTemplate.Size);
imgResult.Draw(rect, new Gray(bestValue), 2);
imageBox1.Image = imgResult;
}
else
{
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if(imageBox1.Image!=null && imageBox2.Image!=null)
{
//输入图像
Image<Bgr, Byte> imageInput = src;
//模板图像
Image<Bgr, Byte> imageTemplate = subsrc;
TM_TYPE tmtype = TM_TYPE.CV_TM_SQDIFF;
Image<Gray, Single> imgResult = imageInput.MatchTemplate(imageTemplate, tmtype);
//找到最好的点和最好点的值
double bestValue;
Point bestPoint;
FindBestMatchPointAndValue(imgResult,tmtype,out bestValue,out bestPoint);
Rectangle rect = new Rectangle(new Point(bestPoint.X - imageTemplate.Width / 2, bestPoint.Y - imageTemplate.Height / 2), imageTemplate.Size);
imgResult.Draw(rect, new Gray(bestValue), 2);
imageBox1.Image = imgResult;
}
else
{
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
if (imageBox1.Image != null && imageBox2.Image != null)
{
//输入图像
Image<Bgr, Byte> imageInput = src;
//模板图像
Image<Bgr, Byte> imageTemplate = subsrc;
TM_TYPE tmtype = TM_TYPE.CV_TM_CCOEFF_NORMED;
Image<Gray, Single> imgResult = imageInput.MatchTemplate(imageTemplate, tmtype);
//找到最好的点和最好点的值
double bestValue;
Point bestPoint;
FindBestMatchPointAndValue(imgResult, tmtype, out bestValue, out bestPoint);
Rectangle rect = new Rectangle(new Point(bestPoint.X - imageTemplate.Width / 2, bestPoint.Y - imageTemplate.Height / 2), imageTemplate.Size);
imgResult.Draw(rect, new Gray(bestValue), 2);
imageBox1.Image = imgResult;
}
else
{
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
if (imageBox1.Image != null && imageBox2.Image != null)
{
//输入图像
Image<Bgr, Byte> imageInput = src;
//模板图像
Image<Bgr, Byte> imageTemplate = subsrc;
TM_TYPE tmtype = TM_TYPE.CV_TM_CCORR_NORMED;
Image<Gray, Single> imgResult = imageInput.MatchTemplate(imageTemplate, tmtype);
//找到最好的点和最好点的值
double bestValue;
Point bestPoint;
FindBestMatchPointAndValue(imgResult, tmtype, out bestValue, out bestPoint);
Rectangle rect = new Rectangle(new Point(bestPoint.X - imageTemplate.Width / 2, bestPoint.Y - imageTemplate.Height / 2), imageTemplate.Size);
imgResult.Draw(rect, new Gray(bestValue), 2);
imageBox1.Image = imgResult;
}
else
{
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
}
private void radioButton6_CheckedChanged(object sender, EventArgs e)
{
if (imageBox1.Image != null && imageBox2.Image != null)
{
//输入图像
Image<Bgr, Byte> imageInput = src;
//模板图像
Image<Bgr, Byte> imageTemplate = subsrc;
TM_TYPE tmtype = TM_TYPE.CV_TM_SQDIFF_NORMED;
Image<Gray, Single> imgResult = imageInput.MatchTemplate(imageTemplate, tmtype);
//找到最好的点和最好点的值
double bestValue;
Point bestPoint;
FindBestMatchPointAndValue(imgResult, tmtype, out bestValue, out bestPoint);
Rectangle rect = new Rectangle(new Point(bestPoint.X - imageTemplate.Width / 2, bestPoint.Y - imageTemplate.Height / 2), imageTemplate.Size);
imgResult.Draw(rect, new Gray(bestValue), 2);
}
else
{
System.Windows.Forms.MessageBox.Show("请先输入图像!");
}
}
private void FindBestMatchPointAndValue(Image<Gray, Single> image, TM_TYPE tmType, out double bestValue, out Point bestPoint)
{
bestValue = 0d;
bestPoint = new Point(0, 0);
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
image.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
//对于平方差匹配和归一化平方差匹配,最小值表示最好的匹配;其他情况下,最大值表示最好的匹配
if (tmType == TM_TYPE.CV_TM_SQDIFF || tmType == TM_TYPE.CV_TM_SQDIFF_NORMED)
{
bestValue = minValues[0];
bestPoint = minLocations[0];
}
else
{
bestValue = maxValues[0];
bestPoint = maxLocations[0];
}
}
private void 图像对比ToolStripMenuItem_Click(object sender, EventArgs e)
{
CompareFrm cpfrm = new CompareFrm();
cpfrm.Show();
}
}
}
- 用Emgu CV模板匹配等一些简单应用
- Emgu CV的安装
- Emgu CV--基础知识
- Emgu CV Lesson 1
- emgu cv 轮廓提取
- emgu cv 透视变换
- Emgu Cv示例
- Emgu.Cv基础知识
- EMGU CV环境配置
- Emgu Cv配置--VS2010、C#、Emgu CV配置
- Emgu CV 第一个实例
- VS2010、C#、Emgu CV配置
- Emgu.CV 人脸锁定
- emgu cv 读取摄像头图像
- Emgu CV 第一个实例
- 【OpenCV3】模板匹配——cv::matchTemplate()详解
- C#下的openCV(Emgu CV)
- C#下的openCV(Emgu CV)
- 设置UINavigationBar的title, 左右按钮等
- 判断文件路径是否有效
- Android中的几种网络请求方式详解
- myeclipse快捷键
- HDU2642 Stars 二维树状数组
- 用Emgu CV模板匹配等一些简单应用
- 黑马程序员 小程序
- java synchronized详解
- linux读写大文件
- HFC
- plain类型的tableView如何不显示多余的空cell
- Java程序员:一刻钟精通正则表达式
- Android的进程回收机制
- Android中资源文件夹res/raw和assets的使用(续)——分割文件以及合并文件