Strategy for matching multiple templates in an image

1 次查看(过去 30 天)
I have followed the concept given in the link :- https://www.youtube.com/watch?v=Q-OzmDen4HU . With these thing i was able to get a single template match. But I have a problem definition wherein i need to find all the templates in an image. I have gone through a link http://docs.adaptive-vision.com/current/studio/machine_vision_guide/TemplateMatching.html which suggests threshold or maxima for getting all the templates.But i was not able to get how to define a threshold or strategy for getting all templates in an image. The following code gives me the best match :-
[rowcc, colcc] = size(cc);
[max_cc, imax] = max(abs(cc(:)));
[ypeak xpeak] = ind2sub(size(cc), imax(1));
peakvalues = cc(xpeak,ypeak);
BestRow = ypeak - (crows-1);
BestCol = xpeak - (ccols-1);
val = cc(BestRow, BestCol);
figure, imshow(A);
h = imrect(gca, [BestCol BestRow (ccols - 1) (crows - 1)]);
How can i change this code to get all possible best matches instead of one ? Thanks in advance!

回答(1 个)

Latika Channapattan
Latika Channapattan 2018-12-10
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB color demo image.
folder = fullfile('C:\Users\aditi\Desktop\New folder');
baseFileName = '49.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
smallSubImage1 = imcrop(rgbImage, [250 100 2500 2500]);
smallSubImage2 = imcrop(rgbImage, [500 100 2500 2500]);
smallSubImage3= imcrop(rgbImage, [675 100 2500 2500]);
smallSubImage4 = imcrop(rgbImage, [900 100 2500 2500]);
smallSubImage5 = imcrop(rgbImage, [1050 100 2500 2500]);
smallSubImage6 = imcrop(rgbImage, [1250 100 2500 2500]);
smallSubImage7 = imcrop(rgbImage, [1450 100 2500 2500]);
smallSubImage8 = imcrop(rgbImage, [1670 100 2500 2500]);
smallSubImage9 = imcrop(rgbImage, [1875 100 2500 2500]);
smallSubImage10 = imcrop(rgbImage, [2100 100 2500 2500]);
correlationOutput = normxcorr2(smallSubImage1(:,:,3), rgbImage(:,:,3));
title('Correlation Output', 'FontSize', fontSize);
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
[ypeak, xpeak] = ind2sub(size(correlationOutput),maxIndex(1));
corr_offset1 = [(xpeak-size(smallSubImage1,2)) (ypeak-size(smallSubImage1,1))];
imshow(rgbImage);
hold on
corr_offset2 = [(xpeak-size(smallSubImage2,2)) (ypeak-size(smallSubImage2,1))];
imshow(rgbImage);
hold on
corr_offset3 = [(xpeak-size(smallSubImage3,2)) (ypeak-size(smallSubImage3,1))];
imshow(rgbImage);
hold on
corr_offset4 = [(xpeak-size(smallSubImage4,2)) (ypeak-size(smallSubImage4,1))];
imshow(rgbImage);
hold on
corr_offset5 = [(xpeak-size(smallSubImage5,2)) (ypeak-size(smallSubImage5,1))];
imshow(rgbImage);
hold on
corr_offset6 = [(xpeak-size(smallSubImage6,2)) (ypeak-size(smallSubImage6,1))];
imshow(rgbImage);
hold on
corr_offset7 = [(xpeak-size(smallSubImage7,2)) (ypeak-size(smallSubImage7,1))];
imshow(rgbImage);
hold on
corr_offset8 = [(xpeak-size(smallSubImage8,2)) (ypeak-size(smallSubImage8,1))];
imshow(rgbImage);
hold on
corr_offset9 = [(xpeak-size(smallSubImage9,2)) (ypeak-size(smallSubImage9,1))];
imshow(rgbImage);
hold on
corr_offset10 = [(xpeak-size(smallSubImage10,2)) (ypeak-size(smallSubImage10,1))];
imshow(rgbImage)
hold on;
rectangle('position',[corr_offset1(1) corr_offset1(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset2(1) corr_offset2(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset3(1) corr_offset3(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset4(1) corr_offset4(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset5(1) corr_offset5(2) 175 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset6(1) corr_offset6(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset7(1) corr_offset7(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset8(1) corr_offset8(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset9(1) corr_offset9(2) 190 950],...
'edgecolor','g','linewidth',2);
rectangle('position',[corr_offset10(1) corr_offset10(2) 190 950],...
'edgecolor','g','linewidth',2);

Community Treasure Hunt

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

Start Hunting!

Translated by