Find Location of Object in Image Using Template Matching
Read the reference image and the template image into the MATLAB workspace.
img = imread('stopSignTest.jpg'); tempImg = imread('vipwarningsigns_stop_template.png');
Display the images. The reference image is a scene containing the stop sign board. The template is a low spatial resolution image of the stop sign board.
figure imshow(img) title('Reference Image') figure imshow(tempImg) title('Template')
Open the Simulink® model.
modelname = 'ex_blktemplatematching.slx';
open_system(modelname)
The model reads the images by using the Image From Workspace block. To perform template matching, you must first convert the input color images to intensity images by using the Color Space Conversion block. Then, find the location of the template image in the reference image by using the Template Matching
block with these parameter values:
Match Metric -
Sum of absolute differences
Output -
Best match location
Search method -
Three-step
The Template Matching
block outputs a location in the reference image for which the pixel regions around it best matches with the template image.
Run the model.
out = sim(modelname);
Read the output value.
location = out.simout;
Draw a circle to highlight the region around the best matching pixel location. Display the results.
img = insertShape(img,'circle',[location(1) location(2) 20]); figure imshow(img); hold on plot(location(1),location(2),'*r') title('Results of Template Matching')