# error in corr2 function in matlab

3 views (last 30 days)
SHOBA MOHAN on 14 Jan 2018
Commented: Walter Roberson on 5 Feb 2021
I have created templates of number and symbols and want to match the vehicle number plate with template using corr2 function. While running the code got the following error
Error using corr2
Expected input number 1, A, to be two-dimensional.
MAtlab code for template creation
%CREATE TEMPLATES
one=imresize(one,[42 24]);
two=imresize(two,[42 24]);
three=imresize(three,[42 24]);
four=imresize(four,[42 24]);
five=imresize(five,[42 24]);
zero=imresize(zero,[42 24]);
sign=imresize(sign,[42 24]);
number=[one two three four five zero ];
character=[number sign];
NewTemplates1=mat2cell(character,42,[24 24 24 24 24 24 24], 3);
save ('NewTemplates1','NewTemplates1')
clear all
matlab code for reading the letter
snap=imresize(snap,[42 24]); % Resize the input image so it can be compared with the template's images.
comp=[ ];
for n=1:length(NewTemplates1)
sem=corr2(NewTemplates1{1,n},snap); % Correlation the input image with every image in the template for best matching.
comp=[comp sem]; % Record the value of correlation for each template's character.
end
vd=find(comp==max(comp)); % Find the index which correspond to the highest matched character.
%*-*-*-*-*-*-*-*-*-*-*-*-*-
% Accodrding to the index assign to 'letter'.
% Alphabets listings.
if vd==1
letter='1';
elseif vd==2
letter='2';
elseif vd==3
letter='3';
elseif vd==4
letter='4';
elseif vd==5
letter='5';
elseif vd==6
letter='0';
elseif vd==7
letter='sign';
end
For kind information: The size of individual component in template is 42x24x3 uint8 and the input image also same dimension. Please provide the suggestion to do further.
##### 3 CommentsShow 1 older commentHide 1 older comment
wiqar Ahmad on 5 Feb 2021
can you please share me the correct code?
Will be Thankfull
Walter Roberson on 5 Feb 2021
As discussed below: use rgb2gray() before doing the corr2.

Walter Roberson on 14 Jan 2018
You are asking corr2 to operate on a 42 x 24 x 3 array. That is a 3d array which is not something that corr2 can use. Your array is 3d because you are carrying through rgb. You need to do each plane independently, or you need to switch to greyscale, or you need to reshape so that you are working in 2d.
SHOBA MOHAN on 15 Jan 2018
The code works fine after doing rgb2gray conversion. Thanks Walter

John BG on 15 Jan 2018
Edited: John Kelly on 16 Jan 2018
Hi Shoba Mohan
Since the actual size of the input images, both the references and the unknown input to correlate with the references, I have omitted the imresize lines.
With that, one way to have your code running is as follows:
clear all;clc
% one=imresize(one,[42 24]);
% two=imresize(two,[42 24]);
% three=imresize(three,[42 24]);
% four=imresize(four,[42 24]);
% five=imresize(five,[42 24]);
% zero=imresize(zero,[42 24]);
% sign=imresize(sign,[42 24]);
number=[one two three four five zero ];
character=[number sign];
NewTemplates1=zeros([size(one) 7]);
% mat2cell(character,42,[24 24 24 24 24 24 24], 3);
NewTemplates1(:,:,1)=one;
NewTemplates1(:,:,2)=two;
NewTemplates1(:,:,3)=three;
NewTemplates1(:,:,4)=four;
NewTemplates1(:,:,5)=five;
NewTemplates1(:,:,6)=zero;
NewTemplates1(:,:,7)=sign;
save ('NewTemplates1','NewTemplates1')
clear all
% snap=imresize(snap,[42 24]); % Resize the input image so it can be compared with the template's images.
comp=[ ];
[sz1 sz2 sz3]=size(NewTemplates1);
for n=1:sz3
sem=corr2(NewTemplates1(:,:,n),snap); % Correlation the input image with every image in the template for best matching.
comp=[comp sem]; % Record the value of correlation for each template's character.
end
[vd,col,q]=find(comp==max(comp)); % Find the index which correspond to the highest matched character.
%*-*-*-*-*-*-*-*-*-*-*-*-*-
% Accodrding to the index assign to 'letter'.
% Alphabets listings.
if vd==1
letter='1';
elseif vd==2
letter='2';
elseif vd==3
letter='3';
elseif vd==4
letter='4';
elseif vd==5
letter='5';
elseif vd==6
letter='0';
elseif vd==7
letter='sign';
end
Now the code doesn't return error but the correlation doesn't catch some of the samples I have used.
Wouldn't it be reasonable to consider already working MATLAB application for car plate recognition, like:
.
thanks in advance for time and attention
John BG
Walter Roberson on 15 Jan 2018
The rules about license plates on the front of cars are provincial in Canada, so some provinces require them and some do not.
John BG on 16 Jan 2018
Thanks again for this other clarification :)