using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
namespace xscreen
{
public partial class FormTest : Form
{
public FormTest()
{
InitializeComponent();
}
//截取全屏图象
private void xscreem_Click(object sender, EventArgs e)
{
//创建图象,保存将来截取的图象
Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> img= new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>("E:\\xscreen\\cp.png");
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域 柯乐义
imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte> GrayImage = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte>(image);
// System.Drawing.Image img = image;
// pictureBox1.Image = img;
imageBox1.Image = GrayImage;
//保存
// SaveImage(image);
}
//保存图象文件
private void SaveImage(Image image)
{
//if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
//{
// string fileName = saveFileDialog.FileName;
// string extension = Path.GetExtension(fileName);
// if (extension == ".jpg")
// {
// image.Save(fileName, ImageFormat.Jpeg);
// }
// else
// {
// image.Save(fileName, ImageFormat.Bmp);
// }
//}
}
private void comparepic_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("E:\\xscreen\\cp.png");
}
private void 获取位置_Click(object sender, EventArgs e)
{
GetPos();
}
public void GetPos()
{
Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> img = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>("E:\\xscreen\\cp.png");
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域 柯乐义
imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte> OriImage = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte>(image);
Image<Bgr, byte> a = OriImage;// new Image<Bgr, byte>("rest.png");
Image<Bgr, byte> b = img;// new Image<Bgr, byte>(AppDomain.CurrentDomain.BaseDirectory + "rest.png"); //("chepai.png");
//输入图像
Image<Bgr, Byte> imageInput = a;
//模板图像
Image<Bgr, Byte> imageTemplate = b;
//匹配方式数组
// TM_TYPE tmType = TM_TYPE.CV_TM_CCOEFF_NORMED;
TM_TYPE tmType = TM_TYPE.CV_TM_SQDIFF_NORMED;
//输出图像(匹配结果)
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 , bestPoint.Y ), imageTemplate.Size);
imageResult.Draw(rect, new Gray(bestValue), 2);
a.Draw(rect, new Bgr(Color.Red), 2);
// CvInvoke.cvShowImage("原始图像", a);
// CvInvoke.cvShowImage("特征模板", b);
imageBox1.Image = a;
// imageBox1.Image = imageResult;
}
public 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];
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.OCR;
using Emgu.CV.Features2D;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
namespace xscreen
{
public partial class FormTest : Form
{
public FormTest()
{
InitializeComponent();
}
//截取全屏图象
private void xscreem_Click(object sender, EventArgs e)
{
//创建图象,保存将来截取的图象
Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> img= new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>("E:\\xscreen\\cp.png");
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域 柯乐义
imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte> GrayImage = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte>(image);
// System.Drawing.Image img = image;
// pictureBox1.Image = img;
imageBox1.Image = GrayImage;
//保存
// SaveImage(image);
}
//保存图象文件
private void SaveImage(Image image)
{
//if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
//{
// string fileName = saveFileDialog.FileName;
// string extension = Path.GetExtension(fileName);
// if (extension == ".jpg")
// {
// image.Save(fileName, ImageFormat.Jpeg);
// }
// else
// {
// image.Save(fileName, ImageFormat.Bmp);
// }
//}
}
private void comparepic_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile("E:\\xscreen\\cp.png");
}
private void 获取位置_Click(object sender, EventArgs e)
{
GetPos();
}
public void GetPos()
{
Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte> img = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, byte>("E:\\xscreen\\cp.png");
Bitmap image = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);
//设置截屏区域 柯乐义
imgGraphics.CopyFromScreen(0, 0, 0, 0, new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte> OriImage = new Emgu.CV.Image<Emgu.CV.Structure.Bgr, Byte>(image);
Image<Bgr, byte> a = OriImage;// new Image<Bgr, byte>("rest.png");
Image<Bgr, byte> b = img;// new Image<Bgr, byte>(AppDomain.CurrentDomain.BaseDirectory + "rest.png"); //("chepai.png");
//输入图像
Image<Bgr, Byte> imageInput = a;
//模板图像
Image<Bgr, Byte> imageTemplate = b;
//匹配方式数组
// TM_TYPE tmType = TM_TYPE.CV_TM_CCOEFF_NORMED;
TM_TYPE tmType = TM_TYPE.CV_TM_SQDIFF_NORMED;
//输出图像(匹配结果)
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 , bestPoint.Y ), imageTemplate.Size);
imageResult.Draw(rect, new Gray(bestValue), 2);
a.Draw(rect, new Bgr(Color.Red), 2);
// CvInvoke.cvShowImage("原始图像", a);
// CvInvoke.cvShowImage("特征模板", b);
imageBox1.Image = a;
// imageBox1.Image = imageResult;
}
public 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];
}
}
}
}