How to get vision.TemplateMatcher to work with colored templates and images?
3 views (last 30 days)
Show older comments
I've been working with the computer vision toolbox, and trying out to use vision.TemplateMatcher to use a template of a truecolor image to find if the template exists on another truecolor image. The documentation says it is however I am getting
Error using vision.TemplateMatcher/parenReference
Data with more than two dimensions is not supported for this System object.
My code looks like this:
Jf=imread("JettFront.png");
picture=imread("Screenshot (186).png");
tMatcher= vision.TemplateMatcher;
location=tMatcher(picture, Jf);
Any ideas on what I should do?
1 Comment
Satwik
on 19 Feb 2025
Hi,
I understand the documention for 'vision.TemplateMatcher' suggests that we can use both truecolor and M-by-N 2-D grayscale image as input arguments. However, 'imread' results in a M-by-N-by-3 array when used on a truecolor image. This might be the probable cause for the error :
"Data with more than two dimensions is not supported for this System object.".
As a workaround, I suggest converting the truecolor image into grayscale using the 'rgb2gray' function before using it with 'vision.TemplateMatcher'.
For more information on the usage of 'rgb2gray', you may refer to the following documentation:
Answers (1)
Hitesh
on 5 Mar 2025
Hi Tim,
The error you are encountering is due to the "vision.TemplateMatcher" system object does not support truecolor (RGB) images directly. It expects grayscale images, which are two-dimensional. To resolve this, you need to convert your truecolor images to grayscale before using the vision.TemplateMatcher.
Kindly refer to the revised code and comments mentioned:
Jf = imread("JettFront.png");
picture = imread("Screenshot (186).png");
% Convert the images to grayscale
JfGray = rgb2gray(Jf);
pictureGray = rgb2gray(picture);
% Create the template matcher object
tMatcher = vision.TemplateMatcher;
% Find the location of the template in the image
location = tMatcher(pictureGray, JfGray);

For more information regarding the "vision.TemplateMatcher", kindly refer to the following MATALB Documentation:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!