matlab中corr2函数的错误

时间:2018-01-14 08:42:14

标签: matlab

我想通过使用corr2函数的模板匹配来读取输入图像中的字母。为此,我创建了数字和符号模板。 在运行读取信件的代码时,我收到以下错误:

Error using corr2
Expected input number 1, A, to be two-dimensional.

enter image description here

用于模板创建的MAtlab代码

%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代码

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

对于种类信息:模板中单个组件的大小为42x24x3 uint8,输入图像也是相同的维度。请提供进一步的建议。

1 个答案:

答案 0 :(得分:0)

您只需使用rgb2gray将rgb转换为灰色图像(2-d矩阵),如:

%CREATE TEMPLATES
one=rgb2gray(imread('1.png')); 
one=imresize(one,[42 24]);
two=rgb2gray(imread('2.png'));
two=imresize(two,[42 24]);
three=rgb2gray(imread('3.png'));
three=imresize(three,[42 24]);
number=[one two three];
NewTemplates1=mat2cell(number,42,[24 24 24]);
snap=rgb2gray(imread('2Ttt0.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 

end