原创-EMGUCV的模板匹配与跟踪完成啦.doc_第1页
原创-EMGUCV的模板匹配与跟踪完成啦.doc_第2页
原创-EMGUCV的模板匹配与跟踪完成啦.doc_第3页
原创-EMGUCV的模板匹配与跟踪完成啦.doc_第4页
原创-EMGUCV的模板匹配与跟踪完成啦.doc_第5页
已阅读5页,还剩6页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

原创-EMGUCV的模板匹配与跟踪完成啦!兄弟们好!经过很多天的努力学习,我的EMGUCV的模板匹配完成了,用实际的摄像头取图象,再存为模板后,就能实现物体跟踪,还能进行相机上马达进行位置确定。好玩吧!弄两幅图来看下。这是匹配的,还能过行坐标二次定位。坐标二次定位的这是跟踪的以下为代码,只有自己写的部分,由软件生成的部分没有帖上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 System.Threading;using System.IO;using System.Drawing.Imaging;using Emgu.CV;using Emgu.CV.Structure;using Emgu.CV.CvEnum;using Emgu.CV.Util;using Emgu.CV.UI;using Emgu.CV.VideoSurveillance;using Emgu.Util;using Emgu.Util.TypeEnum;using Emgu.CV.GPU;namespace 模板匹配 public partial class Form1 : Form public Image src; public Image tempsrc; public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) OpenFileDialog of = new OpenFileDialog(); of.Filter = (jpg)|*.jpg; if (of.ShowDialog ()= DialogResult.OK) Image imsrc = new Image(of.FileName); imageBox2.Image = imsrc; src = imsrc; imageBox1.Image = null; private void button2_Click(object sender, EventArgs e) if (src != null) Image imgsrc = src.Clone();/不可直接将src符值给新建图象,它传的只是一个地址,不是数据,要先考贝后才能不再引响原图像 Image imggray = imgsrc.Convert(); Image imgthread = imggray.ThresholdBinary(new Gray(60), new Gray(255); Image imgcanny = imgthread.Canny(130,255); /Contour contour = imgcanny.FindContours();/这个函数用来找中间的轮廓的每一个点,是一个数组点,很多个才能组成一个圆。 CircleF cf = imgthread.HoughCircles(new Gray(130), new Gray(255), 10, 1, 1, 400)0; MCvFont mf = new MCvFont(FONT.CV_FONT_HERSHEY_COMPLEX, 1, 1); if (cf.Length 0) cf0.Radius = cf0.Radius + 50; imgsrc.Draw(cf0, new Bgr(0, 0, 255), 2); Rectangle rec = new Rectangle(int)(cf0.Center.X - cf0.Radius), (int)(cf0.Center.Y - cf0.Radius), (int)(cf0.Radius * 2), (int)(cf0.Radius * 2); imgsrc.Draw(rec, new Bgr(0, 0, 255), 2); imgsrc.Draw(cf0.Center.ToString() + , + cf0.Radius.ToString(), ref mf, new Point(0, src.Height - 30), new Bgr(0, 0, 255); Image templatebgr = src.Clone(); Image temp1 = templatebgr.GetSubRect(rec).Clone (); imageBox1.Image = temp1; temp1.Save(e:template.jpg); tempsrc = temp1.Clone(); MessageBox.Show(模板生成成功!已保存到e:template.jpg中); else imgsrc.Draw(Dnt find out circle!PLS try agina., ref mf, new Point(0, src.Height - 30), new Bgr(0, 0, 255); imageBox1.Image = imgcanny; private void button3_Click(object sender, EventArgs e) Image imgsrc = src.Convert().Clone(); Image readimg = new Image(e:template.jpg); Image template =readimg.Convert().Clone(); Image imgcolor=src.Clone(); Image imgresult = imgsrc.MatchTemplate(template, TM_TYPE.CV_TM_CCOEFF ).Convert () .Clone(); imageBox1.Image = imgresult; double bestvalue; Point bestpoint; FindBestPoint(imgresult, TM_TYPE.CV_TM_CCOEFF, out bestvalue, out bestpoint); Rectangle rec1 = new Rectangle(new Point(bestpoint.X, bestpoint.Y ), template.Size); imgcolor.Draw(rec1,new Bgr (0,0,255),2); MCvFont mf = new MCvFont(FONT.CV_FONT_HERSHEY_COMPLEX, 1, 1); imgcolor.Draw(rec1.X.ToString() +,+ rec1.Y.ToString(),ref mf ,new Point (0,src.Height -30),new Bgr (0,0,255); imageBox1.Image = imgcolor ; public void FindBestPoint(Image image,TM_TYPE tmtype,out double bestvalue,out Point bestpoint) bestvalue = 0d; bestpoint = new Point(0, 0); double max, min; Point maxl, minl; image.MinMax(out min, out max, out minl, out maxl); if (tmtype = TM_TYPE.CV_TM_SQDIFF | tmtype = TM_TYPE.CV_TM_SQDIFF_NORMED) bestvalue = min0; bestpoint = minl0; else bestvalue = max0; bestpoint = maxl0; private bool mousestatus = false; private Point startpoint; private Point endpoint; private Rectangle rectcurrent; private void imageBox2_MouseDown(object sender, MouseEventArgs e) if (imageBox2 .Image !=null ) startpoint.X = e.X; startpoint.Y = e.Y; mousestatus = true; private void imageBox2_MouseUp(object sender, MouseEventArgs e) if (mousestatus ) endpoint.X = e.X; endpoint.Y = e.Y; mousestatus = false; private void imageBox2_MouseMove(object sender, MouseEventArgs e) if (mousestatus ) endpoint.X = e.X; endpoint.Y = e.Y; Rectangle rec1 = new Rectangle(startpoint, new Size(Math.Abs(startpoint.X - endpoint.X), Math.Abs(startpoint.Y - endpoint.Y); Image imgdraw = src.Clone(); imgdraw.Draw(rec1, new Bgr(0, 255, 0), 2); imageBox2.Image = imgdraw; rectcurrent = rec1; imgdraw.Dispose(); private void button4_Click(object sender, EventArgs e) if (rectcurrent != null) Image imgsrc = src.Clone(); Image redo = imgsrc.GetSubRect(rectcurrent); redo.Save(e:template.jpg); Capture ct = new Capture(0); private void button5_Click(object sender, EventArgs e) if (ct != null) Application.Idle += new EventHandler(GetFrame); private void GetFrame(object sender, EventArgs e) Image fram = ct.QueryFrame(); imageBox2.Image = fram; src = fram; if (captureflg=true) Image imgsrc = src.Convert().Clone(); Image readimg = tempsrc; Image template = readimg.Convert().Clone(); Image imgcolor = src.Clone(); Image imgresult = imgsrc.MatchTemplate(template, TM_TYPE.CV_TM_CCOEFF).Convert().Clone(); imageBox1.Image = imgresult; double bestvalue; Point bestpoint; FindBestPoint(imgresult, TM_TYPE.CV_TM_CCOEFF, out bestvalue, out bestpoint); Rectangle rec1 = new Rectangle(new Point(bestpoint.X, bestpoint.Y), template.Size); imgcolor.Draw(rec1, new Bgr(0, 0, 255), 2); imageBox1.Image = imgcolor; Thread.Sleep(1); private void button6_Click(object sender, EventArgs e) Application.Idle -= new EventHandler(GetFrame); Image imgsrc = src.Clone(); imageBox2.Image = imgsrc; private void button8_Click(object sender, EventArgs e) if (rectcurrent.Width != 0|rectcurrent .Height !=0) Image imgsrc = src.Clone(); Image redo = imgsrc.GetSubRect(rectcurrent); redo.Save(e:template.jpg); tempsrc = redo; Applica

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论