1、原创-EMGUCV的模板匹配与跟踪完成啦! 兄弟们好! 经过很多天的努力学习,我的EMGUCV的模板匹配完成了,用实际的摄像头取图象,再存为模板后,就能实现物体跟踪,还能进行相机上马达进行位置确定。好玩吧!弄两幅图来看下。 这是匹配的,还能过行坐标二次定位。 坐标二次定位的 这是跟踪的 以下为代码,只有自己写的部分,由软件生成的部分没有帖上 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; us
2、ing 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.VideoSurvei
3、llance;
using Emgu.Util;
using Emgu.Util.TypeEnum;
using Emgu.CV.GPU;
namespace 模板匹配
{
public partial class Form1 : Form
{
public Image
4、}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "(jpg)|*.jpg";
if (of.ShowDialog ()== DialogResult.OK)
{
Image 5、yte>(of.FileName);
imageBox2.Image = imsrc;
src = imsrc;
imageBox1.Image = null;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (src != null)
{
Ima 6、ge 7、ny = imgthread.Canny(130,255);
//Contour 8、SHEY_COMPLEX, 1, 1);
if (cf.Length > 0)
{
cf[0].Radius = cf[0].Radius + 50;
imgsrc.Draw(cf[0], new Bgr(0, 0, 255), 2);
Rectangle rec = new Rectangle((int)(cf[0].Center.X - cf[0].Radius), (int)(cf[0].Cent 9、er.Y - cf[0].Radius), (int)(cf[0].Radius * 2), (int)(cf[0].Radius * 2));
imgsrc.Draw(rec, new Bgr(0, 0, 255), 2);
imgsrc.Draw(cf[0].Center.ToString() + "," + cf[0].Radius.ToString(), ref mf, new Point(0, src.Height - 30), new Bgr(0, 0, 255));
10、 Image 11、 MessageBox.Show(@"模板生成成功!已保存到e:\template.jpg中");
}
else
{
imgsrc.Draw(@"Dn't find out circle!PLS try agina.", ref mf, new Point(0, src.Height - 30), new Bgr(0, 0, 255));
imageBox1.Image = imgcanny;
12、 }
}
}
private void button3_Click(object sender, EventArgs e)
{
Image 13、yte> template =readimg.Convert 14、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);
MCv 15、Font 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 16、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_T 17、YPE.CV_TM_SQDIFF || tmtype == TM_TYPE.CV_TM_SQDIFF_NORMED)
{
bestvalue = min[0];
bestpoint = minl[0];
}
else
{
bestvalue = max[0];
bestpoint = maxl[0];
}
}
18、 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 )
{
19、 startpoint.X = e.X;
startpoint.Y = e.Y;
mousestatus = true;
}
}
private void imageBox2_MouseUp(object sender, MouseEventArgs e)
{
if (mousestatus )
{
endpoint.X = e.X;
20、 endpoint.Y = e.Y;
mousestatus = false;
}
}
private void imageBox2_MouseMove(object sender, MouseEventArgs e)
{
if (mousestatus )
{
endpoint.X = e.X;
endpoint.Y = e.Y 21、
Rectangle rec1 = new Rectangle(startpoint, new Size(Math.Abs(startpoint.X - endpoint.X), Math.Abs(startpoint.Y - endpoint.Y)));
Image 22、mgdraw;
rectcurrent = rec1;
imgdraw.Dispose();
}
}
private void button4_Click(object sender, EventArgs e)
{
if (rectcurrent != null)
{
Image 23、 Image 24、 {
Application.Idle += new EventHandler(GetFrame);
}
}
private void GetFrame(object sender, EventArgs e)
{
Image 25、captureflg==true)
{
Image 26、Clone();
Image 27、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) 28、
}
private void button6_Click(object sender, EventArgs e)
{
Application.Idle -= new EventHandler(GetFrame);
Image 29、nder, EventArgs e)
{
if (rectcurrent.Width != 0|rectcurrent .Height !=0)
{
Image 30、 tempsrc = redo;
}
Application.Idle += new EventHandler(GetFrame);
}
public bool captureflg = false;
private void button7_Click(object sender, EventArgs e)
{
tempsrc = new Image
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818