2012-02-07 41 views
1

我已經裁剪出如下所示的對象的單個圖像,並且需要進行模板匹配。如何將模板匹配方法應用於我當前的編碼?感謝如何做圖像匹配?

處理的圖像:

http://i40.tinypic.com/2egh06o.jpg

模板:

http://i43.tinypic.com/531pvs.jpg

im=imread('bus70.jpg'); 
    im=rgb2gray(im); % convert to gray scale 
    im=im>graythresh(im)*255; % covert to binary 
    siz=size(im); % image dimensions 
    L=bwlabel(im,8); % Label the disconnected foreground regions (using 8 conned neighbourhood) 
    % Get the bounding box around each object 
    bb=regionprops(L,'BoundingBox'); 
    % Crop the individual objects and store them in a cell 
    n=max(L(:)); % number of objects 
    ObjCell=cell(n,1); 
    for i=1:n 
     % Get the bb of the i-th object and offest by 2 pixels in all 
     % directions 
     bb_i=ceil(bb(i).BoundingBox); 
     idx_x=[bb_i(1)-2 bb_i(1)+bb_i(3)+2]; % bb_i(1) read from 1st location 
     idx_y=[bb_i(2)-2 bb_i(2)+bb_i(4)+2]; 
     if idx_x(1)<1, idx_x(1)=1; end 
     if idx_y(1)<1, idx_y(1)=1; end 
     if idx_x(2)>siz(2), idx_x(2)=siz(2); end 
     if idx_y(2)>siz(1), idx_y(2)=siz(1); end 
     % Crop the object and write to ObjCell 
     im=L==i; 
     ObjCell{i}=im(idx_y(1):idx_y(2),idx_x(1):idx_x(2)); 
    end 
    % Visualize the individual objects 
    figure 
    for i=1:n 
     subplot(1,n,i) 

     imshow(ObjCell{i}) 
    end 
    clear im L bb n i bb_i idx_x idx_y siz 

回答

2
  1. 運行歸一化互相關性 - normxcorr2
  2. 在結果中找到峯值(最大值)。
  3. 這是模板的位置。 (Center)

This也許會讓你感興趣。

+0

我該如何在編碼中實現這一點? – celine 2012-02-08 07:37:35