Introduction to the

One of the most basic methods in pattern recognition is template matching, which is basically a statistical recognition method. In order to detect the object with known shape in the image, we use the shape template (or window) of the object to match with the image, and detect the object image under a certain agreed criterion, which is usually called template matching method. It can detect lines, curves, patterns and so on in an image. Its applications include: matching target template with reconnaissance image; Word recognition and speech recognition.

The principle of

The following formula is used to measure the relationship between template T (m,n) and the covered subgraph Sij (I,j). The original image S(W,H) is known, as shown in the figure:

Use the following formula to measure their similarity:

In the above formula, the first term is the energy of the subgraph and the third term is the energy of the template, which has nothing to do with template matching. The second term is the interdependence of template and subgraph, which varies with (I,j). When the template and subgraph match, the maximum value is used. After normalization, the correlation coefficient of template matching can be obtained:

When template and subgraph are exactly the same, correlation coefficient R(I,j) = 1. After all searches are completed in the searched graph S, the maximum value Rmax(im,jm) of R is found, and its corresponding subgraph Simjm is the bit-matching target. Obviously, it is too much and slow to use this formula to do image matching. We can use another algorithm to measure the error of T and Sij, and the formula is as follows:

The calculation speed can be increased by calculating the vector errors of the two images. An error threshold E0 is selected according to different matching directions. When E(I,j)>E0, the calculation at this point is stopped and the calculation at the next point is continued.

The final experiment proves that the larger the searched image is, the slower the matching speed is. The smaller the template, the faster the matching speed; The size of the threshold has great influence on the matching speed.

 

Improved template matching algorithm

Change the template matching process from one to two;

The first match is a rough match. The interlaced and column data of the template, i.e., 1/4 of the template data, are matched on the searched soil, i.e., within 1/4 of the original image. Because the amount of data is greatly reduced, the matching speed is significantly improved. At the same time, a reasonable error threshold E0 should be designed:

E0 = e0 * (m + 1) / 2 * (n + 1) / 2

Where, e0 is the average maximum error of each point, generally 40~50;

M,n are the length and width of the template;

The second match is an exact match. In the neighborhood of the minimum error point of the first time (imin, jmin), that is, in the rectangle with diagonal points (Imin-1, jmin-1), (Imin + 1, jmin + 1), search matching is carried out to obtain the final result.

The flow chart

The key problem of the algorithm is to match and find the minimum distance. The solution is to calculate the distance with the samples of the training set one by one, and finally find out the most adjacent samples to get the category number.

function varargout = face(varargin) % FACE MATLAB code for face.fig % FACE, by itself, creates a new FACE or raises the existing % singleton*. % % H = FACE returns the handle to a new FACE or the handle to %  the existing singleton*. % % FACE('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FACE.M with the given input arguments. % % FACE('Property','Value',...) creates a new FACE or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before face_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to face_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 face % Last Modified by GUIDE V2.5 18-Dec-2014 12:02:18 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @face_OpeningFcn, ... 'gui_OutputFcn', @face_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 face is made visible. function face_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 face (see VARARGIN) % Choose default command line output for face handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes face wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = face_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 pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % 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) % read image to be recognize global im; [filename, pathname] = uigetfile({'*.bmp'},'choose photo'); str = [pathname, filename]; im = imread(str); axes( handles.axes1); imshow(im); % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % 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) global im global reference global W global imgmean global Col_of_data global pathname global img_path_list % im = double(im(:)); objectone = W'*(im - imgmean); distance = 100000000; For k = 1:col_of_data temp = norm(objectone-reference (:,k)); if(distance>temp) aimone = k; distance = temp; aimpath = strcat(pathname, '/', img_path_list(aimone).name); Axes (handles. Axes2) imshow(aimpath) end end % AImpath = strcat(pathName, '/', img_path_list(aimone).name); % axes( handles.axes2 ) % imshow(aimpath)Copy the code

 

References and codes for private message bloggers