I have cropped out individual image of the objects as shown below and require to do a template matching. I have read out on this
but could not understand. How can I apply the template matching method to my current coding? thanks
Original Image:
Processed Image:
Template:
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

답변 (1개)

David Young
David Young 2012년 2월 7일

0 개 추천

If you have the Image Processing Toolbox, use normxcorr2. In the documentation, "template" means your small image, and "A" is the large original image. You find the maximum of the result to get the accuracy of the match and the coordinates at which the best match occurs.

댓글 수: 2

lingru celine
lingru celine 2012년 2월 8일
@David Young
thanks ,,,can i have the coding ?
lingru celine
lingru celine 2012년 2월 8일
@David Young
I have tried [normxcorr2], but seemed not work for matching? below is my code, any part need to modify?
cover = rgb2gray(imread('bus70.jpg'));
template = rgb2gray(imread('70blackA.jpg'));
[m n] = size(template);
Icorr = normxcorr2(template,cover);
[max_c, imax] = max(abs(Icorr(:)));
[yp, xp] = ind2sub(size(Icorr),imax(1));
xy = [(xp-n) (yp-m)];
figure, imshow(cover); hold on;
rectangle('position',[xy(1) xy(2) n m],...
'edgecolor','g','linewidth',2);

댓글을 달려면 로그인하십시오.

질문:

2012년 2월 7일

편집:

2013년 10월 4일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by