Introduction:
This paper mainly introduces several image matching algorithms based on gray scale: mean absolute difference algorithm (MAD), absolute error and algorithm (SAD), error square sum algorithm (SSD), mean error square sum algorithm (MSD), Normalized product correlation algorithm (NCC), Sequential similarity algorithm (SSDA). The following is explained in turn.
Mad AlgorithmIntroduction
mean absolute difference algorithm ( mean Absolute Differences , referred to as mad algorithm", which is leese A matching algorithm proposed by the in 1971. is a common method in pattern recognition, the algorithm is simple in idea, has high matching precision and less computational amount, and is widely used in image matching.
Set S (x, y) is a search image of the size of MXN , the template image of the MXN atT (x, y) , shown in (a),(b) , respectively, Our aim is to find the area matching ( b) in ( a) (shown in yellow box).
Algorithm Ideas
In the search graph S , take (I,J) as the upper left corner, theMxN size of the sub-graph, calculate its similarity with the template diagram; In all the sub-graphs that can be taken, find the sub-diagram most similar to the template diagram as the final result. The similarity measure formula of the MAD algorithm is as follows. Obviously, the smaller the average absolute difference D (i,j) , the more similar, so just find the smallest D (i,j) to determine the sub-plot position:
which
Algorithm Evaluation:Advantages:
① idea is simple, easy to understand (sub-map and template map corresponding position, gray value of the absolute sum of the difference, and then the average, the essence: is the sub-graph and template diagram of the L1 distance average).
② operation process is simple and the matching precision is high.
Disadvantages:
① computation is too large.
The ② is very sensitive to noise.
——————————————————————————————————————————————————————————————————————————————
Sad algorithmIntroduction
Absolute error and algorithm (Sum of Absolute Differences, short for the SAD algorithm). In fact, theSAD algorithm is almost identical to the MAD algorithm idea, but its similarity measurement formula has a little change (the L1 distance between the sub-graph and the template graph is computed). Don't repeat it here.
Algorithm Implementation
Because the article introduces a few algorithms very similar, so this article only lists the SAD algorithm code, the rest of the algorithm implementation is the same.
matlab code
%%% Absolute Error and algorithm (SAD) Clear all;close all;%%src=imread (' lena.jpg '); [A B d]=size (SRC); if d==3 src=rgb2gray (SRC); Endmask=imread (' lena_mask.jpg '); [ M n D]=size (mask), if d==3 mask=rgb2gray (mask), end%%n=n;% template size, default template is square m=a;% generation search image size, default search image is square%%dst=zeros (m-n, M-N); for i=1:m-n % line for j=1:m-n temp=src (i:i+n-1,j:j+n-1); DST (i,j) =DST (i,j) +sum (SUM (ABS (Temp-mask))); Endendabs_min=min (min (DST)); [X,y]=find (Dst==abs_min); Figure;imshow (mask); title (' template '); figure;imshow (SRC); hold On;rectangle (' position ', [x, Y, N-1,n-1], ' Edgecolor ', ' R '), hold Off;title (' Search graph ');
Output Results
——————————————————————————————————————————————————————————————————————————————
SSD Algorithm
Error squared sum algorithm (Sum of Squared Differences, abbreviated SSD algorithm), also called Chaffang and algorithm. In fact, theSSD algorithm is similar to the SAD algorithm, except that the similarity measurement formula has a little change (the L2 distance between the sub-graph and the template graph is computed). Don't repeat it here.
——————————————————————————————————————————————————————————————————————————————
MSD Algorithm
The mean squared error sum algorithm (Mean square Differences, or the MSD algorithm), also known as the mean variance algorithm. In fact , the SSDis equivalent to MAD SAD, so I will not repeat it.
————————————————————————————————————————————————————————————————————————————————
NCC Algorithm
Normalized product correlation algorithm (normalized crossCorrelation, called NCC algorithm), similar to the above algorithm, is still the use of sub-graph and template map of the grayscale, Calculates the degree of matching between the two by normalized correlation measure formula.
Among them,, respectively, (I,J) , the average gray value of the template.
OK, the above is a few common gray-based template matching algorithm.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Template matching algorithm based on gray level (a): MAD, SAD, SSD, MSD, NCC, SSDA algorithm