error in corr2 function in matlab

4 visualizzazioni (ultimi 30 giorni)
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=imread('1.png');
one=imresize(one,[42 24]);
two=imread('2.png');
two=imresize(two,[42 24]);
three=imread('3.png');
three=imresize(three,[42 24]);
four=imread('4.png');
four=imresize(four,[42 24]);
five=imread('5.png');
five=imresize(five,[42 24]);
zero=imread('0.png');
zero=imresize(zero,[42 24]);
sign=imread('sign.png');
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
load NewTemplates1 % Loads the templates of characters in the memory.
snap=imread('untitled.png');
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 Commenti
wiqar Ahmad
wiqar Ahmad il 5 Feb 2021
can you please share me the correct code?
Will be Thankfull
Walter Roberson
Walter Roberson il 5 Feb 2021
As discussed below: use rgb2gray() before doing the corr2.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 14 Gen 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.
  1 Commento
SHOBA MOHAN
SHOBA MOHAN il 15 Gen 2018
The code works fine after doing rgb2gray conversion. Thanks Walter

Accedi per commentare.

Più risposte (1)

John BG
John BG il 15 Gen 2018
Modificato: John Kelly il 16 Gen 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=imread('1.png');one=one(:,:,1);
% one=imresize(one,[42 24]);
two=imread('2.png');two=two(:,:,1);
% two=imresize(two,[42 24]);
three=imread('3.png');three=three(:,:,1);
% three=imresize(three,[42 24]);
four=imread('4.png');four=four(:,:,1);
% four=imresize(four,[42 24]);
five=imread('5.png'); five=five(:,:,1);
% five=imresize(five,[42 24]);
zero=imread('0.png');zero=zero(:,:,1);
% zero=imresize(zero,[42 24]);
sign=imread('sign.png');sign=sign(:,:,1);
% 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
load NewTemplates1 % Loads the templates of characters in the memory.
snap=imread('untitled.png');snap=snap(:,:,1);
% 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
  5 Commenti
Walter Roberson
Walter Roberson il 15 Gen 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
John BG il 16 Gen 2018
Thanks again for this other clarification :)

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by