本文主要介绍几种基于灰度的图像匹配算法:平均绝对差算法(MAD)、绝对误差和算法(SAD)、误差平方和算法(SSD)、平均误差平方和算法(MSD)、归一化积相关算法(NCC)、序贯相似性检测算法(SSDA)、hadamard变换算法(SATD)。下面依次对其进行讲解。
MAD算法介绍
平均绝对差算法(Mean Absolute Differences,简称MAD算法),它是Leese在1971年提出的一种匹配算法。是模式识别中常用方法,该算法的思想简单,具有较高的匹配精度,广泛用于图像匹配。
设S(x,y)是大小为mxn的搜索图像,T(x,y)是MxN的模板图像,分别如下图(a)、(b)所示,我们的目的是:在(a)中找到与(b)匹配的区域(黄框所示)。
算法思路
在搜索图S中,以(i,j)为左上角,取MxN大小的子图,计算其与模板的相似度;遍历整个搜索图,在所有能够取到的子图中,找到与模板图最相似的子图作为最终匹配结果。
MAD算法的相似性测度公式如下。显然,平均绝对差D(i,j)越小,表明越相似,故只需找到最小的D(i,j)即可确定能匹配的子图位置:
其中:
算法评价:
优点:
①思路简单,容易理解(子图与模板图对应位置上,灰度值之差的绝对值总和,再求平均,实质:是计算的是子图与模板图的L1距离的平均值)。
②运算过程简单,匹配精度高。
缺点:
①运算量偏大。
②对噪声非常敏感。
——————————————————————————————————————————————————————————————————————————————
SAD算法介绍
绝对误差和算法(Sum of Absolute Differences,简称SAD算法)。实际上,SAD算法与MAD算法思想几乎是完全一致,只是其相似度测量公式有一点改动(计算的是子图与模板图的L1距离),这里不再赘述。
function varargout = homework2(varargin)
% HOMEWORK2 M-file for homework2.fig
% HOMEWORK2, by itself, creates a new HOMEWORK2 or raises the existing
% singleton*.
%
% H = HOMEWORK2 returns the handle to a new HOMEWORK2 or the handle to
% the existing singleton*.
%
% HOMEWORK2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HOMEWORK2.M with the given input arguments.
%
% HOMEWORK2('Property','Value',...) creates a new HOMEWORK2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before homework2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to homework2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help homework2
% Last Modified by GUIDE v2.5 20-May-2013 21:21:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @homework2_OpeningFcn, ...
'gui_OutputFcn', @homework2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before homework2 is made visible.
function homework2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to homework2 (see VARARGIN)
% Choose default command line output for homework2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes homework2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = homework2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
%载入原始身份证图像的回调函数
% --- Executes on button press in OriginalImg.
function OriginalImg_Callback(hObject, eventdata, handles)
% hObject handle to OriginalImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName] = uigetfile('*.jpg','Select an image');
if PathName~=0
str = [PathName,FileName];
T=imread(str);
axes(handles.Img);
imshow(T);
end
%图像自动亮度调整的回调函数
% --- Executes on button press in autoLight.
function autoLight_Callback(hObject, eventdata, handles)
% hObject handle to autoLight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.Img);
T=getimage;
low_out=0.2; high_out=0.9;
gamma=1.518;
hsv=rgb2hsv(T);
I=hsv(:,:,3);
minL=min(min(I));
maxL=max(max(I));
J=imadjust(I,[minL;maxL],[low_out;high_out],gamma);
hsv(:,:,3)=J;
rgb_atuoI=hsv2rgb(hsv);
axes(handles.Light);
imshow(rgb_atuoI);
%图像二值化的回调函数
% --- Executes on button press in DIP.
function DIP_Callback(hObject, eventdata, handles)
% hObject handle to DIP (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.Img);
I=getimage;
[m,n,r]=size(I);%图像的像素为width*height
%%%%%蓝色字体变黑
myI=double(I);
for i=1:m
for j=1:n
if((myI(i,j,1)>=15)&&(myI(i,j,1)<=130)&&((myI(i,j,2)<=165)&&(myI(i,j,2)>=90))&&((myI(i,j,3)<=220)&&(myI(i,j,3)>=135))) % 蓝色RGB的灰度范围
I(i,j,1)=40; %红色分量
I(i,j,2)=40; %绿色分量
I(i,j,3)=40; %蓝色分量
end
end
end
%figure, imshow(I);title('变色后的图像');
width=round(0.9*n);height=round(0.87*m);
rx=round(0.05*n);cy=round(0.075*m);
I=subim(I,height,width,rx,cy);
%figure,imshow(I);
if sum(size(I)>0)==3 %倘若是彩色图--2维*3,先转换成灰度图
I=rgb2gray(I);
end
%figure,imhist(I);
x=3;%行数分为x部分
y=1;%列数分为y部分
BW=erzhihua(I,x,y);
[n m l]=size(BW);%图像的像素为m*n
c = [0.65*m 0.65*m m m];
r = [0 0.85*n 0.85*n 0];
BW = roifill(BW,c,r);
BW=imadjust(BW);%使用imadjust函数对图像进行增强对比度
% Convert to BW
threshold = graythresh(BW);
BW =~im2bw(BW,0.6*threshold);
[image_h image_w]=size(BW);
% Remove all object containing fewer than (imagen/80) pixels
BW = bwareaopen(BW,floor(image_w/80));
% 滤波
%h=fspecial('average',1);
%BW=im2bw(round(filter2(h,BW)));
%imwrite(d,'4.均值滤波后.jpg');
axes(handles.Binary);
imshow(BW);
%图像分割与识别按钮的回调函数
% --- Executes on button press in OCR.
function OCR_Callback(hObject, eventdata, handles)
% hObject handle to OCR (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.Binary);
imagen = getimage;
[image_h image_w]=size(imagen);
%figure;imshow(imagen);title('INPUT IMAGE')
% Convert to gray scale
if size(imagen,3)==3 %RGB image
imagen=rgb2gray(imagen);
end
%Storage matrix word from image
word=[ ];
re=imagen;
%Opens text.txt as file for write
fid = fopen('ID_card.txt', 'wt');
% Load templates
load templates
global templates
% Compute the number of letters in template file
num_letras=size(templates,2);
figure;
plot_flag=1;
while 1
%Fcn 'lines' separate lines in text
[fl re]=lines(re);
imgn=fl;
[line_h line_w]=size(fl);%记录下切割出来的一行字符的长宽
%Uncomment line below to see lines one by one
% imshow(fl);pause(1)
%-----------------------------------------------------------------
% Label and count connected components
[L Ne] = bwlabel(imgn);
n=1;%记录循环次数
while(n<=Ne)
char_flag=0;%为0时,是第一次判断这个连通域
flag=1;%初始化两个连通域属于同个字符
while(flag==1)
if char_flag==0
[r,c] = find(L==n);
Width0=max(r)-min(r);%连通域宽度
Height0=max(c)-min(c);%连通域高度
Radio0=Width0/Height0;%连通域宽高比
Square0=Width0*Height0;%连通域面积
maxr=max(r);
maxc=max(c);
minr=min(r);
minc=min(c);
end
if n<Ne
[r1,c1] = find(L==(n+1));%寻找下一个连通域
Width1=max(r)-min(r);%连通域宽度
Height1=max(c)-min(c);%连通域高度
Radio1=Width1/Height1;%连通域宽高比
Square1=Width1*Height1;%连通域面积
Uheight=max(maxc,max(c1))-min(minc,min(c1));%合并后高度
Uwidth=max(maxr,max(r1))-min(minr,min(r1));%合并后宽度
Uradio=Uwidth/Uheight;%合并后的宽高比
Oheigth=Height0+Height1-Uheight;%重叠高度
Owidth=Width0+Width1-Uwidth;%重叠宽度
Osquare=Oheigth*Owidth;%重叠面积
else
flag=0;%这是这一行最后一个连通域
end
ph=5;%边界因子
pw=7;
if(flag==1)&&((Owidth>=-(image_w/pw)&&Owidth<=0)||(Oheigth>=-(line_h*0.3)&&Oheigth<=0))%两个连通域较近,但不重叠
if((Uradio>=0.8)&&(Uradio<=1.2))%认为两个连通域属于同一个字符
elseif Uheight<line_h*0.4;%连通域的合并之后高度过小的,认为是一个字符的一部分,很可能是边旁部首
else flag=0;%否则这两个连通域属于不同字符
end
elseif(flag==1)&&(Owidth<-(image_w/pw))%两个连通域里相距较远
flag=0;%两个连通域属于不同字符
% elseif(flag==1)&&((Owidth>0)||(Oheigth>0))%两连通域重叠
elseif(flag==1)&&((Owidth>0))%两连通域重叠
if(((Uradio>=0.78)&&(Uradio<=1.3)))%认为两个连通域属于同一个字符
elseif(Osquare>=0.4*min(Square0,Square1)&&(Uwidth<image_w/45))
else
flag=0;%两个连通域属于不同字符
end
else flag=0;%两个连通域属于不同字符
end
if flag==1%经过上面判断,两个连通域属于同一个字符,进行连通域合并
Width0=Uwidth;%连通域宽度
Height0=Uheight;%连通域高度
Radio0=Width0/Height0;%连通域宽高比
Square0=Width0*Height0;%连通域面积
maxr=max(maxr,max(r1));
maxc=max(maxc,max(c1));
minr=min(minr,min(r1));
minc=min(minc,min(c1));
n=n+1;%指向下一个连通域
char_flag=1;
end
end %while(flag==1)的end
% Extract letter
n1=imgn(minr:maxr,minc:maxc);
% Resize letter (same size of template)
img_r=imresize(n1,[36 23]);
subplot(10,10,plot_flag),imshow(img_r);title(plot_flag);
plot_flag=plot_flag+1;
%Uncomment line below to see letters one by one
% imshow(img_r);title(n);pause(0.5)
%-------------------------------------------------------------------
% Call fcn to convert image to text
letter=read_letter(img_r,num_letras);
% Letter concatenation
word=[word letter];
n=n+1;
end % while(n<=Ne)的end
%fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)
% Clear 'word' variable
word=[ ];
%*When the sentences finish, breaks the loop
if isempty(re) %See variable 're' in Fcn 'lines'
break
end
end
fclose(fid);
%Open 'ID_card.txt' file
winopen('ID_card.txt')
% --- Executes on button press in Exit.
function Exit_Callback(hObject, eventdata, handles)
% hObject handle to Exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
close all;
close(gcf);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.