模板匹配的工作方式
模板匹配的工作方式跟直方图的反向投影基本一样,大致过程是这样的:通过在输入图像上滑动图像块对实际的图像块和输入图像进行匹配。
假设我们有一张100x100的输入图像,有一张10x10的模板图像,查找的过程是这样的:
(1)从输入图像的左上角(0,0)开始,切割一块(0,0)至(10,10)的临时图像;
(2)用临时图像和模板图像进行对比,对比结果记为c;
(3)对比结果c,就是结果图像(0,0)处的像素值;
(4)切割输入图像从(0,1)至(10,11)的临时图像,对比,并记录到结果图像;
(5)重复(1)~(4)步直到输入图像的右下角。
%做出个界面,并由串联按钮组成系统
clc;
clear;
close;
GUI=figure('color',[0.8 0.8 0.8],'position',[200 200 600 600],'name',...
'数字识别','numbertitle','off');
%图像及结果显示
GUI_axes=axes('position',[0.1 0.55 0.4 0.4]); %坐标轴
xlabel('待识别图像','fontsize',12)
GUI_jieguo=uicontrol(GUI,'style','text','units','normalized','position',[0.15 0.15 0.3 0.3],...
'back',[1 1 1]);
uicontrol(GUI,'style','text','units','normalized','position',[0.22 0.08 0.15 0.05],...
'string','识别结果','back',[0.8 0.8 0.8],'fontsize',12);
%像素值的获取
uibuttongroup('units','normalized','position',[0.65 0.53 0.32 0.43],...
'Title','请选择像素值:','back',[0.8 0.8 0.8],'fontsize',16);
m=0;n=0;
r1=uicontrol(GUI,'style','radio','units','normalized','position',[0.73 0.83 0.16 0.08],...
'string','8×8','value',0,'back',[0.8 0.8 0.8],'fontsize',14,'callback',['m=8;','n=8;',...
'set(r1,''value'',1);','set(r2,''value'',0);','set(r3,''value'',0);','set(r4,''value'',0);','set(r5,''value'',0);']);
r2=uicontrol(GUI,'style','radio','units','normalized','position',[0.73 0.76 0.16 0.08],...
'string','16×16','value',0,'back',[0.8 0.8 0.8],'fontsize',14,'callback',['m=16;','n=16;',...
'set(r1,''value'',0);','set(r2,''value'',1);','set(r3,''value'',0);','set(r4,''value'',0);','set(r5,''value'',0);']);
r3=uicontrol(GUI,'style','radio','units','normalized','position',[0.73 0.69 0.16 0.08],...
'string','32×32','value',0,'back',[0.8 0.8 0.8],'fontsize',14,'callback',['m=32;','n=32;',...
'set(r1,''value'',0);','set(r2,''value'',0);','set(r3,''value'',1);','set(r4,''value'',0);','set(r5,''value'',0);']);
r4=uicontrol(GUI,'style','radio','units','normalized','position',[0.73 0.61 0.16 0.08],...
'string','64×64','value',0,'back',[0.8 0.8 0.8],'fontsize',14,'callback',['m=64;','n=64;',...
'set(r1,''value'',0);','set(r2,''value'',0);','set(r3,''value'',0);','set(r4,''value'',1);','set(r5,''value'',0);']);
r5=uicontrol(GUI,'style','radio','units','normalized','position',[0.73 0.54 0.16 0.08],...
'string','128×128','value',0,'back',[0.8 0.8 0.8],'fontsize',14,'callback',['m=128;','n=128;',...
'set(r1,''value'',0);','set(r2,''value'',0);','set(r3,''value'',0);','set(r4,''value'',0);','set(r5,''value'',1);']);
%按钮
uicontrol(GUI,'style','push','units','normalized','position',[0.7 0.40 0.2 0.08],...
'string','待识别图像','fontsize',14,'callback', 'daishibie');
%uicontrol(GUI,'style','push','units','normalized','position',[0.7 0.3 0.2 0.08],...
% 'string','单模板识别','fontsize',14,'callback', 'main1');
uicontrol(GUI,'style','push','units','normalized','position',[0.7 0.25 0.2 0.08],...
'string','识别','fontsize',14,'callback', 'main');
uicontrol(GUI,'style','push','units','normalized','position',[0.7 0.10 0.2 0.08],...
'string','清除结果','callback','GUI_clear','fontsize',14);
完整代码或者代写添加QQ1575304183