In the target detection process, a common method is to set a template to traverse the entire source image (the image to be detected) in the form of a sliding window ); each slide generates an ROI image of the same size as the template. Based on a certain measurement method, the similarity measurement value between the template and the current ROI image is calculated. After traversing the complete image, an image is formed to find the location (x, y) corresponding to the maximum value, which is the position of the target we are looking.
The above is a rough step of template matching. When the source image is large and it takes a lot of time to traverse the complete image, a pyramid-based template matching method is proposed. The following describes the steps involved in this method:
(1) set the number of pyramid layers nlevels to create the nlevels pyramid image corresponding to the source image and template image;
(2) When downsampling (divided by 2) is involved in creating a pyramid image at each layer, the downsampling will produce a sawtooth image, and the smoothing filter must be used for processing. The Gaussian smoothing filter has good performance but is time-consuming, the Mean Filter of small templates can be used directly;
(3) When calculating the similarity between the template and the ROI image, select the similarity measurement criterion. The similarity measurement criteria include sad (sum of absolute values) and SSD (sum of squared differences) compared with NCC (normalization correlation coefficient), NCC is the most time-consuming but best-performing computing, and can adapt to illumination changes.
After understanding the general process and related operations, you can directly write code. The pyramid matching algorithm is as follows:
1 function [R, C, nccimg] = pyramidmatch (IMG, template, nlevels) 2% percentile 3% using the pyramid Matching Algorithm for template matching 4% 5% processes: (1) create a nlevels-layer pyramid image for the images to be matched and template images 6% (2) Perform a match from the top of the pyramid, and perform a full scan match at the top of the pyramid, the obtained optimal matching position is 7% and then passed to the lower layer (multiplied by 2); scan and match within the 5*5 window range based on the passed position, so that 8% is done until the lowest layer. 9% (3) the similarity measurement adopted during matching is normalized correlation coefficient 10% (4) downsampling was performed when the pyramid was created (except 2 ), then we processed 11% 12% IMG-source image (assumed as grayscale image) 13% template-template image (assumed as grayscale image) with a 2*2 smoothing filter) 14% nlevels -- pyramid layers 15% @ r, c -- the most matched position in the source image 16% @ nccimg -- the normalized correlation coefficient image corresponding to each layer for Pyramid matching 17% 18% Author: l. l. he 19% time: 26/7/2014 20% % 1_21 imshow (IMG); 22 hold on; 23 [t_r, T_c] = size (Template); 24 NC Cimg = cell (nlevels, 1); 25% calculate the pyramid of the image to be matched and the template 26 nstep = 2; % 5*5 27 srcprad = pyramid (IMG, nlevels ); 28 temprad = pyramid (template, nlevels); 29 [R, C, nccimg {nlevels}] = matchtemplate (srcprad {nlevels}, temprad {nlevels }); 30 for I = nLevels-1:-31 r_start = 2 * r-floor (SIZE (temprad {I}, 1)-1)/2)-nstep; 32 r_end = r_start + 2 * nstep + 1; 33 c_start = 2 * C-floor (SIZE (temprad {I}, 2)-1)/2)-nstep; 34 C_end = c_start ++ 2 * nstep + 1; 35 [R, C, nccimg {I}] = matchtemplate (srcprad {I}, temprad {I },... 36 r_start, r_end, c_start, c_end); 37 end 38 c_r = round (t_r/2); 39 C_c = round (T_C/2); 40 rectangle ('position ', [c-c_c + 1, r-c_r + 1, T_c, t_r], 'edgecolor', 'R '); 41 end 42% limit 43 44% ======================================== ====================================== ===================== 45% template matching algorithm 46 function [objr, objc, ncc_img] = matchtemplate (IMG, template, r_start ,... 47 r_end, c_start, c_end) 48 [src_r, src_c] = size (IMG); 49 [t_r, T_c] = size (Template ); 50 if nargin = 2 51 r_start = 1; 52 r_end = src_r-t_r + 1; 53 c_start = 1; 54 c_end = src_c-t_c + 1; 55 end 56% here first calculate the template image's normalized image 57 norm_img = normalize (Template); 58 ncc_img = zeros (r_end-r_start + 1, c_end-c_s Tart + 1); 59 c_r = round (t_r/2); 60 C_c = round (T_C/2); 61 for R = r_start: r_end 62 for C = c_start: c_end 63 currpatch = IMG (r: R + t_r-1, C: C + t_c-1); 64 currpatch = normalize (currpatch); 65 ncc_img (R + c_r-1, C + c_c-1) = NCC (norm_img, currpatch); 66 end 67 end 68 [val_1, POS] = max (ncc_img); 69 [val_2, objc] = max (val_1 ); 70 objr = pos (find (val_1 = val_2 )); 71 end 72% ============================================ === ===================================================73 74% ====== ========================================================== ============================================ 75% calculate the normalization correlation coefficient of the two images (one similarity measurement ), the larger the absolute value, the more similar 76 function NCC = NCC (img_1, img_2, isnorm) 77 If ~ Exist ('isnorm', 'var') 78 isnorm = 1; 79 end 80% judge whether the parameter is a normalized image 81 If ~ Isnorm 82 img_1 = normalize (img_1); 83 img_2 = normalize (img_2); 84 end 85 NCC = sum (img_1. * img_2 )). /size (img_1 (:), 1 ); 86 end 87% ============================================ ===========================================================88 89% = ========================================================== ===================================== 90% image normalization 91 function norm_img = normalize (IMG) 92 if size (IMG, 3 )~ = 1 93 IMG = rgb2gray (IMG); 94 end 95 norm_img = zeros (SIZE (IMG); 96 mu = mean (IMG (:)); 97 ST = max (STD (double (IMG (:)), EPS); 98 norm_img = bsxfun (@ minus, IMG, Mu); 99 norm_img = bsxfun (@ rdivide, norm_img, St ); 100 end101 % ============================================ ========================= ========================================================== ====================================== 104% calculate the pyramid of the image, layers are composed of NL If the evels parameter is specified, a struct is returned, including the nlevels image 105 function pyimg = pyramid (IMG, nlevels) 106%, and the image 107 pyimg = cell (nlevels, 1) in each layer of the pyramid is created ); 108 pyimg {1} = IMG; 109 for I = 2: nlevels110 pyimg {I} = downsample (pyimg {I-1 }); 111 end 112 end113 % ======================================== ================================================================= 114 115% ========================================================== ============================================ 116% downsample the image (2*2) and smooth handling 11 7 function d_img = downsample (IMG) 118% if the row and column of the original image are the base number, add 119 If Mod (SIZE (IMG, 1), 2 )~ = 0120 IMG = [img (1, :); img]; 121 end122 if Mod (SIZE (IMG, 2), 2 )~ = 0123 IMG = [img (:, 1), img]; 124 end125 [R, C] = size (IMG); 126 d_img = zeros (R/2, c/2). 127 for I = 1: 2: r128 for j = 1: 2: c129 x = (I + 1)/2; 130 y = (J + 1) /2; 131 d_img (x, y) = sum (IMG (I: I + 1, J: J + 1)/4; 132 end1_end134 end
The test code is as follows:
1 src = rgb2gray(imread(‘src.jpg‘));2 template = rgb2gray(imread(‘template.jpg‘));3 [x,y,ncc_Img] = pyramidMatch(src, template, 4);
The left is the source image, the middle is the template image, and the right is
Pyramid-based template matching algorithm