www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱matlab程序源码 > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I4_01_1_TemplateMatching.m

    clear;clc;close all;imtool close all

%% 僥儞僾儗乕僩儅僢僠儞僌梡偺僆僽僕僃僋僩嶌惉
htm=vision.TemplateMatcher;

%% 夋憸偺庢崬傒丒僌儗乕僗働乕儖壔
I = im2double(imread('board.tif'));
I = I(1:200,1:200,:);      % 夋憸偑戝偒偄偺偱堦晹愗傝弌偡
Igray = rgb2gray(I);
figure;imshow(Igray);

%% 僥儞僾儗乕僩夋憸偺惗惉丒昞帵
T = Igray(20:75,90:135);
figure; imshow(T);

%% 僥儞僾儗乕僩儅僢僠儞僌偺幚峴
Loc = step(htm, Igray, T)            % 僥儞僾儗乕僩拞怱偵懳墳偡傞嵗昗 [x y]

%% 尒偮偐偭偨応強偵儅乕僉儞僌丒寢壥夋憸偺昞帵
J = insertShape(I, 'FilledCircle', [Loc, 10], 'Opacity', 1, 'Color', 'red');
figure; imshow(J); title('Marked target');

%% 廔椆
% Copyright 2014 The MathWorks, Inc.











%% Metric matrix 偺庢摼
release(htm);
htm=vision.TemplateMatcher('OutputValue','Metric matrix', 'OverflowAction','Saturate');    % 夋憸偑uint8偺堊丄撪晹偼屌掕彫悢揰儌乕僪
mat = step(htm,Igray,T);
imtool(mat);

%% 僥儞僾儗乕僩傪揮抲偟丄崌偆傕偺偑側偄応崌
T1 = T';
figure; imshow(T1);
%% 僥儞僾儗乕僩儅僢僠儞僌偺幚峴
release(htm);
htm=vision.TemplateMatcher('BestMatchNeighborhoodOutputPort',true, 'NeighborhoodSize',1);
[Loc val] =step(htm, Igray, T)