EmguCV Image类中的函数(四)使用MatchTemplate函数进行模板匹配

MatchTemplate的函数原型为

Image<Gray, float> MatchTemplate(Image<TColor, TDepth> template, TemplateMatchingType method);

其中TemplateMatchingType 共有6种检测方式
1、平方差匹配 Sqdiff
最好匹配为0.匹配越差,匹配值越大。
2、相关匹配 Ccorr
数值越大表示匹配程度较高,0为最坏的匹配效果。
3相关匹配Ccoeff
1为完美匹配,-1表示最差的匹配,0表示没有任何相关性(随机序列).
4、标准平方差匹配SqdiffNormed
最小值表示最好的匹配
5、标准相关匹配CcorrNormed
最大值为最好匹配
6、标准相关匹配CcoeffNormed
最大值为最好匹配

原理:
通过图像块一次移动一个像素 (从左往右,从上往下)的. 在每一个位置, 都进行一次度量计算来说明图像块和原图像的特定区域的相似度。

用法如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.Util;
using Emgu.CV.UI;

namespace EmguCVHist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Image<Bgr, byte> a = new Image<Bgr, byte ("738b4710b912c8fc453a8235fe039245d7882178.jpg");
            Image<Bgr, byte> b = new Image<Bgr, byte>("QQ截图20150827003943.png");
            Image<Gray, float> c = new Image<Gray, float>(a.Width, a.Height);
            c = a.MatchTemplate(b, TemplateMatchingType.CcorrNormed);
            double min=0, max=0;
            Point maxp = new Point(0, 0);
            Point minp = new Point(0, 0);
            CvInvoke.MinMaxLoc(c, ref min, ref max, ref minp, ref maxp);
            Console.WriteLine(min + " " + max);
            //CvInvoke.Normalize(c, c);
            CvInvoke.Rectangle(a, new Rectangle(maxp, new Size(b.Width, b.Height)), new MCvScalar(0, 0, 255), 3);
            imageBox1.Image = a;
        }
    }
}

效果图:中间为运行图
这里写图片描述

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cccccc1212

这是c币不是人民币,不要充值

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值