一、模闆比對算法簡介
1 概述
模式識别就是通過計算機,用數學模型求解的方法研究模式的自動處理和判讀。在模式識别的各種方法中,模闆比對是最容易的一種,其數學模型易于建立,通過模闆比對對數字圖像模式識别有助于我們了解數學模型在數字圖像中的應用。
2 模闆比對算法
2.1 相似性測度求比對
模闆比對的實際操作思路很簡單:拿已知的模闆,和原圖像中同樣大小的一塊區域去對。最開始時,模闆的左上角點和圖像的左上角點是重合的,拿模闆和原圖像中同樣大小的一塊區域去對比,然後平移到下一個像素,仍然進行同樣的操作, ……所有的位置都對完後,差别最小的那塊就是我們要找的物體。
以上所描述的是相似性測度法求比對的求解思路,其在計算機中操作的如圖2所示。設模闆T疊放在搜尋圖上平移,被模闆覆寫搜尋圖下的那個圖像叫做子圖Si , j,i , j 為這塊子圖的左上角像素點在S圖的坐标,稱為參考點,從圖2可知,i , j 的取值範圍是:1<i ,j <N- M+1. 現在可以比較T和Si , j的内容。若兩者一緻,則T和S之差為零. 是以,可用下列公式(1) 和公式(2) 來衡量T和Si , j的相似程度。
在(2) 式中第3項表示模闆總能量,是一個與(i , j) 無關的常數;第1項是模闆覆寫下子圖的能量,它随着(i , j) 的位置緩慢地改變;第2項表示的子圖與模闆的互相關系,随着(i , j) 的改變而改變,當T和Si , j比對時這項取值最大。是以可用下列相關函數(3) 作相似性測度。
當矢量t 和S1之間的夾角為0時,即當S1(i , j) =kt 時(k為常量) ,有R(i , j) =1,否則R(i , j) <1. 顯然R(i , j) 越大,模闆T和Si , j就越相似,點(i , j) 就是我們要尋找的比對點。
2.2 序貫相似性檢測的算法
用相關法求比對的計算量很大,因為模闆要在(N- M+1)2個參考位置上作相關計算,除了在比對點外,其它點作的都是無用功。是以,人們提出一種叫序貫相似性檢測的算法,簡稱SSDA(Sequential SimiliarityDetectionAlgorithm) 其要點是:
在數字圖像中,SSDA法用公式(6) 計算圖像f ( x, y) 在點(i , j) 的非相似度m(i , j) 作為比對尺度。式中(i , j) 表示的不是模闆中心坐标,而是它左上角坐标。模闆的大小為n ×m。
如果在(i , j) 處圖像中有和模闆一緻的圖案時,則m(i , j) 值很小,反之則很大。特别是模闆和搜尋圖下的子圖部分,完全不一緻的場合下,如果在模闆内的各像素與圖像重合部分對應的像素灰階差的絕對值依次增加,其和會急劇增大。 是以,在作加法時,如果灰階差的絕對值部分和超過某一門檻值時,就認為這個位置不存在和模闆一緻的圖案,進而轉移到下一個位置上計算m(i , j)。并且在這模闆下的各像素點計算中止,是以能大幅度地縮短計算時間,提高比對速度。
根據上述思路,我們可以進一步改進SSDA算法。就是把在圖像上的模闆移動分為粗檢索和細檢索2個階段進行。首先進行粗檢索,它不是讓模闆每次移動1個像素,而是每隔若幹的像素把模闆和圖像重疊,并且計算比對的尺度,進而求出待尋找的圖案大緻存在的範圍。然後在這個範圍内,讓模闆每隔1個像素移動1次,根據比對的尺度确定尋找圖案的所在位置。這樣,整體上計算模闆比對的次數減少了,計算時間縮短,比對的速度提高了。但用這種方法具有漏掉圖像中最恰當位置的危險性。
2.3 相關算法
2個函數的相關性定義,可用公式(7)表示:
f*表示f 的複共轭。我們知道相關理論類似卷積理論,F( u, v) 和 H( u, v) 分别表示f ( x, y) 和h( x, y)的傅立葉變換. 根據卷積理論有
可知卷積是空間域過濾和頻率域過濾之間的紐帶。相關的重要用途在于比對。在比對中,f ( x, y) 是一幅包含物體或區域的圖像。如果想要确定f 是否包含有感興趣的物體或區域,讓h( x, y) 作為那個物體的區域(通常稱該圖像為模闆)。如果比對成功,2個函數的相關值會在h找到f 中相應點的位置上達到最大。從上面分析可知,相關算法可以有2種方法:可在空間域進行,也可在頻率域進行。
2.4 幅度排序相關算法
這種算法有2個步驟組成:
第1步,把實時圖中的各個灰階值按幅度的大小排成列的形式,然後在對它進行二進制(或三進制) 編碼,根據二進制排序的序列,把實時圖變換為二進制陣列的一個有序的集合{ Cn, n =1,2, …, N}。這一過程稱之為幅度排序預處理。
第2步,序貫地将這些二進制序列與基準圖進行由粗到細的相關,直到确定出比對點為止。由于篇幅的限制,這裡就不列出例子了。
2.5 分層搜尋的序慣判決算法
這種分層搜尋算法是直接基于人們先粗後細尋找事物的慣例而形成的,例如,在中國地圖上找肇慶的位置時,可以先找廣東省這個地域,這過程稱為粗相關。然後在這個地域中,再仔細确定肇慶的位置,這叫做細相關。很明顯,利用這種方法,可以很快找出肇慶的位置。因為在這過程中省略了尋找廣東省以外區域所需的時間,這種方法稱為分層搜尋的序貫判決法,利用這種思想形成的分層算法具有相當高的搜尋速度。限于篇幅,這裡隻給出這種操作的一般思路。
3 總結
模式比對本質就是應用數學。模闆比對過程如下:①将圖像數字化,按順序取出每個點的像素值;②代入事先建立的數學模型進行預處理;③選擇一種合适的算法進行模式比對;④将比對後圖像的坐标列出或直接在原圖顯示.。在模闆比對操作中最為關鍵的是如何建立數學模型,這是正确比對的核心。
二、部分源代碼
function varargout = trsUI(varargin)
% TRSUI M-file for trsUI.fig
% TRSUI, by itself, creates a new TRSUI or raises the existing
% singleton*.
%
% H = TRSUI returns the handle to a new TRSUI or the handle to
% the existing singleton*.
%
% TRSUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TRSUI.M with the given input arguments.
%
% TRSUI('Property','Value',...) creates a new TRSUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before trsUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to trsUI_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 trsUI
% Last Modified by GUIDE v2.5 19-Feb-2021 15:53:44
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @trsUI_OpeningFcn, ...
'gui_OutputFcn', @trsUI_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 trsUI is made visible.
function trsUI_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 trsUI (see VARARGIN)
% Choose default command line output for trsUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes trsUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = trsUI_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;
global I1;
global mb1;
global mb2;
global x;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%m_file_open (hObject, eventdata, handles);
[FileName,PathName ]= uigetfile({'*.bmp';'*.png';'*.jpg';'*.*'},'Select an image');
axes(handles.axes1);%用axes指令設定目前操作的坐标軸是axes_src
fpath=[PathName FileName];%将檔案名和目錄名組合成一個完整的路徑
%I1 = imread(FileName);
global I1;
I1 = imread(FileName);
imshow(I1);
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global I;
global mb1;
global mb2;
I=imread('禁止停車.bmp');
mb1=tuxiangchuli(I);
I=imread('禁止駛入.bmp');
mb2=tuxiangchuli(I);
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I1;
global x;
x=tuxiangshibie(I1);
set(handles.xData,'string',num2str(x));
guidata(hObject, handles);
drawnow;
%function zuavg = tuxiangchuli(I)
I=imread('禁止停車.bmp');
%I=imadjust(I,[0.3,0.8],[0,1],0.5);
%imshow(I);
%I1=rgb2hsi(I);%figure,imshow(I1);
%I2=histeq(I(:,:,3));% figure,imshow(I2);
%I1(:,:,3)=double(I2)/255; %figure,imshow(I1);
%I3=hsi2rgb(I1);I4= im2uint8(mat2gray(I3));%figure,imshow(I4);
I5=((I(:,:,1)-I(:,:,2)>30)&(I(:,:,1)-I(:,:,3)>30));%|(I(:,:,1)<30&I(:,:,2)<30&I(:,:,3)<30);
%figure,imshow(I5);
I6=medfilt2(I5);%figure,imshow(I6);
[I7,num]=bwlabel(I6,8);%figure,imshow(I7);
I8=bwareaopen(I7,500);%figure,imshow(I8);
[I9,num]=bwlabel(I8,8);%figure,imshow(I9);
a=regionprops(I9,'area');
area=cat(1,a.Area);
p=regionprops(I9,'perimeter');
perimeter=cat(1,p.Perimeter);
C=4*pi*(area)./(perimeter.*perimeter);
I10=ismember(I9,(find(C)>0.5));%figure,imshow(I10);
se=strel('square',4);
I11=imerode(I10,se);%figure,imshow(I12);
se=strel('square',3);
I12=imdilate(I11,se);%figure,imshow(I11);
I13=I12==0;%figure,imshow(I13);
I14=bwareaopen(I13,50);%figure,imshow(I14);
I15=I14==0;%figure,imshow(I15);
I16=mat2gray(I15);%figure,imshow(I16);
I17=bwperim(I16,8);%figure,imshow(I17);
%h=fspecial('gaussian',5); I18=edge(I17,'zerocross',[],h);figure,imshow(I18);
I18=zs(I17);%figure,imshow(I18);
c=0;
[m,n]=size(I18);
for i=1:1:m
for j=1:1:n
if I18(i,j)==1
c=i;
break;
end
end
if c>0
break
end
end
for i=c:1:m
for j=1:1:n
if I18(i,j)==1
e=i;
break;
end
end
if e>0
e=0;
d=i;
end
end
c1=0;
for j=1:1:n
for i=1:1:m
if I18(i,j)==1
c1=j;
break;
end
end
if c1>0
break
end
end
for j=c1:1:n
for i=1:1:m
if I18(i,j)==1
e2=j;
break;
end
end
if e2>0
e2=0;
d1=j;
end
end