%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Matching an image using multiscale search % % % %Hao Jiang, Oct 2007 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% scale_levels = 3; im_target = imread('spider1.bmp'); im_target = im2double(im_target); imt_pyramid = cell(3,1); % scale the target image into different levels for n = 1 : scale_levels - 1 imt_pyramid{n} = im_target; im_target = imresize(im_target, 0.5); end im = imread('spider2.bmp'); im = im2double(im); figure(1); imshow(im); [x,y] = ginput(2); x = floor(x); y = floor(y); imb = zeros(max(y)-min(y)+1, max(x)-min(x)+1, 3); imb(:,:,1) = im(min(y):max(y), min(x):max(x), 1); imb(:,:,2) = im(min(y):max(y), min(x):max(x), 2); imb(:,:,3) = im(min(y):max(y), min(x):max(x), 3); [tph, tpw, tpd] = size(imb); pp = [x(1), y(1); x(1) + tpw - 1, y(1);... x(1) + tpw - 1, y(1) + tph - 1;... x(1), y(1) + tph - 1]; hold on; plot([pp(:,1); pp(1,1)], [pp(:,2); pp(1,2)], 'LineWidth', 3, 'Color', [1,0,0]); hold off; im_template = imb; ims_pyramid = cell(3,1); % scale the template into several levels for n = 1 : scale_levels - 1 ims_pyramid{n} = im_template; im_template = imresize(im_template, 0.5); end tic % multiscale search bb = locate_imageblock(im_template, im_target); for n = scale_levels - 1 : -1 : 1 bb = bb * 2; bb = local_search(ims_pyramid{n}, imt_pyramid{n}, bb(1,1), bb(1,2)); end toc figure(2); imshow(imt_pyramid{1}); hold on; plot([bb(:,1); bb(1,1)], [bb(:,2); bb(1,2)], 'LineWidth', 3); hold off;