【问题标题】:error in corr2 function in matlabmatlab中corr2函数的错误
【发布时间】:2018-06-23 04:42:58
【问题描述】:

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

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

用于创建模板的 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,输入图像的尺寸也相同。请提供进一步的建议。

【问题讨论】:

  • 如果您添加问题中使用的数据样本会很棒。
  • @nagdwi 我已经给出了问题中的输入图像。我已经从输入图像中裁剪了字母和符号并创建了模板以执行匹配任务。
  • 如果你像下面的例子一样添加你的读取图像将更容易检查你的代码stackoverflow.com/questions/48243972/…

标签: matlab


【解决方案1】:

您只需要使用rgb2gray 将 rgb 转换为灰度图像(二维矩阵),例如:

%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

【讨论】:

  • 感谢侯赛因·卡。进行 rgb2gray 转换后代码工作正常。
猜你喜欢
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多