图片C#基于模板获取位置

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];
            }
        }
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值